How to redirect Page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
umapathy
Forum Newbie
Posts: 14
Joined: Fri Jul 28, 2006 2:21 am
Location: chennai - india

How to redirect Page

Post by umapathy »

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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

header();
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

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.
	}
?>
All you got to do is set $Correct to your corresponding input variable.

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
	}
?>
Both are practicaly the same, just the bottom example checks for if the value is TRUE, FALSE, or OTHER, while the top checks only if it is TRUE.

Hope this helps :), btw I just woke up sorry for any spelling errors lol :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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;
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Everah wrote:Make sure to follow header() with exit;
And always, always use full URLs. Standards strict browsers will be unable to follow relative URLs.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

God catch. As per the manual:
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:
Code to standard and use full URI's. Thanks Feyd.
PapiSolo
Forum Newbie
Posts: 24
Joined: Wed Aug 09, 2006 12:16 pm

Post by PapiSolo »

Could this also be done using http_redirect(); ?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

PapiSolo wrote:Could this also be done using http_redirect(); ?
That is an extension: http://us3.php.net/manual/en/ref.http.php
PapiSolo
Forum Newbie
Posts: 24
Joined: Wed Aug 09, 2006 12:16 pm

Post by PapiSolo »

Thanks for the quick reply... i have a similar problem and was trying to achieve this with

Code: Select all

inlcude(file.php);
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(); ...???
Last edited by PapiSolo on Sun Aug 20, 2006 3:03 pm, edited 1 time in total.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

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.Image
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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(); ...???
Make a new thread detailing your problem and the code involved.
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.Image
headers_sent() may be of interest for some, too.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

Everah wrote:Make sure to follow header() with exit;
Oh, good call :) Like I said I had just woken up, but yes always remember that exit!
Post Reply