-->

How to redirect the users to their id specific url

2020-06-06 07:26发布

问题:

How to redirect the users to their id specific urls/landing pages after they log in? The redirected urls' query strings should show values associated with their ids (Primary key) stored in Mysql database.

For instance,

The corresponding values of "id" for 1st, 2nd 3rd and 4th users are 1, 2, 3 & 4

The logged pages / landing pages should look like the following urls respectively after the users are looged in successfully.

www.anysite.com/anydirectory/user.php?id=1

www.anysite.com/anydirectory/user.php?id=2

www.anysite.com/anydirectory/user.php?id=3

www.anysite.com/anydirectory/user.php?id=4

*The log in session of the log in script is as following:*

// this sets session and logs user in**

session_start();

//prevent against session fixation attacks.

   session_regenerate_id (true); 

// this sets variables in the session

$_SESSION['user_id']= $id;  
    $_SESSION['user_name'] = $full_name;
    $_SESSION['user_level'] = $user_level;
    $_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']);

    **//update the timestamp and key for cookie**
    $stamp = time();
    $ckey = GenKey();
    mysql_query("update users set `ctime`='$stamp', `ckey` = '$ckey' where id='$id'") or die(mysql_error());

//set a cookie

  if(isset($_POST['remember'])){
              setcookie("user_id", $_SESSION['user_id'], time()+60*60*24*COOKIE_TIME_OUT, "/");
              setcookie("user_key", sha1($ckey), time()+60*60*24*COOKIE_TIME_OUT, "/");
              setcookie("user_name",$_SESSION['user_name'], time()+60*60*24*COOKIE_TIME_OUT, "/");
               }
               header('Location: user.php');
     }
    }
    else
    {
    //$msg = urlencode("Invalid Login. Please try again with correct user email and password. ");
    $err[] = "Invalid Username Or Password.";

What header I should use to redirect the users that way? Also, how to create the landing page for the same purpose.

Any help shall be well appreciated.

回答1:

Just append the user id from your $_SESSION to the URL you're redirecting to.

header('Location: user.php?id=' . $_SESSION['user_id']);


回答2:

When you are checking for a successful log in, return the user's id in your query. Here's some quick pseudocode:

  • In query: Select user_id where user_name = ? and password=?
  • If results.count = 1 then store the user_id as $user_id
  • Redirect to "www.anysite.com/anydirectory/user.php?id=" . $User_Id


回答3:

Would you like to redirectjust like this :-

www.anysite.com/anydirectory/user.php?id=1

then only do

header("Location: user.php?id=".$userId);