Page 1 of 1

Cookie redirect won't help. Any suggestions?

Posted: Thu Aug 11, 2005 8:43 am
by pilau
Take a look at the code below. What it does is to check if a cookie called "resoRedirect" exists. If not, it will let the user set it (the cookie is being set I checked on my own.) Otherwise, it needs to redirect to a page according to the cookie content.
If the cookie exists, the page is not supposed to show anything, onlt redirect the user to the next page.
Everything works perfectly, except for when the page is loaded after the cookie has been set, it won't redirect me unless I refresh the page.
Maybe you know why?

Code: Select all

<?php

 if (!isset($_COOKIE['resoRedirect'])) {

$html1 = <<<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'>

EOT;
$html2 = <<<EOT

<SCRIPT LANGUAGE='JavaScript'>

function putCookie(RediReso)
{
document.cookie="resoRedirect"+"="+RediReso+"; expires=Monday, 04-Apr-2010 05:00:00 GMT";
}

</SCRIPT>

EOT;

$html3 = <<<EOT

 </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(800)'>800x600</a>
 <a href="index_1024.html" onClick='putCookie(1024)'>1024x768</a>
 <a href="index_1280.html" onClick='putCookie(1280)'>1280x960+</a>

	<br>&nbsp;
	<br>&nbsp;

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

</center>
 </body>
</html>
EOT;
print $html1."<br>";
print $html2."<br>";
print $html3;
}

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['resoRedirect'].".html";
relativeRedirect($redirect_file);

 }
?>

Posted: Thu Aug 11, 2005 8:51 am
by feyd
looks fine to me... (other than the <br> you place inside the header)

Posted: Thu Aug 11, 2005 9:07 am
by pilau
Where?

Posted: Thu Aug 11, 2005 9:23 am
by theda
By any chance are you running an old install of PHP? Maybe your host has Superglobals off...

Posted: Thu Aug 11, 2005 9:24 am
by pilau
Nope.
It's php 4.4.0.

Posted: Thu Aug 11, 2005 9:46 am
by theda
Oh I know, it's because you terminated the header before the information was inputted.

Code: Select all

header("Location: " . $http . $_SERVER['SERVER_NAME'] . dirname($_SERVER['PHP_SELF']) . $relative_url . "");
should work for you

Basically, it would output header("Location: ") and then the other stuff you had in it would be dropped as it isn't inside a quotation mark.

Posted: Thu Aug 11, 2005 9:53 am
by pilau
The redirection function works, (maybe now it'll work better?)
But the problem is that the script won't get inside the else {} code,
It always thinks that the cookie is not set!

Posted: Thu Aug 11, 2005 10:24 am
by theda
Remove the ! from !isset. Try that once. I'm not entire clear as to what the ! does in that place, but I can assume it probably means "if XXX is not set"

Posted: Thu Aug 11, 2005 10:39 am
by pilau
Yes it does.
But it would turn the script up-side-down, making it do the "else" part instaed of the "if" part an vice versa.

Posted: Thu Aug 11, 2005 5:00 pm
by theda
Although I'm not quite sure about this, but could it be that it won't work as you have header() in a function declaration? Is there another way to do this, where you aren't using header inside the function?