Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

[web] Page redirecting in PHP not working in IE

Started by jon723 Aug 19, 2008 at 12:47 PM 5 replies 5.3k views
Original Post
jon723
jon723
Hello everyone, I have a function I'm using to redirect the user to the previous page after they submit some data to a form. When I'm testing in firefox and safari the page is redirected like it should but when testing in IE it attempts to redirect but ends up staying on the same page. This is the PHP I'm using to redirect: function pageRedirect() { if(!empty($_SERVER['HTTP_REFERER'])) header('Refresh: 3,URL="'.$_SERVER['HTTP_REFERER'].'"'); else echo ''; } else { echo "Form error"; } } Does anyone know how I can get this to work in IE?? Jon
www.lefthandinteractive.net
UltimaX
UltimaX
You have to be careful using the header() though because you could get an error if any output has been sent to the browser (headers already sent error to be more specific). Here is the function that I use to redirect:

function APUTRedirect($sURL){    if(!headers_sent())    {        header('Location: '.$sURL);    }    else    {        echo("<script type='text/&#106avascript'>");        echo("  window.location.href='".$sURL."'");        echo("</script>");        echo("<noscript>");        echo("  <meta http-equiv='refresh' content='0;url=".$sURL."' />");        echo("</noscript>");    }}


That function has worked in all browsers that I use, it avoids the headers already sent error, and it will work with &#106avascript disabled.
jon723
jon723
Hey thanks for you replies. I tried using the function in place of the one I had and it still doesn't work in IE. Also, I would like to refrain from using absolute URL's since the site I'm working on is still in development and the absolute URL isn't the actual URL I'm using while testing.

www.lefthandinteractive.net
UltimaX
UltimaX
I would check the rest of the code then. I have used this in IE6/IE7 with no problems at all. I would check your error logs to see what the actual problem is. Since you are using PHP I would turn the error handling on to E_ALL I belive it is.

Did you check your orig. code? If it goes to the same page then I assume that is the home page and the referrer is empty. The reason I say that is you have history.go(-1) and it is the home page (or you are using AJAX and there is no history registered on the stack) then it will just reload the same page.

Either way, I assure you it is not the redirect code causing this.
jon723
jon723
Ok, frustration is setting in. I've replaced the code I was using with the function you suggested. While the redirect "is" working, the messages I print to the screen aren't being displayed. I added a delay to the refresh call but stuff is just not cooperating. Hopefully a second pair of eyes can see what I'm doing wrong.


Jon

[SOURCE]<?phpfunction pageRedirect(){    if(!headers_sent())    {        header('Location: '.$_SERVER['HTTP_REFERER']);		exit;    }	else	{	 echo("<script type='text/javascript'>");        echo("  window.location.href='".$_SERVER['HTTP_REFERER']."'");        echo("</script>");        echo("<noscript>");        echo("	<meta http-equiv='refresh' content='3;url=".$_SERVER['HTTP_REFERER']."' />");        echo("</noscript>");	  	exit;	}}?><?phpif(isset($_POST['submit'])) {	$subject = "Contact Form";	$name_field = $_POST['name'];	$number_field = $_POST['number'];	 	$body = "From: $name_field\n Phone#: $number_field";	if(($name_field == "") || ($number_field == ""))	{		echo 'You must fill out both fields' . "
"; pageRedirect(); } else { echo "Form has been submitted."; pageRedirect(); } } ?>[/SOURCE]
[/source]
www.lefthandinteractive.net
Arkantis
Arkantis
If you really need a delay they the easiest way to do it I know if is the meta refresh..

function pageRedirect($delay = 3)
{
echo(" ");

}

Try that function, then use tweak around the $delay parameter so they get ample time to read your message.

The problem with the other function for what your doing is if the headers are NOT sent the page will reload immediately. Using the meta it will allow you to control a delay easily.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.