/    Sign up×
Community /Pin to ProfileBookmark

PHP redirect script

Can someone give me a php script to redirect to another page, for example.

[url]http://www.example.com/go.php?=1[/url]

so that the 1 will redirect to another site. Thank you

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@cjc1055Feb 06.2007 — You mean like this?

[code=php]
header("Location: http://www.whatever.com/");
[/code]
Copy linkTweet thisAlerts:
@TaboauthorFeb 06.2007 — 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
Copy linkTweet thisAlerts:
@cjc1055Feb 06.2007 — you can do this...

change the link to http://www.example.com/go.php?id=1

[code=php]
if (isset($_GET['id']) == "1") { header("Location: http://www.whatever.com/"); }
elseif ( .... etc....) { ;}
[/code]
Copy linkTweet thisAlerts:
@pcthugFeb 07.2007 — Try a switch statement with a [I]default[/I] case to catch bad requests[code=php]
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");
[/code]
Copy linkTweet thisAlerts:
@NightShift58Feb 07.2007 — You should consider using IF/ELSEIF/ELSE instead, as it is both more efficient and less error-prone:[code=php]<?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");
?>[/code]
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.
Copy linkTweet thisAlerts:
@pcthugFeb 07.2007 — 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.
Copy linkTweet thisAlerts:
@NightShift58Feb 07.2007 — I tried digging up the thread you mentioned NightShift, but to no avail.[/quote]This is a good starting point, though there is some before and after...

It offers greater read and maintainability over and if/else statement[/quote]The "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.[/QUOTE]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.
×

Success!

Help @Tabo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...