XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).
Moderator: General Moderators
pilau
Forum Regular
Posts: 594 Joined: Sat Jul 09, 2005 10:22 am
Location: Israel
Post
by pilau » Thu Aug 11, 2005 8:43 am
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>
<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(800)'>800x600</a>
<a href="index_1024.html" onClick='putCookie(1024)'>1024x768</a>
<a href="index_1280.html" onClick='putCookie(1280)'>1280x960+</a>
<br>
<br>
</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);
}
?>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Aug 11, 2005 8:51 am
looks fine to me... (other than the <br> you place inside the header)
pilau
Forum Regular
Posts: 594 Joined: Sat Jul 09, 2005 10:22 am
Location: Israel
Post
by pilau » Thu Aug 11, 2005 9:07 am
Where?
theda
Forum Contributor
Posts: 332 Joined: Sat Feb 19, 2005 8:35 am
Location: USA
Post
by theda » Thu Aug 11, 2005 9:23 am
By any chance are you running an old install of PHP? Maybe your host has Superglobals off...
pilau
Forum Regular
Posts: 594 Joined: Sat Jul 09, 2005 10:22 am
Location: Israel
Post
by pilau » Thu Aug 11, 2005 9:24 am
Nope.
It's php 4.4.0.
theda
Forum Contributor
Posts: 332 Joined: Sat Feb 19, 2005 8:35 am
Location: USA
Post
by theda » Thu Aug 11, 2005 9:46 am
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.
pilau
Forum Regular
Posts: 594 Joined: Sat Jul 09, 2005 10:22 am
Location: Israel
Post
by pilau » Thu Aug 11, 2005 9:53 am
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!
theda
Forum Contributor
Posts: 332 Joined: Sat Feb 19, 2005 8:35 am
Location: USA
Post
by theda » Thu Aug 11, 2005 10:24 am
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"
pilau
Forum Regular
Posts: 594 Joined: Sat Jul 09, 2005 10:22 am
Location: Israel
Post
by pilau » Thu Aug 11, 2005 10:39 am
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.
theda
Forum Contributor
Posts: 332 Joined: Sat Feb 19, 2005 8:35 am
Location: USA
Post
by theda » Thu Aug 11, 2005 5:00 pm
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?