Lennart
Lennart

Reputation: 91

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 :-(

Upvotes: 2

Views: 321

Answers (2)

siddhesh
siddhesh

Reputation: 598

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), "/"); 

   }

Upvotes: 1

Ciprian Stoica
Ciprian Stoica

Reputation: 2439

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";
        }
    }
}

?>

Upvotes: 2

Related Questions