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
Did you know?Explore Trending and Topic pages for more stories like this.
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

array_count_values question?
ok i'm trying to create a top ten list from a database of listed favorite movies.

the followi

FFmpeg Images 2 Video conversion problem ??
I got folder full of images , number of images = 600 , I want to make a video which plays one image

Select Rows as Columns..
is there a way to select COLUMN_NAME from user_tab_columns where table_name='TABLENAME';
and ha

Multiple WHILE loops help
Hi there. I'm writing an application that is pulling and displaying results for real estate listing

Multi Level Array Problem
hi all,

For example I have array like below:

$temp = array(array('north america', 'us'

Function
How can I make this function that once you click the link in the code it takes you to a new page wit

Help with page encoding issue and weird characters
I'm trying to write a screen scraper and when I pull out the lines of the html file that I'm interes

question about stripslashes and real_escape_string
im cleaning up an old app that I wrote fixing some of the vulernabilities from attacks.

I hav

Code Help.. If txt input box empty search X instead..
I am having a problem with my search script. At current it will simply search by a selected date whi

Passing variables with pagination - iterating through unique id per link
Hi everyone,
I have seen a few topics like this one. Still cant find the specific thing, so I am

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