简体   繁体   中英

php setcookie() not working correct

I'm trying to get the hang of how setcookie() works in php so i made i simple script for testing purposes. The only thing the script does is ask for a username and then makes a cookie for it. when you enter the same username again it should welcome you back. if it is the first time you entered that username it should welcome you for the first time. Here's what i did:

This is the form;

<!DOCTYPE html>
<html lang="nl">
<head>
<title>XXL Computer winkel</title>
</head>
<body>
<h3>php lab 12</h3>
<table border=0 cellpadding=0 cellspacing=0 >
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<tr>
<td>gebruikersnaam</td>
<td><input type="text" name="user" /></td>
<td><input type="submit" name="send" value="inloggen" /> </td>
</tr>
</form>
</table>
<?php

include ("cookiefuncties.php");

welkom();

?>
</body>
</html>

cookiefuncties.php looks like this:

<?php

function welkom()
{

if(isset($_POST['user']))
{
    $cookie_value = $_POST['user'];
    $cookie_name = "gebruikersnaam";
    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); 
}
if(isset($_POST['user'])&& $_COOKIE[$cookie_name] === ($_POST['user']))
    {

        echo "Welcome back " . $_COOKIE[$cookie_name] . "!";
    }
if(isset($_POST['user'])&& $_COOKIE[$cookie_name] != ($_POST['user']))
    {

        echo "Welcome " . $_COOKIE[$cookie_name] . "This is your first visit";
    }
}

?>

Problem is that the cookie is always 'one name late'. Therefore i need to refresh the page and it will always welcome the user back instead of saying it's the first visit. I know there is an option to 'force' a refresh but i don't know how, and if that is the solution.

Greetings, Lennart

ps. Don't mind the spacings. I still don't know how to get that right on this site :-(

You must keep a cookie for each user. Just set a cookie with value true for each new user. Try this code in your cookiefuncties.php :

<?php

function welkom() {

    if(isset($_POST['user'])) {
        $cookie_name = $_POST['user'];
        setcookie($cookie_name, true, time() + (86400 * 30), "/"); 
        if( isset($_COOKIE[$cookie_name]) && true == $_COOKIE[$cookie_name]) {
            echo "Welcome back " . $_POST['user'] . "!";
        } else {
            echo "Welcome " . $_POST['user'] . "This is your first visit";
        }
    }
}

?>

Set the unique name per user cookie and check it exist or not.

   if(isset($_POST['user']) and !empty($_POST['user']))
   {

       $user = $_POST['user'];
       // set the unique cookie name
       $cookie_name = "gebruikersnaam_$user";    
       $setCookie = true;

       if(isset($_COOKIE[$cookie_name]) and $_COOKIE[$cookie_name] = $user )
       {
           echo "Welcome Back ".$user;
       }else
       {
           echo "This is your first visit $user";
       }

       setcookie($cookie_name, $user, time() + (86400 * 30), "/"); 

   }

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