setcookie and isset($_COOKIE(name)) seem very finnicky.


Posted on 16th Feb 2014 07:03 pm by admin

I'm currently playing around with a user system with login and registration. I'm trying to use cookies to log the user in but the cookie either doesn't set or it sets after I travel through a few pages. Also, when I use isset($_COOKIE(name)) it sometimes doesn't return that it is set even though it is.

login.php (the username and password request page which I am currently using to display if a cookie is set)
Code: <?php
if (isset($_COOKIE["grocerycookie"]))
{
$username = $_COOKIE["grocerycookie"];
echo "You are logged in, <b>$username</b><br />";
}
else
{
echo "You have not been logged in.";
}
?>


login2.php (sets the cookie)
Code: <?php
mysql_connect("*", "*", "*") or die(mysql_error());
mysql_select_db("*") or die(mysql_error());
$username = strtolower($_POST['username']);
$password = $_POST['password'];
$data = mysql_query("SELECT * FROM * WHERE username='$username'") or die(mysql_error());

while($info = mysql_fetch_array($data))
{
if ($password == $info['password'])
{
setcookie("grocerycookie", $username, time()+3600);
}
}

Am I setting the cookie wrong or checking for it wrong?

No comments posted yet

Your Answer:

Login to answer
112 Like 47 Dislike
Previous forums Next forums
Other forums

2 decima places & How to reload my page
Hello There,

How do i put full-stop (.) after second figure from behind? ie if i have 123456

session variables not working anymore
hello,
i use sessions to handle authentication within my site. my host has been changing a lot of

If a record matches an existing record do nothing
So I don't know if I should put this here or in mysql, but what my script is for is for people to ad

Unique Visitor Tracking
Hello. Currently, I run a voting site where voters are tracked by their IP address and can only vote

ECC6 - Single sign-on
We are in the process of upgrading to ECC 6 which will support single sign on with user passwords al

How to search for several parameters from objects in a database?
I have a database with lots of information about objects.
Now I would like to search for 4 or 5 p

New to Arrays
Hi I am new to PHP (a week and a half now) and I am just beginning to read about arrays. I understan

mysqli_fetch_assoc returns multiple arrays, how to return a single array
Hello, I am using mysqli_fetch_assoc which is returning multiple rows, so it looks like:

Code

echo $rows help
Hi all,

Hopefully something simple that I don't know!

I have this:

Code: [Selec

Header is not working in IE
Hi ! header function is not working in IE but it works in FF, Safari, Chrome. any help please. :

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash