Page 1 of 1

How to redirect Page

Posted: Sun Aug 20, 2006 7:51 am
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

Posted: Sun Aug 20, 2006 7:52 am
by Benjamin

Code: Select all

header();

Posted: Sun Aug 20, 2006 10:52 am
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 :)

Posted: Sun Aug 20, 2006 10:56 am
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;
}
?>

Posted: Sun Aug 20, 2006 11:16 am
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.

Posted: Sun Aug 20, 2006 11:21 am
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.

Posted: Sun Aug 20, 2006 2:35 pm
by PapiSolo
Could this also be done using http_redirect(); ?

Posted: Sun Aug 20, 2006 2:39 pm
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

Posted: Sun Aug 20, 2006 2:55 pm
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(); ...???

Posted: Sun Aug 20, 2006 3:02 pm
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

Posted: Sun Aug 20, 2006 3:07 pm
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.

Posted: Sun Aug 20, 2006 7:13 pm
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!