Create a cookie without using a form

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Create a cookie without using a form

Post by pilau »

Is there a way I can set a cookie by pressing a link and not by submitting a form?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

you can use javascript to set a cookie. or you can use php to set a cookie without a form submishion.

so your answer is "yes"
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

I tried to do it but it didn't work.
I mean, the cookie is set, but I dont get redirected to the page.
Maybe you can help me out:

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>&nbsp;
	<br>&nbsp;
	<br>&nbsp;
	<br>&nbsp;
	<br>&nbsp;
	<br>&nbsp;
	<br>&nbsp;
	<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>&nbsp;
	<br>&nbsp;

   </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);
 }
?>
Last edited by pilau on Wed Aug 10, 2005 4:03 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

if(document.cookie != document.cookie)
If the document's cookie isn't equal to the document's cookie ? ... wait for it...... wait for it... :?
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Well, I copied it out of this website, and didn't think of removing it... :oops:
The shame.

[EDIT]
Anyway, it works, I just found out the problem. Thanks.
[/EDIT]

[EDIT]
Wait! it's not working... :( Waaaaa!
The cookie is not being created! Why on earth?
[/EDIT]

Here's the code:

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>&nbsp;
	<br>&nbsp;
	<br>&nbsp;
	<br>&nbsp;
	<br>&nbsp;
	<br>&nbsp;
	<br>&nbsp;
	<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>&nbsp;
	<br>&nbsp;

   </td></tr>
 </table>

</center>
 </body>
</html>
EOT;
print $html;
}

else {



 $redirect_file="index_".$_COOKIE['Resolution'].".html";
relativeRedirect($redirect_file);
 }
?>
Post Reply