Hello

I havea problem with the setcookie() command. I have some code which sets a cookie during a login. It works perfectly fine when I access the site from one PC but when I try it on a different PC it will not set the cookie? I am accessing it using the following: test.domain.com/admin/login.php

After some extensive research I thought I might have a problem with path or domain in the setcookie string so I created the following code and put it in the base directory and accessed it using www.domain.com/index.php and the cookie still does not set on one machine but does on the other? Can anyone give me some ideas. This is driving me nuts.

Both Machines are Win XP Home, Both same version of IE and both have identical cookie settings in Privacy settings?

CODE:

<?php
if (!isset($_COOKIE["keith"])) {
$value="keith";
setcookie("keith",$value,time()+1000);
header("Location: test.php");
}
?>
<html>

<head>
	<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
	<meta name="generator" content="Adobe GoLive">
	<title>Keith</title>
</head>

<body bgcolor="#ffffff">
	<p><? if (isset($_COOKIE["keith"])) {
		echo $_COOKIE["keith"];
	} else {
		echo "Cookie Not Set";
	}
	?>
	</p>
</body>

</html>

Thank you in advance.

    Has anyone had this happen before?? I have done quite a bit of reading and have seen many things about IE and cookie issues...but have not been able to find a solution.

      Your problem is this:

      if (!isset($_COOKIE["keith"])) { 
      $value="keith"; 
      setcookie("keith",$value,time()+1000); 
      header("Location: test.php"); 
      } 
      

      Change that to

      if (!isset($_COOKIE["keith"])) { 
      header("Location: test.php"); 
      $value="keith"; 
      setcookie("keith",$value,time()+1000); 
      } 
      

      header() should always be sent out first.

        If you complete the cookie, you may get better results

        setcookie('keith',$value,time()+(1000),'/','',0);

        This will set the cookie to the visiting domain and all sub-directories.

          Write a Reply...