Create a cookie without using a form
Posted: Wed Aug 10, 2005 3:18 pm
Is there a way I can set a cookie by pressing a link and not by submitting a form?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?php
if (!isset($_COOKIE['Resolution'])) {
$html = <<<EOT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="scripts/style.css">
<title>.:: The Official Cry Havoc Website ::.</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<SCRIPT LANGUAGE="JavaScript">
cookie_name = "Resolution";
function putCookie(resosize) {
if(document.cookie != document.cookie)
{index = document.cookie.indexOf(cookie_name);}
else
{ index = -1;}
if (index == -1)
{
document.cookie=cookie_name+"="+resosize+"; expires=Monday, 04-Apr-2010 05:00:00 GMT";
}
}
</SCRIPT>
</head>
<body style="background-image:url('images/int_bg.png');">
<center>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<table background="images/int_frame_bg.png" width="260">
<tr><td align="center">
<br><br>
Please choose your resolution:<br />
<a href="index_800.html" onClick="putCookie('800x600')">800x600</a>
<a href="index_1024.html" onClick="putCookie('1024x768')">1024x768</a>
<a href="index_1280.html" onClick="putCookie('1280x960')">1280x960+</a>
<br>
<br>
</td></tr>
</table>
</center>
</body>
</html>
EOT;
print $html;
}
else {
function relativeRedirect($relative_url) {
if($relative_url{0} != DIRECTORY_SEPARATOR)
{
$relative_url = DIRECTORY_SEPARATOR.$relative_url;
}
if($_SERVER['HTTPS'] == 'on')
{
$http = 'https://';
}
else {$http = 'http://';}
header("Location: " . $http . $_SERVER['SERVER_NAME'] . dirname($_SERVER['PHP_SELF']) . $relative_url);
exit;
}
$redirect_file="index_".$_COOKIE['Resolution']."html";
relativeRedirect($redirect_file);
}
?>Code: Select all
if(document.cookie != document.cookie)Code: Select all
<?php
function relativeRedirect($relative_url) {
if($relative_url{0} != DIRECTORY_SEPARATOR)
{
$relative_url = DIRECTORY_SEPARATOR.$relative_url;
}
if($_SERVER['HTTPS'] == 'on')
{
$http = 'https://';
}
else {$http = 'http://';}
header("Location: " . $http . $_SERVER['SERVER_NAME'] . dirname($_SERVER['PHP_SELF']) . $relative_url);
exit;
}
if (!isset($_COOKIE['Resolution'])) {
$html = <<<EOT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="scripts/style.css">
<title>.:: The Official Cry Havoc Website ::.</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<SCRIPT LANGUAGE="JavaScript">
cookie_name = "Resolution";
function putCookie(resosize) {
document.cookie=cookie_name+"="+resosize+"; expires=Monday, 04-Apr-2010 05:00:00 GMT";
}
</SCRIPT>
</head>
<body style="background-image:url('images/int_bg.png');">
<center>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<table background="images/int_frame_bg.png" width="260">
<tr><td align="center">
<br><br>
Please choose your resolution:<br />
<a href="index_800.html" onClick="putCookie('800x600')">800x600</a>
<a href="index_1024.html" onClick="putCookie('1024x768')">1024x768</a>
<a href="index_1280.html" onClick="putCookie('1280x960')">1280x960+</a>
<br>
<br>
</td></tr>
</table>
</center>
</body>
</html>
EOT;
print $html;
}
else {
$redirect_file="index_".$_COOKIE['Resolution'].".html";
relativeRedirect($redirect_file);
}
?>