How to redirect Page
Moderator: General Moderators
How to redirect Page
Hi All
I have a one page .here i get input
if the input is correct it should redirect to correct page otherwise it should go to another page.
Give me solution
Regards
Umapathy
I have a one page .here i get input
if the input is correct it should redirect to correct page otherwise it should go to another page.
Give me solution
Regards
Umapathy
Code: Select all
header();Code: Select all
<?PHP
ob_start(); //Incase you allready have some data outputed to the buffer, this will save you some errors
if ($Correct) {
header('LOCATION: http://forums.devnetwork.net/'); //If $Correct is true, it will take us here
} else {
header('LOCATION: http://www.google.com/'); //Otherwise it will take us here.
}
?>You could also do the same, but have more options on how to handle your input. Example:
Code: Select all
<?PHP
ob_start();
switch ($Correct) {
case TRUE:
header('LOCATION: http://forums.devnetwork.net/'); //If $Correct is true, it will take us here
break;
case FALSE:
header('LOCATION: http://www.google.com/'); //Otherwise it will take us here.
break;
default:
header('LOCATION: http://www.yahoo.com/'); //If the input does not look like our pre-defined expected values, it will go to this page
}
?>Hope this helps
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Make sure to follow header() with exit;
Code: Select all
<?php
if ($should_redirect)
{
header('Location: http://www.myserver.com/page1.php');
exit;
}
else
{
header('Location: http://www.myserver.com/page2.php');
exit;
}
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
God catch. As per the manual:
Code to standard and use full URI's. Thanks Feyd.Note: HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:
That is an extension: http://us3.php.net/manual/en/ref.http.phpPapiSolo wrote:Could this also be done using http_redirect(); ?
Thanks for the quick reply... i have a similar problem and was trying to achieve this with
I will try using header instead.
I have just one problem... From what I read in the other posts, I have to supply the full URL, but the page that I want to redirect to is in an array.
In other words, I'm iterating through an array of pages, if a condition is met, it moves on to the next, if not, it returns to the previous. have can I do this with header(); ...???
Code: Select all
inlcude(file.php);I have just one problem... From what I read in the other posts, I have to supply the full URL, but the page that I want to redirect to is in an array.
In other words, I'm iterating through an array of pages, if a condition is met, it moves on to the next, if not, it returns to the previous. have can I do this with header(); ...???
Last edited by PapiSolo on Sun Aug 20, 2006 3:03 pm, edited 1 time in total.
Just a warning to newbies... be sure that no output is sent before you call header()... otherwise you'll get an error. ob_start() is a way to bandaid this, but it is not fixing the problem, it's just sort of like if you had an ant problem and there was a line of ants going up your wall, and you just painted over them... sort of similar to solving a problem like that.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Make a new thread detailing your problem and the code involved.PapiSolo wrote:I have just one problem... From what I read in the other posts, I have to supply the full URL, but the page that I want to redirect to is in an array.
In other words, I'm iterating through an array of pages, if a condition is met, it moves on to the next, if not, it returns to the previous. have can I do this with header(); ...???
headers_sent() may be of interest for some, too.The Ninja Space Goat wrote:Just a warning to newbies... be sure that no output is sent before you call header()... otherwise you'll get an error. ob_start() is a way to bandaid this, but it is not fixing the problem, it's just sort of like if you had an ant problem and there was a line of ants going up your wall, and you just painted over them... sort of similar to solving a problem like that.