FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Cookies Not Working





Horizon
I'll start off with the code:
Code:

<?php

session_start();         
   
$clear = $_GET['clear'];
$act = $_GET['act'];   
   
if ($clear == "cookie")
{
setcookie("test", "", -10000000);
}

if ($clear == "session")
{
unset($_SESSION['test']);
}

if ($clear == "both")
{
unset($_SESSION['test']);
setcookie("test", "", -10000000);
}

if ($act == "cookie")
{
setcookie("test", "Tester", 10000000);
}

if ($act == "session")
{
$_SESSION['test'] = "Testttt";
}

if ($act == "both")
{
$_SESSION['test'] = "Testttt";
setcookie("test", "Tester", 10000000);
}

$cookie = $_COOKIE['test'];      
$session = $_SESSION['test'];      
      
      
      
      
   echo "Cookie ({$_COOKIE['test']}) and session ({$_SESSION['test']}) testing.<br /><br />";
   
   
   
   
   if (isset($cookie)){ echo "<a href=\"login.php?clear=cookie\">Clear the Cookie.</a><br />"; }
   if (isset($session)){ echo "<a href=\"login.php?clear=session\">Clear the Session.</a><br />"; }
   if ((isset($session)) || (isset($session))){ echo "<a href=\"login.php?clear=both\">Clear both.</a><br /><br /><br />"; }
   
   if (!isset($cookie)){ echo "<a href=\"login.php?act=cookie\">Activate the Cookie.</a><br />"; }
   if (!isset($session)){ echo "<a href=\"login.php?act=session\">Activate the Session.</a><br />"; }
   if ((!isset($session)) || (!isset($session))){ echo "<a href=\"login.php?act=both\">Activate both.</a><br /><br /><br />"; }
   
?>


Long, sorry, but I wanted to get precise. I am a noob when it comes to PHP cookies, but I've learned enough to at least start.. or so I thought. For some reason, after many attempts, I am unable to get the cookie to work. The session works completely fine, but I'm not sure if it's 'hurting' the cookie.

I'm also sure that just because of the size of this simple script, there's more than just cookie errors Rolling Eyes. Yes, I do clear my cookies before every test. I've tried this in both IE and Google Chrome and the result is the same: The cookie never seems to be listed.

Any help is appreciated. Just note that the above script was made for testing, so no comments on how bad it is Evil or Very Mad
Peterssidan
You set you cookies to expire at 10000000 which is around 26 April 1970 d'oh! Wink

The time is the number of seconds from the year 1970 sometime. the php function time() will give you the current time. Use that and add a constant instead:
Code:
setcookie("test", "Tester", time() + 10000000);
imagefree
you cannot get the value of 'test' cookie on the same page visit. YOu can get the value set during 1st page load in the next page loads.
Horizon
Peterssidan wrote:
You set you cookies to expire at 10000000 which is around 26 April 1970 d'oh! Wink

The time is the number of seconds from the year 1970 sometime. the php function time() will give you the current time. Use that and add a constant instead:
Code:
setcookie("test", "Tester", time() + 10000000);


Oh my, maybe that's why Shocked. Yes, I did try it with the time() inserted and it is now giving me the cookie, but with one little detail that will be explained in the next quote.

Quote:
you cannot get the value of 'test' cookie on the same page visit. YOu can get the value set during 1st page load in the next page loads.


Which explains why I always have to click on a different link after I activate the cookie to see the cookie.


Thanks for the help you two, everything's working fine now.
imagefree
thanks. always here to help you.
Related topics
managing a bilingual site
Do we support php include and/or SSI?
PHP Tutorials
[php/cookies] poll on two sites
Php supported?
Index.php not found
Disclaimer Redirection with cookies/sessions?
How to get a php script working under a cron job?
PHP not working
PHP Cookies - Problems
Cookies - Sessions
PHP Not Working on Localhost
php not on?
Need a PHP expert or Wordpress wizard
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.