0

I want to create a URL redirect based on a URL variable.

so, if student%20gender (student gender) is male then go to www.one.com, if female, go to www.two.com.

couldn't figure this one out yet. any help?

2
  • What have you tried? Please post some code and we can help you along a bit more. Commented Oct 31, 2010 at 17:17
  • also please add an underscore to your variable, student_gender, will save you massive headaches in the future. steer clear of spaces in variable or key names. Commented Oct 31, 2010 at 17:39

3 Answers 3

3

Question could use a little bit of a better explanation. Do you mean that someone is going to http://www.yoursite.com/yourscript.php?student%20gender=male and you want them to be redirected to http://www.one.com?

If this is the case, PHP has a built in variable known as $_GET which stores the values listed after a ? in a URL. So in the above example, we'd see:

$_GET['student gender'] = male;

You can use this to access any number of parameters separated by &

So the URL http://www.site.com/index.php?val1=a&val2=b&val3=c would give us:

$_GET['val1'] = a;
$_GET['val2'] = b;
$_GET['val3'] = c;

After this, to do a redirect in PHP the easiest way is to send a Location: header. This is done like so:

<?php
header("Location: www.newsite.com");
?>

Combining this with our $_GET variable and some simple logic:

<?php
    if($_GET['student gender'] == 'male'){
        header("Location: www.one.com");
        die();
    } else {
        header("Location: www.two.com");
        die();
    }
?>
Sign up to request clarification or add additional context in comments.

4 Comments

for some reason the space between student and gender is causing this problem. localhost/test.php?custom gender=female, and test.php has : <?php $val = $_GET['custom gender']; echo "the gender is: $val"; i cannot print the $val on the screen. it's blank
you don't need the second else, just add it header after the first if, also strings should be wrapped in quotes due to PHP Getting mixed up with constants, and you should be using empty() or isset() on your expressions within the if
also, i am getting this error: Warning: Cannot modify header information - headers already sent by (output started at C:\...\test.php:7) in C:\...\test.php on line 12
First off, links to localhost don't really work for us =) Second, you can't use the header() function after you've output anything. This output may have been intentional (using echo) or may have been unintentional (having a blank line before <?php). If you plan on redirecting them, don't output anything. If you may or may not redirect them, wait to output after you know they aren't being redirected.
2
$var = $_GET['yourvar'];

if($var == 'one'){
    header("Location: http://www.one.com/");
}else if ($var == 'two'){
    header("Location: http://www.two.com/");
}

then do http://www.yoururl.com?yourvar=one

Comments

1

You also have to make sure you look at the security aspects here, the best way yo accomplish this is

$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender'] : false;

switch($gender)
{
    default: //The default action
        //Send back to the gender select form
    break;

    case 'male':
        //Send to male site!
    break;

    case 'female':
        //Send to female site!
    break;
}

This should be sufficient, but please never use $_X['?'] in functions that execute either shell or database queries without sanitation.

Note: _X being (GET,POST,REQUEST,FILES)

1 Comment

for some reason the space between student and gender is causing this problem. localhost/test.php?custom gender=female, and test.php has : <?php $val = $_GET['custom gender']; echo "the gender is: $val"; i cannot print the $val on the screen. it's blank. also, i am getting this error: Warning: Cannot modify header information - headers already sent by (output started at C:\...\test.php:7) in C:\...\test.php on line 12. i don't know why i get this error.

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.