user3891365
user3891365

Reputation: 71

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 [email protected]!)

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>'; 
}
?>

Upvotes: 0

Views: 176

Answers (2)

Damilola Olowookere
Damilola Olowookere

Reputation: 2383

You need to show us the code on your login page so that we know how you are setting your "Email" cookie variable, like did you use

    setcookie("Email", "bla_blah_blah");

Upvotes: 1

Steven Mills
Steven Mills

Reputation: 11

Have you tried using sessions instead?

After successful login, before redirect:

session_start();
$_SESSION['Email']="[email protected]";

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();

Upvotes: 1

Related Questions