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

There was a problem with the request. (Code: 0)
Any Idea what could cause this? I get it randomly at best. But with both IE and FF. It derives from

Quick fix: Conditional statement with an array
Hi, I'm getting the temperature value off of the Environment Canada website along with the icon file

need a script for 3 pages
I have a customer who wants me to set up a site with a number of pages with the following requiremen

Taxonomy? Classification? Categorisation?
Not sure if there is a way around this classification problem
I have a supplier who produces

Just cannot get 'area' to submit to db using same php code as already used
Basically all i have done is just modified some code and added an area section.
The form is allre

help countdown timers
hello every one,

I'd like to know how to insert many countdowns in the same page.
The dura

Convert array to string and write to csv file
I am looking to write a MySQL query to a csv file.

At the minute I:

check for the orig

JSON SORT WITH PHP
I have two products that I want to sort by say "Id:17, value: xxx" using php
The page w

Lining up columns nicely.
Hello everyone, I'm having problems with making columns line up properly. Here is my code:

Function to extract email attachments using PHP IMAP
function extract_attachments($connection, $message_number) {

$attachments = array();

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