1

I am new to PHP, so please bear with me in this elementary level question.

I want to create a script that redirects the user to various addresses based on the GET variable. for example, redirection.php?id=youtube should redirect them to www.youtube.com, redirection.php?id=twitter should redirect them to www.twitter.com, and so on.

Here is my code:

<!DOCTYPE html>
<html>
<head>
<title>Please Wait...</title>
</head>
<body>

<?php
// directs the user to various locations on the internet
print_r($_GET);

if($_GET['id'] === 'youtube') {
    header('Location: http://www.youtube.com/') ;
    die()
}
if($_GET['id'] === 'twitter') {
    header('Location: http://www.twitter.com/') ;
    die()
}
if($_GET['id'] === 'reddit') {
    header('Location: http://www.reddit.com/') ;
    die()
}
?>

</body>
</html>

So far the PHP file does not respond at all, what do I change to fix this?

Again, sorry for the elementary level question, but this is literally my first PHP script and I am not very familiar with some of the terminology which makes Google searching for the correct code difficult.

3
  • die(); you forget semicolon in die() Commented Apr 18, 2015 at 7:55
  • That managed to fix it, Thanks! Commented Apr 18, 2015 at 7:58
  • @ Richie Accept saty's answer if that is working for you. Commented Apr 18, 2015 at 8:02

2 Answers 2

1

When comparing values in PHP for equality you can use either the == operator or the === operator. What’s the difference between the 2? Well, it’s quite simple. The == operator just checks to see if the left and right values are equal. But, the === operator (note the extra “=”) actually checks to see if the left and right values are equal, and also checks to see if they are of the same variable type (like whether they are both booleans, ints, etc.).

And

die(); you forget semicolon in die()

you code should be

if($_GET['id'] == 'youtube') {
    header('Location: http://www.youtube.com/') ;
    die();
}
if($_GET['id'] == 'twitter') {
    header('Location: http://www.twitter.com/') ;
    die();
}
if($_GET['id'] == 'reddit') {
    header('Location: http://www.reddit.com/') ;
    die();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can try below code:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Please Wait...</title>
    </head>
    <body>

    <?php
    // directs the user to various locations on the internet
    extract($_REQUEST);

    if(isset($id) && $id == 'youtube') {
        header('Location: http://www.youtube.com/') ;
        die();
    }
    if(isset($id) && $id === 'twitter') {
        header('Location: http://www.twitter.com/') ;
        die();
    }
    if(isset($id) && $id === 'reddit') {
        header('Location: http://www.reddit.com/') ;
        die();
    }
    ?>

    </body>
    </html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.