Page 1 of 1

Another question: Php wont accept Cookies.

Posted: Tue Jun 10, 2003 4:01 am
by Tokee
Hi,

I use for my php-testing a PC without network. Installed WinXP (and IE), Apache version 1.xx (the newest, just downloaded) and php (also the newest, downloaded the zipfile)..

Well, here is my problem: I tried to code a page containing cookies. The Browser gets that cookie (I know it, browser-settings shows the cookie,including right content), but my neither the same page, nor the next (I have heard that the same page couldnt recieve the cookie) could read the cookie from the browser. What could be wrong? I have read something about ..for using cookies you need the complete adress in the link and it must be sure that the http-protocol is used.. Means this I have to change the link to the second page from <a href=test2.php> to <a href=http://127.0.0.1/test2.php>? Or is the problem WinXP, which wont start the network-driver if no connection to another PC is detected???

I have also tried a simple php-script explaining how cookies work. In the www it worked, downloaded on PC at home it does not!

I hope anybody understand my scribblings..

Posted: Tue Jun 10, 2003 4:12 am
by delorian
It can be WXP problem, but I want to ask how do you refer to your cookie :?: - by $_COOKIE['cookie_name'] :?:

Posted: Wed Jun 11, 2003 1:39 am
by igoy
Can we some code you are using to set cookie as well code you using to retrive the data from cookie... I don't think it should be winXP problem as I'm also using a XP system (currently) without network. and everything works great .. . I you can show some code you are using maybe I can try to figure out.. :)

Posted: Wed Jun 18, 2003 5:35 am
by Tokee
Sure.

One thing I tried is this: (if you want to see it in action visit http://www.chipchapin.com/WebTools/cookietest.php)

Code: Select all

<?php
  $pgEmailSubj = 'CookieTestPage';  // Subject for email target in footer.

  // Control dynamic style sheet
  $styleBodyIndent=true;
  $styleBodyBGcolor="#FFDBB7";

  // This code is mostly from http://www.rnetwork.tv/cookietest/, by Sean McLean
  // This adaptation by Chip Chapin
  
  $REMOTE_ADDR     = $_SERVER&#1111;'REMOTE_ADDR'];
  $HTTP_HOST       = $_SERVER&#1111;'HTTP_HOST'];
  $HTTP_USER_AGENT = $_SERVER&#1111;'HTTP_USER_AGENT'];
  
  $clogfile = "/tmp/cookielog$REMOTE_ADDR.txt"; // Use a unique logfile for each run.
  $ctestfile = basename($PHP_SELF);
  $clogcount = 5; // number of fields in cookielog
  
function cookie_log( $tnum, $tresult ) &#123; 
  global $REMOTE_ADDR;
  global $HTTP_USER_AGENT;
  global $HTTP_HOST;
  global $clogfile;
  $fd = fopen($clogfile, "a+");
  fwrite($fd,"$HTTP_HOST, $REMOTE_ADDR, $HTTP_USER_AGENT, $tnum, $tresult\n");
  fclose($fd);
&#125; // cookielog

function clearcookies()
&#123;
  global $ctestfile;
  //
  //if(rand(0,10) != 5) &#123; 
  //  header("Location: index2.php");
  //&#125;
  setcookie("testcookie1");
  setcookie("testcookie2","",0);
  setcookie("testcookie3","",0,"/");
  setcookie("testcookie4");
  setcookie("testcookie5","",0);
  //setcookie("testcookie6","",0);
  setcookie("testcookie7","",0,"/","$HTTP_HOST");
  //setcookie("testcookie8","",0,"/","$HTTP_HOST");
  setcookie("testcookie9");
  setcookie("testcookie10","",0);
  setcookie("testcookie11","",0,"/","$HTTP_HOST");
  setcookie("testcookie12");
  setcookie("testcookie13","",0,"/","$HTTP_HOST");
&#125; // clearcookies

if (isset($_GET&#1111;'source'])) &#123;
  echo "<html><head><title>Source Code for $ctestfile</title></head>\n";
  echo "<body><h1>Source Code for $ctestfile</h1>\n";
  highlight_file($_SERVER&#1111;'DOCUMENT_ROOT'] . $_SERVER&#1111;'PHP_SELF']);
  echo "</body></html>\n";
  exit;
&#125;

if(!isset($_GET&#1111;'mode'])) &#123; 
  // Clear all cookies.  Do this at start, and again when we're finished.
  clearcookies();
  if (!isset($_GET&#1111;'test'])) &#123;
    header("Location: $ctestfile?mode=1");
  &#125;
  exit;
&#125;

if($_GET&#1111;'mode'] == 1) &#123; 
  // Try setting all the cookies
  $time = mktime()+600;
  $date = gmstrftime("%A, %d-%b-%Y %H:%M:%S", (mktime()+6400));

  header("Set-Cookie: testcookie1=present;");
  header("Set-Cookie: testcookie2=present; expires=$date");
  header("Set-Cookie: testcookie3=present; expires=$date; path=/");
  header("Set-Cookie: testcookie13=present; path=/; domain=$HTTP_HOST; expires=".gmstrftime("%A, %d-%b-%Y %H:%M:%S GMT",time()+9600));
  setcookie("testcookie4", "present");
  setcookie("testcookie5", "present", (time()+6400));
  // setcookie("testcookie6", "present", "(time()+6400)");
  setcookie("testcookie7", "present", (time()+6400), "/", "$HTTP_HOST");
  // setcookie("testcookie8", "present", "(time()+6400)", "/", "$HTTP_HOST");

  print "<meta http-equiv="Set-Cookie" content="testcookie9=present">\n";
  print "<meta http-equiv="Set-Cookie" content="testcookie10=present; expires=$date">\n";
  print "<meta http-equiv="Set-Cookie" content="testcookie11=present; expires=$date; path=/">\n";

  print "<script>document.cookie = 'testcookie12' + '=' + 'present';</script>\n";

  sleep(1);
  if (!isset($_GET&#1111;'test'])) &#123;
    print "<meta http-equiv=refresh content="0;URL=$ctestfile?mode=2">\n";
  &#125;
  exit;
&#125;

if($_GET&#1111;'mode'] == 2) &#123; 
  // Check and log results
  if($_COOKIE &#1111;'testcookie1'] != "present")
  if($_COOKIE &#1111;'testcookie2'] != "present")
  if($_COOKIE &#1111;'testcookie3'] != "present")
  if($_COOKIE &#1111;'testcookie4'] != "present")
  if($_COOKIE &#1111;'testcookie5'] != "present")
  //if($_COOKIE &#1111;'testcookie6'] != "present")
  if($_COOKIE &#1111;'testcookie7'] != "present")
  //if($_COOKIE &#1111;'testcookie8'] != "present")
  if($_COOKIE &#1111;'testcookie9'] != "present")
  if($_COOKIE &#1111;'testcookie10'] != "present")
  if($_COOKIE &#1111;'testcookie11'] != "present")
  if($_COOKIE &#1111;'testcookie13'] != "present") 
  if($_COOKIE &#1111;'testcookie12'] != "present") &#123; 
    cookie_log(0, "Cookies Disabled");    
          if (!isset($_GET&#1111;'test'])) &#123;
      header("Location: $ctestfile?mode=3");    
    &#125;
    exit;
  &#125;
 
  if($_COOKIE &#1111;'testcookie1'] == "present") &#123; cookie_log(1,"OK"); &#125;
  else   &#123; cookie_log(1,"Fail"); &#125;
  if($_COOKIE &#1111;'testcookie2'] == "present") &#123; cookie_log(2, "OK"); &#125;
  else    &#123; cookie_log(2, "Fail"); &#125;
  if($_COOKIE &#1111;'testcookie3'] == "present") &#123; cookie_log(3, "OK"); &#125;
  else    &#123; cookie_log(3, "Fail"); &#125;
  if($_COOKIE &#1111;'testcookie4'] == "present") &#123; cookie_log(4, "OK"); &#125;
  else    &#123; cookie_log(4, "Fail"); &#125;
  if($_COOKIE &#1111;'testcookie5'] == "present") &#123; cookie_log(5, "OK"); &#125;
  else    &#123; cookie_log(5, "Fail"); &#125;
  //if($_COOKIE &#1111;'testcookie6'] == "present") &#123; cookie_log(6, "OK"); &#125;
  //else    &#123; cookie_log(6, "Fail"); &#125;
  if($_COOKIE &#1111;'testcookie7'] == "present") &#123; cookie_log(7, "OK"); &#125;
  else    &#123; cookie_log(7, "Fail"); &#125;
  //if($_COOKIE &#1111;'testcookie8'] == "present") &#123; cookie_log(8, "OK"); &#125;
  //else    &#123; cookie_log(8, "Fail"); &#125; 
  if($_COOKIE &#1111;'testcookie9'] == "present") &#123; cookie_log(9, "OK"); &#125;
  else    &#123; cookie_log(9, "Fail"); &#125;
  if($_COOKIE &#1111;'testcookie10'] == "present") &#123; cookie_log(10, "OK"); &#125;
  else    &#123; cookie_log(10, "Fail"); &#125;
  if($_COOKIE &#1111;'testcookie11'] == "present") &#123; cookie_log(11, "OK"); &#125;
  else    &#123; cookie_log(11, "Fail"); &#125;
  if($_COOKIE &#1111;'testcookie12'] == "present") &#123; cookie_log(12, "OK"); &#125;
  else    &#123; cookie_log(12, "Fail"); &#125;
  if($_COOKIE &#1111;'testcookie13'] == "present") &#123; cookie_log(13, "OK"); &#125;
  else    &#123; cookie_log(13, "Fail"); &#125;
  
  // Now clean up the cookies
  clearcookies();
  
  if (!isset($_GET&#1111;'test'])) &#123;
    header("Location: $ctestfile?mode=3");
  &#125;
  exit;
&#125;

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- InstanceBegin template="/Templates/stdlayout01.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Chip's PHP Cookie Test</title>
<!-- InstanceEndEditable --> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<?php
  // Include dynamic style sheet.
  echo '<style type="text/css">'."\n  <!--\n";
  if (file_exists( 'ccSiteStyle.css.php' )) &#123;
    include_once( 'ccSiteStyle.css.php' );
  &#125; 
  else &#123;
    include_once( $_SERVER&#1111;'DOCUMENT_ROOT'] . '/ccSiteStyle.css.php' );
  &#125;
  echo "\n  -->\n</style>\n";
  if (false) &#123;
?>
<link href="/ccSiteStyle.css" rel="stylesheet" type="text/css">
<?php &#125; ?>
<script type="text/javascript" language="JavaScript">
  <!--
  // Bust my page out of any frames
  if (top != self) top.location.href = location.href;
  -->
</script>
</head>

<body bgcolor="#FFDBB7">
<!-- InstanceBeginEditable name="MainTitle" --> 
<h1 style="text-align:left">PHP Cookie Test -- Client Results</h1>
<!-- InstanceEndEditable --> <!-- InstanceBeginEditable name="MainBody" --> 
<h3>A Tool For Testing Your Browser's Cookie Quotient By <a href="http://www.chipchapin.com/" target="_blank">Chip 
  Chapin</a></h3>
<table summary="" width="640">
  <tr> 
    <td width="611" align="left"><a href="../">Chip's Home</a> > <a href="./">WebTools</a> 
      > <strong>phpCookieTest</strong></td>
    <td width="4" align="center">&nbsp;</td>
    <td width="9" align="RIGHT">&nbsp;</td>
  </tr>
  <tr> 
    <td colspan="3"> <hr> </td>
  </tr>
</table>
<table width="240" border="0" align="right" cellpadding="2" cellspacing="4">
  <tr> 
    <td> <p><strong>Page Revision History</strong></p>
      <ul>
        <li><span class="smalltext">2002-11-08 Facelift.</span></li>
        <li><span class="smalltext">2002-08-13 Updated for PHP 4.2.</span></li>
        <li><span class="smalltext">2001-11-29 Initial Release (1.0)</span></li>
      </ul>
      <p><strong>Contents</strong></p>
      <ul>
        <li> <a href="#results"><strong>YOUR Results</strong></a></li>
        <li><a href="#gotfailures">Got Failures?</a></li>
        <li><a href="#moreinfo">References: Cookie Info</a> </li>
        <li><a href="#sourcecode">Got Source?</a></li>
      </ul> </td>
  </tr>
</table>
<h2>What's Here?</h2>
<p>A painless evaluation of your browser's ability to set and return HTTP cookies.</p>
<h2>&#8220;The Great PHP Cookie Test&#8221;</h2>
<p>These tests and the idea behind them are derived from "The Great PHP Cookie 
  Test" by Sean McLean at <a href="http://www.rnetwork.tv/cookietest/" target="_blank">http://www.rnetwork.tv/cookietest/</a>. 
  I have basically just taken Sean's code and repackaged it as a client test to 
  help diagnose cookie problems. Sean used it more to gather statistics on browser 
  behavior.</p>
<blockquote> 
  <p> Your Client's Remote Address: <?php echo $REMOTE_ADDR; ?> <br>
    Your Browser's User Agent string: <?php echo $HTTP_USER_AGENT; ?> </p>
</blockquote>
<p>There are 13 tested cookie setting procedures, as they appear in Sean McLean's 
  original tests as of 11/29/01. For consistency, I've kept all the same tests, 
  however, <i>tests 6 and 8 incorrectly pass a string instead of the expiration 
  time. This is essentially the same as passing an expiration time of zero.</i> 
  But many PHP coders seem to do this, so Sean included them in the list. <strong>Update</strong>: 
  <em>Tighter parameter checking in PHP 4.2.x now catches these errors, so tests 
  6 and 8 were removed August 13, 2002.</em> 
<blockquote> 
  <p> <i>$date = gmstrftime("%A, %d-%b-%Y %H:%M:%S",(mktime()+6400) );</i><br>
    <i>$HTTP_HOST = <?php echo ""$HTTP_HOST""; ?>; // our server's hostname</i></p>
</blockquote>
<pre>
1:    header("Set-Cookie: testcookie1=present;");
2:    header("Set-Cookie: testcookie2=present; expires=$date");
3:    header("Set-Cookie: testcookie3=present; expires=$date; path=/");
4:    setcookie("testcookie4", "present");
5:    setcookie("testcookie5", "present", (time()+6400));
  Removed -- 6:    setcookie("testcookie6", "present", "(time()+6400)");
7:    setcookie("testcookie7", "present", (time()+6400), "/", "$HTTP_HOST");
  Removed -- 8:    setcookie("testcookie8", "present", "(time()+6400)", "/", "$HTTP_HOST");
9:    print "<meta http-equiv="Set-Cookie" content="testcookie9=present">\n";
10:   print "<meta http-equiv="Set-Cookie" content="testcookie10=present; expires=$date">\n";
11:   print "<meta http-equiv="Set-Cookie" content="testcookie11=present; expires=$date; path=/">\n";
12:   print "<script>document.cookie = 'testcookie12' + '=' + 'present';</script>\n";
13:   header("Set-Cookie: testcookie13=present; path=/; domain=$HTTP_HOST; expires=".gmstrftime("%A, %d-%b-%Y %H:%M:%S GMT",time()+9600));
</pre>
<h2><a name="results"></a>YOUR Results</h2>
<p>Below are the results for <strong>your</strong> current address and browser:</p>
<blockquote>
<pre>
<?php
  $fp=@fopen($clogfile, "r");
  if (!$fp) &#123;
    echo "No results are currently available.  Please rerun the tests.\n";
  &#125;
  else &#123;
    $lnum = 0;
    while ($line = fgetcsv ($fp, 1000, ",")) &#123;
      $lcount++;
      if (count($line) != $clogcount) &#123;
        echo "Bad field count, line $lcount: ". implode(", ", $line)."\n";
        continue;
      &#125;
      if ($line&#1111;1] == $REMOTE_ADDR && $line&#1111;2] == $HTTP_USER_AGENT) &#123;
        //echo implode(", ", $line)."\n";
        echo "Method " . $line&#1111;3] . ": " . $line&#1111;4] . "\n";
      &#125;
    &#125;
    // echo "\nProcessed $lcount test result lines.<br>\n";
    fclose($fp);
    unlink($clogfile);  // In this implementation, I use a unique logfile for each run.
  &#125;
?>
</pre>
</blockquote>
<p>Ideally, all methods should test 'OK'. To re-run the tests, <strong><a href="<?php echo $ctestfile; ?>">click 
  here</a></strong>.</p>
<h2><a name="gotfailures"></a>Got Failures? <b>Check Your System Date</b> ...</h2>
<p>Your client's system date should be close to "correct". Please compare 
  it to our server's system date. If your system date is significantly wrong, 
  then you will have problems with cookie expiration times. In fact, when I developed 
  this test page to help me resolve a client problem, it turned out that the client's 
  date was set <i>8 years in the future</i> (2009 instead of 2001) so every cookie 
  with an expiration date failed.</p>
<blockquote> 
  <p>Our Server's System Date: <?php echo gmstrftime ("%b %d %Y %H:%M:%S GMT/UTC", time() ); ?> 
    <br>
    Your Client's System Date: 
    <script language="JavaScript">
      <!--
      today = new Date();
      document.writeln(today.toGMTString());
      // -->
    </script>
  <noscript>
  (JavaScript required) 
  </noscript></p>
  </blockquote>
<h2><a name="moreinfo"></a>Want More Info About Cookies?</h2>
<p>For detailed (though conflicting) information about setting cookies in PHP 
  see the <a href="http://download.php.net/manual/en/function.setcookie.php" target="_blank">annotated 
  online PHP Manual</a>. Here are some other useful cookie resources:</p>
<dl>
  <dl>
    <dt>Netscape Spec: <a href="http://www.netscape.com/newsref/std/cookie_spec.html" target="_blank">HTTP 
      Cookies</a></dt>
    <dd><i>Netscape's preliminary specification, "Persistent Client State: 
      HTTP Cookies" provides a simple and readable introduction to the client-server 
      protocol required by cookies.</i></dd>
    <dt><a href="ftp://ftp.isi.edu/in-notes/rfc2965.txt" target="_blank">RFC 2965 
      -- "HTTP State Management Mechanism"</a></dt>
    <dd><i>The October 2000 internet standard for HTTP cookies.</i></dd>
    <dt>Microsoft's <a href="http://support.microsoft.com/default.aspx?scid=kb;EN-US;q260971&GSSNB=1" target="_blank">Description 
      of Cookies</a></dt>
    <dd><i>An overview document with links to various resources, including relevant 
      Microsoft support issues and external standards.</i></dd>
  </dl>
</dl>
<h2><a name="sourcecode"></a>Source Code Here...</h2>
<p>To see the source code for this page (including all the tests) <a href="<?php echo $ctestfile; ?>?source=true">click 
  here</a>.</p>
<!-- InstanceEndEditable -->. 
<table border="0" cellspacing="2" cellpadding="2" width=
      "100%">
  <tr> 
    <td colspan="2"> <hr> </td>
  </tr>
  <tr> 
    <td colspan="2">
  <table width="100%" cellspacing="0" summary="">
        <tr> 
      <td width="88"> <a href=
              "http://validator.w3.org/check?uri=<?php echo $_SERVER&#1111;'HTTP_HOST'].$PHP_SELF; ?>"> 
        <img src="/images/vh40.png" alt="Valid HTML 4.0!"
              border="0" height="31" width="88"></a> </td>
      <td width="88"> <a href=
              "http://jigsaw.w3.org/css-validator/validator?uri=<?php echo $_SERVER&#1111;'HTTP_HOST'].$PHP_SELF; ?>"> 
        <img border="0" width="88" height="31" src=
              "/images/vcss.gif" alt="Valid CSS!"></a> </td>
          <td align="RIGHT"> <a href="http://www.chipchapin.com/" target="_blank">Chip's 
            Home Page</a> </td>
    </tr>
  </table>
    </td>
  </tr>
  <tr> 
    <td colspan="2"> <hr> </td>
  </tr>
  <tr> 
    <td valign="top"> <div class="footer"> Copyright &copy; 2002 <a href=
              "mailto:cchapin@chipchapin.com?subject=<?php echo $pgEmailSubj ?>">Chip 
        Chapin</a>,  <a href=
              "http://www.chipchapin.com/" target="_blank">http://www.chipchapin.com</a> 
      </div></td>
    <td align="right"> <div class="date"> 
        Updated 
<?php
  echo strftime("%A %B %d, %Y %T %Z", getlastmod());
  if (false) &#123;
?>
        <!-- #BeginDate format:Am1 -->November 8, 2002<!-- #EndDate -->
<? &#125; ?>
      </div></td>
  </tr>
</table>
<p>&nbsp;</p>
</body>
<!-- InstanceEnd --></html>

Posted: Thu Jun 19, 2003 8:52 am
by igoy
Okay .. .I Think Logically I'm understanding what is happening. ..
but let me test this on my XP box. . .will let u know something concrete soon... :) hope on

Posted: Wed Jun 25, 2003 4:20 am
by Tokee
I connected my PC with the www and run cookietest.php from the online source. And all was fine. So even with TCP/IP active, the script on my local apache did not work correctly.

I guess, my problem is a wrong setting in my apache-/php-ini..

Thanks anyway!

RegisterGlobals

Posted: Tue Jul 01, 2003 2:24 pm
by tal3323
I'm not sure but if you don't have RegisterGlobals set to on, that could be the problem.