根据 url 中的 ?id= 参数重定向

Redirect based on the ?id= parameter in the url

我有一个在 PHP 和 MySQL 中构建的登录系统。 如果我的任何用户登录,他们将被定向到 user.php 页面

我有一个 authenticate.php 页面,可以根据用户的 ID 将用户重定向到某个页面。这是将 id 添加到 URL 的代码:header("Location: user.php?id=".$id); )

<?php
session_start();
// Change this to your connection info.
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = 'root';
$DATABASE_NAME = 'phplogin';
// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
    // If there is an error with the connection, stop the script and display the error.
    exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}

// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
    // Could not get the data that should have been sent.
    exit('Please fill both the username and password fields!');
}

// Prepare our SQL, preparing the SQL statement will prevent SQL injection.
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
    // Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute();
    // Store the result so we can check if the account exists in the database.
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
        $stmt->bind_result($id, $password);
        $stmt->fetch();
        // Account exists, now we verify the password.
        // Note: remember to use password_hash in your registration file to store the hashed passwords.
        if ($_POST['password'] === $password) {
            // Verification success! User has logged-in!
            // Create sessions, so we know the user is logged in, they basically act like cookies but remember the data on the server.
            session_regenerate_id();
            $_SESSION['loggedin'] = TRUE;
            $_SESSION['name'] = $_POST['username'];
            $_SESSION['id'] = $id;
            header("Location: user.php?id=".$id);
        } else {
            // Incorrect password
            echo 'Incorrect username and/or password!';
        }
    } else {
        // Incorrect username
        echo 'Incorrect username and/or password!';
    }

    $stmt->close();
}
?>

我可以在原始 user.php 文件上编写一个 javascript 函数来读取 URL 中的用户 ID 并重定向到我网站上的特定页面吗?

示例:

用户 1 需要离开

来自:http://www.mysite/user.php?id=1

收件人:http://www.mysite/dashboard/clientA/home.php

用户 2 需要离开

来自:http://www.mysite/user.php?id=2

收件人:http://www.mysite/dashboard/clientB/home.php

您好,您可以执行以下操作:

//function redirect to page
function redirect(){
  var id= <?php echo $_GET['id'];?>;
  if(id==1){
    window.location.href = "url1";
  }
  if(id==2){
    window.location.href = "url2";
  }
}
redirect();