See topic.
My problem is that I already have output before I get to where I need to redirect, and a meta tag refresh isn't really the answer. I'm basically trying to dump the user to an error page if certain conditions aren't met. Any help is appreciated.
Redirecting in PHP without using header?
Started by _*Calamari_*, Feb 19 2004 07:05 PM
7 replies to this topic
#1 _*Calamari_*
Posted 19 February 2004 - 07:05 PM
#2
Posted 20 February 2004 - 09:17 AM
Calamari, on Feb 19 2004, 07:05 PM, said:
See topic.
My problem is that I already have output before I get to where I need to redirect, and a meta tag refresh isn't really the answer. I'm basically trying to dump the user to an error page if certain conditions aren't met. Any help is appreciated.
My problem is that I already have output before I get to where I need to redirect, and a meta tag refresh isn't really the answer. I'm basically trying to dump the user to an error page if certain conditions aren't met. Any help is appreciated.
Hopefully Shao will drop by and have a look at your request as he is the resident PHP guru, but after a quick search, I've found some info for you that may or may not be useful. You mentioned the meta tag refresh not being an answer, but it would seem in all the examples I've found, they use it.
Like this one:
if ($success)
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';
exit;
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=retry.php">';
exit;
}
This causes the browser to open the new page after 0 seconds, i.e immediately.
I've also found refrences to using ob_start(),
Found here and several other places: http://www.webmaster...orum88/2662.htm
Example:
ob_start();
//script
header("Location:file.php");
ob_end_flush();
Is this for a form? You could also use a php script like this one:
On the form...
echo '<input type="hidden" name="back" value="'.$_SERVER['HTTP_REFERER'].'">';
The form itself....
<?php
// Do something with the $_POST variables, insert into db etc.
// then...
header("Location: ".$_SERVER['HTTP_SERVER'].$_POST['back']."");
?>
Any of that help you? Maybe Shao can add something to this discussion if I've missed the mark.
Good luck,
Faken
#3 _*Shao_*
Posted 20 February 2004 - 10:57 AM
Looks simple enough. Just think outside the php box and go with javascript. After the condition are not met add this little java script: <script>location.href='errorpage.html'</script>.
So after your main page loads but once you get to the above javascript it will trigger the browser to call errorpage.html and reloads with that page. Hope that's what your looking for.
Here's what I did for some of my php:
<?php
// show some stuffs
printf("<html><body>");
printf("Just some stuffs on the page...");
// do you condition tests
.....
if ($myConditions != $meetsExpectation)
{
printf("<script>location.href='errorpage.html'</script>");
}
else
{
// show other stuffs
}
?>
That should point you to the right area. Keep in mind that there are numerous other ways of doing this. Just pick the one that makes the most sense to you.
Shao
So after your main page loads but once you get to the above javascript it will trigger the browser to call errorpage.html and reloads with that page. Hope that's what your looking for.
Here's what I did for some of my php:
<?php
// show some stuffs
printf("<html><body>");
printf("Just some stuffs on the page...");
// do you condition tests
.....
if ($myConditions != $meetsExpectation)
{
printf("<script>location.href='errorpage.html'</script>");
}
else
{
// show other stuffs
}
?>
That should point you to the right area. Keep in mind that there are numerous other ways of doing this. Just pick the one that makes the most sense to you.
Shao
#4
Posted 24 July 2011 - 01:31 PM
Thanks Shao
7 years later and this is still the best help I found on redirect in php without header (location.... )
Thanks for keeping it up.
Here's what I did for some of my php:
<?php
// show some stuffs
printf("<html><body>");
printf("Just some stuffs on the page...");
// do you condition tests
.....
if ($myConditions != $meetsExpectation)
{
printf("<script>location.href='errorpage.html'</script>");
}
else
{
// show other stuffs
}
?>
7 years later and this is still the best help I found on redirect in php without header (location.... )
Thanks for keeping it up.
Shao, on 20 February 2004 - 10:57 AM, said:
Here's what I did for some of my php:
<?php
// show some stuffs
printf("<html><body>");
printf("Just some stuffs on the page...");
// do you condition tests
.....
if ($myConditions != $meetsExpectation)
{
printf("<script>location.href='errorpage.html'</script>");
}
else
{
// show other stuffs
}
?>
#5
Posted 08 August 2011 - 09:40 PM
I don't see how a javascript location redirect is better than a Meta refresh set at 0 seconds.
#6
Posted 08 August 2011 - 10:20 PM
Considering the javascript can be blocked, i would say it's worse. However, and i have no way of verifying this, but the JS redirect might be executed a fraction of a second sooner than the meta redirect due to not having to wait for most of the page to be download before being able to execute.
#7
Posted 03 January 2012 - 10:47 PM
Well, okay, I didn't think about javascript being blocked. Pretty much negates my initial reply.
#8
Posted 05 January 2012 - 03:09 PM
rc69, on 08 August 2011 - 10:20 PM, said:
Considering the javascript can be blocked, i would say it's worse. However, and i have no way of verifying this, but the JS redirect might be executed a fraction of a second sooner than the meta redirect due to not having to wait for most of the page to be download before being able to execute.
Nobody blocks Javascript anymore, Facebook, Twitter, YouTube, any site really nowadays all use Javascript so I don't agree. A nice alternative (since JS redirects and META redirects suck anyways) is to generate an AJAX call for your error page and replace the BODY with the results from the AJAX call. (I use jQuery to do my AJAX since I'm a big fat noob).
Really the better way to approach this from the PHP side would be to build up a simple class or function to handle your error messages, have your template file check for errors and then if they exist, echo them out in whatever page you are including.
Example, if I'm on a login page and I enter an invalid user/pass combo, I will be redirected back to the login page, but sitting on the page would be a red error message.
4 user(s) are reading this topic
0 members, 4 guests, 0 anonymous users
