Sure.
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ї'REMOTE_ADDR'];
$HTTP_HOST = $_SERVERї'HTTP_HOST'];
$HTTP_USER_AGENT = $_SERVERї'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 ) {
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);
} // cookielog
function clearcookies()
{
global $ctestfile;
//
//if(rand(0,10) != 5) {
// header("Location: index2.php");
//}
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");
} // clearcookies
if (isset($_GETї'source'])) {
echo "<html><head><title>Source Code for $ctestfile</title></head>\n";
echo "<body><h1>Source Code for $ctestfile</h1>\n";
highlight_file($_SERVERї'DOCUMENT_ROOT'] . $_SERVERї'PHP_SELF']);
echo "</body></html>\n";
exit;
}
if(!isset($_GETї'mode'])) {
// Clear all cookies. Do this at start, and again when we're finished.
clearcookies();
if (!isset($_GETї'test'])) {
header("Location: $ctestfile?mode=1");
}
exit;
}
if($_GETї'mode'] == 1) {
// 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ї'test'])) {
print "<meta http-equiv=refresh content="0;URL=$ctestfile?mode=2">\n";
}
exit;
}
if($_GETї'mode'] == 2) {
// Check and log results
if($_COOKIE ї'testcookie1'] != "present")
if($_COOKIE ї'testcookie2'] != "present")
if($_COOKIE ї'testcookie3'] != "present")
if($_COOKIE ї'testcookie4'] != "present")
if($_COOKIE ї'testcookie5'] != "present")
//if($_COOKIE ї'testcookie6'] != "present")
if($_COOKIE ї'testcookie7'] != "present")
//if($_COOKIE ї'testcookie8'] != "present")
if($_COOKIE ї'testcookie9'] != "present")
if($_COOKIE ї'testcookie10'] != "present")
if($_COOKIE ї'testcookie11'] != "present")
if($_COOKIE ї'testcookie13'] != "present")
if($_COOKIE ї'testcookie12'] != "present") {
cookie_log(0, "Cookies Disabled");
if (!isset($_GETї'test'])) {
header("Location: $ctestfile?mode=3");
}
exit;
}
if($_COOKIE ї'testcookie1'] == "present") { cookie_log(1,"OK"); }
else { cookie_log(1,"Fail"); }
if($_COOKIE ї'testcookie2'] == "present") { cookie_log(2, "OK"); }
else { cookie_log(2, "Fail"); }
if($_COOKIE ї'testcookie3'] == "present") { cookie_log(3, "OK"); }
else { cookie_log(3, "Fail"); }
if($_COOKIE ї'testcookie4'] == "present") { cookie_log(4, "OK"); }
else { cookie_log(4, "Fail"); }
if($_COOKIE ї'testcookie5'] == "present") { cookie_log(5, "OK"); }
else { cookie_log(5, "Fail"); }
//if($_COOKIE ї'testcookie6'] == "present") { cookie_log(6, "OK"); }
//else { cookie_log(6, "Fail"); }
if($_COOKIE ї'testcookie7'] == "present") { cookie_log(7, "OK"); }
else { cookie_log(7, "Fail"); }
//if($_COOKIE ї'testcookie8'] == "present") { cookie_log(8, "OK"); }
//else { cookie_log(8, "Fail"); }
if($_COOKIE ї'testcookie9'] == "present") { cookie_log(9, "OK"); }
else { cookie_log(9, "Fail"); }
if($_COOKIE ї'testcookie10'] == "present") { cookie_log(10, "OK"); }
else { cookie_log(10, "Fail"); }
if($_COOKIE ї'testcookie11'] == "present") { cookie_log(11, "OK"); }
else { cookie_log(11, "Fail"); }
if($_COOKIE ї'testcookie12'] == "present") { cookie_log(12, "OK"); }
else { cookie_log(12, "Fail"); }
if($_COOKIE ї'testcookie13'] == "present") { cookie_log(13, "OK"); }
else { cookie_log(13, "Fail"); }
// Now clean up the cookies
clearcookies();
if (!isset($_GETї'test'])) {
header("Location: $ctestfile?mode=3");
}
exit;
}
?>
<!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' )) {
include_once( 'ccSiteStyle.css.php' );
}
else {
include_once( $_SERVERї'DOCUMENT_ROOT'] . '/ccSiteStyle.css.php' );
}
echo "\n -->\n</style>\n";
if (false) {
?>
<link href="/ccSiteStyle.css" rel="stylesheet" type="text/css">
<?php } ?>
<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"> </td>
<td width="9" align="RIGHT"> </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>“The Great PHP Cookie Test”</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) {
echo "No results are currently available. Please rerun the tests.\n";
}
else {
$lnum = 0;
while ($line = fgetcsv ($fp, 1000, ",")) {
$lcount++;
if (count($line) != $clogcount) {
echo "Bad field count, line $lcount: ". implode(", ", $line)."\n";
continue;
}
if ($lineї1] == $REMOTE_ADDR && $lineї2] == $HTTP_USER_AGENT) {
//echo implode(", ", $line)."\n";
echo "Method " . $lineї3] . ": " . $lineї4] . "\n";
}
}
// echo "\nProcessed $lcount test result lines.<br>\n";
fclose($fp);
unlink($clogfile); // In this implementation, I use a unique logfile for each run.
}
?>
</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ї'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ї'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 © 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) {
?>
<!-- #BeginDate format:Am1 -->November 8, 2002<!-- #EndDate -->
<? } ?>
</div></td>
</tr>
</table>
<p> </p>
</body>
<!-- InstanceEnd --></html>