# [web] Page redirecting in PHP not working in IE

This topic is 3474 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 '<script type="text/javascript">setTimeout("history.go(-1);",3000);</script>'; } else { echo "Form error"; } } Does anyone know how I can get this to work in IE?? Jon

##### Share on other sites
Try:

header('Location: http://www.example.org');

##### Share on other sites
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/javascript'>");        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 javascript disabled.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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' . "<br />";				pageRedirect();		}	else	{		echo "Form has been submitted.";				pageRedirect();	}	} ?>[/SOURCE]
[/source]

##### Share on other sites
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(" <meta http-equiv='refresh' content='$delay;url=".$_SERVER['HTTP_REFERER']."' />"); } 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.