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

Binding 2 UDP sockets on same port, connected to different destinations, 1 receives
Hello,
My application wants to send/recv data to 2 different UDP ports on a remote computer, us

UL and LI Add Form
The idea I want here is when the user click on a character name from the drop down select bar at the

Contact Form Not Working When I Add Validation Codes
So I built my first working PHP "Contact Us" like form. But, I have been trying to now mak

how can i display php source code snippets ?
I am outputting some pho code .. and I want to display the source code so people can copy and paste

WORDPRESS - Multiple Loops Only Going to One Post?
I have various pages with multiple loops and i need them to be individual to all other categories.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
what is wrong with this

Code: [Select]<?php
if ($_SERVER['HTTP_REFERER'])
{

Number Format
Hi All,

I have number '000000000050085' I want to format it to 500.85 Can any one give me

I apologise! It's a newbie to PHP feedback form problem!
Hi,

I am a total newbie to PHP. I am also a unemployed and need to get a job. So after 100% r

radio button reamains checked
Hello i'm trying to keep the state off my radio buttons so it shows which ones where clicked after t

Character increment
Hi,

I am facing a scenario like above,but in my case i want to show up like Col A,Col B etc..

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