简体   繁体   中英

How to move cookies from one page to another in PHP?

I am dealing with a PHP site that use cookies to login users; the problem is I can't get the input email from the login page to the home page.

I always get this in the home page:

(Welcome Email!)

where it should be:

(Welcome user@gmail.com!)

Here is my code of the home page:

<?php
 if (isset($_COOKIE["Email"])){

  echo "Welcome " . $_COOKIE["Email"]. "!<br>";
  echo '<a href="logoutForm.php">logout</a>';  
}
else{
setcookie("Email", "Email", time()-50000);
echo 'you are logged out please <a href="log_in.php">login</a>'; 
}
?>

Have you tried using sessions instead?

After successful login, before redirect:

session_start();
$_SESSION['Email']="user@gmail.com";

Then on your welcome page:

<?php

 session_start();

 if(isset($_SESSION["Email"])){
    echo "Welcome " . $_SESSION["Email"]. "!<br>";
    echo '<a href="logoutForm.php">logout</a>';  
 }
 else{
    setcookie("Email", "Email", time()-50000);
    echo 'you are logged out please <a href="log_in.php">login</a>'; 
 }

?>

This also has the added benefit of storing the session info on your server as opposed to on the user's computer.

To kill the session just use:

session_destroy();

您需要在登录页面上向我们显示代码,以便我们了解您如何设置“ Email” cookie变量,就像您使用的一样

    setcookie("Email", "bla_blah_blah");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM