setcookie problems

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

WillemM
New php-forum User
New php-forum User
Posts: 3
Joined: Sun Nov 24, 2002 10:28 am
Contact:

setcookie problems

Postby WillemM » Sun Nov 24, 2002 10:30 am

I have this code:

Code: Select all


if(($username == 'testuser') && ($password == 'testpassword'))
{
  setcookie('userid',1,$cookie_life);
  header('Location: index.php');
}


When I use this, my cookie doesn't become set.
How can I solve this ?[/code]

User avatar
WiZARD
Moderator
Moderator
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Re: setcookie problems

Postby WiZARD » Mon Nov 25, 2002 12:17 am

Hi!
Try this:

Code: Select all

if(($username == 'testuser') && ($password == 'testpassword'))
{
  setcookie("userid"',"1", time() - 3600);
  header('Location: index.php');
  exit;
}else
{
  setcookie("userid"',"0", time() - 3600);
  header('Location: index.php');
  exit;
}

As you see firstly your script nothing to do, couse one of your variable ($username or $password) not sets.

WillemM
New php-forum User
New php-forum User
Posts: 3
Joined: Sun Nov 24, 2002 10:28 am
Contact:

Postby WillemM » Mon Nov 25, 2002 3:10 am

OK my code does not generate anymore errors.

But when I go to page 2 I cannot read the cookie value anymore.

I do this:

Code: Select all

echo $_COOKIE['userid'];


It says something like: variable not set !


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot], Google Feedfetcher and 2 guests