tags:

views:

33

answers:

3

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  A: 
$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

akellehe
@akellehe elseif not else if
esryl
+1  A: 

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();
    }
?>
steven_desu
for some reason the space between student and gender is causing this problem. http://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
FALCONSEYE
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`
RobertPitt
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
FALCONSEYE
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.
steven_desu
A: 

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)

RobertPitt
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.
FALCONSEYE
You should always keep your keys very string to `a-z` `0-9` and `_` so that your key in your form should become `custom_gender`.
RobertPitt