Click to See Complete Forum and Search --> : PHP redirect script
Can someone give me a php script to redirect to another page, for example.
http://www.example.com/go.php?=1
so that the 1 will redirect to another site. Thank you
cjc1055
02-06-2007, 12:46 PM
You mean like this?
header("Location: http://www.whatever.com/");
Yes but i can set what the web page it will redirect to, so if the url is
http://www.example.com/go.php?=1 it will redirect www.google.com
http://www.example.com/go.php?=2 it will redirect to www.yahoo.com
for example
cjc1055
02-06-2007, 01:07 PM
you can do this...
change the link to http://www.example.com/go.php?id=1
if (isset($_GET['id']) == "1") { header("Location: http://www.whatever.com/"); }
elseif ( .... etc....) { ;}
pcthug
02-06-2007, 06:58 PM
Try a switch statement with a default case to catch bad requests
switch (@(int)$_GET['id'])
{
case 1 : $url = 'http://www.google.com/'; break;
case 2 : $url = 'http://www.yahoo.com/'; break;
case 3 : $url = 'http://www.amazon.com/'; break;
// the default URL to redirect to (will be called if id does not match any of the previous cases)
default: $url = 'http://www.webdeveloper.com/';
}
header("Location: $url");
NightShift58
02-06-2007, 08:06 PM
You should consider using IF/ELSEIF/ELSE instead, as it is both more efficient and less error-prone:<?php
$userCHOICE = intval($_GET['id']);
IF {$userCHOICE == 1) :
$url = 'http://www.google.com/';
ELSEIF {$userCHOICE == 2) :
$url = 'http://www.yahoo.com/';
ELSEIF {$userCHOICE == 3) :
$url = 'http://www.amazon.com/';
ELSE :
// the default URL to redirect to (will be called if id does not match any of the previous tests)
$url = 'http://www.webdeveloper.com/';
ENDIF;
header("Location: $url");
?>SWITCH/CASE is really a poor alternative to IF/ELSE. In another thread, Bokeh demonstrated how treacherous it can be and unless you have a special reason for using some of its unique control features, you should stay away from it.
pcthug
02-06-2007, 08:37 PM
I tried digging up the thread you mentioned NightShift, but to no avail. Anyways, I generally use switch statements whenever I need to compare the same expression two or more times. It offers greater read and maintainability over and if/else statement and is a core feature in all mainstream programming languages.
NightShift58
02-06-2007, 10:12 PM
I tried digging up the thread you mentioned NightShift, but to no avail.This is a good starting point, though there is some before and after...
It offers greater read and maintainability over and if/else statementThe "easier" reading is in the eyes of the beholder so I will not disagree. In fact, if it wasn't such a limited and inefficient control structure, I like the simplicity of the statement and the visual aspect - if only it woul live up to the promise...
... and is a core feature in all mainstream programming languages.Yes, it is. But so is IF/ELSE.
With the exception that, unlike SWITCH, IF/ELSE is a core feature of machine language and SWITCH requires translation by the compiler so it is broken down to a series of IF/ELSE instructions. Also, not every programming language requires the use of BREAK or similar premature block exit.