Forum Moderators: coopster

Message Too Old, No Replies

Simple PHP redirect script

simple php redirect script

         

Roger30

12:05 am on Mar 8, 2006 (gmt 0)

10+ Year Member



Hi everyone, new to PHP but learning. I am trying to make a simple redirect script for my site and this is what I have so far. Problem is I get the following error and cant seem to figure out why.

Parse error: parse error, unexpected ':' in /usr/www/mysite/jump.php on line 3

Here is the script I have.


<?php

if ($id == "") {$link = http://www.mysite.com;} //Default Blank
if ($id == "1") {$link = http://www.yahoo.com;} // COMMENT
if ($id == "2") {$link = http://www.google.com;} // COMMENT
if ($id == "3") {$link = http://www.webmasterworld.com;} // COMMENT

header('HTTP/1.1 301 Moved Permanently'); // Clean 301 header
header("Location: $link"); // Jump to the link
exit();

?>

All I want it to do is if I point a link to www.mysite.com/jump.php?=1 is to redirect in this case to www.yahoo.com. Any thoughts I what I did wrong?

JollyK

12:11 am on Mar 8, 2006 (gmt 0)

10+ Year Member



You have to put quotes around strings:

if ($id == "") {$link = "http://www.mysite.com";} //Default Blank
if ($id == "1") {$link = "http://www.yahoo.com";} // COMMENT
if ($id == "2") {$link = "http://www.google.com";} // COMMENT
if ($id == "3") {$link = "http://www.webmasterworld.com";} // COMMENT

Single quotes would work too.

:-)

JK

whoisgregg

12:15 am on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(Someone may have already done it, but just in case...)
Welcome to WebmasterWorld, Roger30!

Hmm, I'd say it's likely that $link is not set somehow when you get to the header("Location: $link") line.

I'd make a few changes, first your links to "www.mysite.com/jump.php?=1" should look more like:

www.mysite.com/jump.php?id=1

Also, your $link = should have quotes of some type around the urls, like so:

$link = 'http://www.mysite.com';

Next, make sure that $id is set to the right thing. Add this line to the beginning of your script:

$id =$_GET['id'];

That should fix your script as it is now. :)

whoisgregg

12:16 am on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JollyK beat me to it. Darn slow typing! :)

Roger30

8:59 am on Mar 8, 2006 (gmt 0)

10+ Year Member



Thanks for the help guys. I really appreciate it.

dreamcatcher

9:15 am on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World Roger30.:)

Another solution in this situation would be a switch statment.

$id = $_GET['id'];

switch ($id)
{

case 1:
header("Location: [yahoo.com");...]
break;

case 2:
header("Location: [google.com");...]
break;

case 3:
header("Location: [webmasterworld.com");...]
break;

default:
header("Location: [mysite.com");...]
break;

}

dc