0

I am not familiar with php and want to write a redirect script using php (The server I am working on does not support asp)

I think it needs to be something like this

<?php if ($_GET[id]; == 'test') {echo '<meta http-equiv="refresh" content="0;URL=http://www.test.com" />'} ?>

I want to send a query string like http://www.domain.com?id=test then the page should redirect to http://www.test.com or a query like http://www.domain.com?id=example and it shoud redirect to http://www.theexamplesite.com

Using javascript and classic asp I would have done it like this:

<% if (Request.QueryString("id") == 'test') { %><meta http-equiv="refresh" content="0;URL=http://www.test.com" /><% } %>
<% if (Request.QueryString("id") == 'example') { %><meta http-equiv="refresh" content="0;URL=http://www.theexamplesite.com" /><% } %>

Hope you can help me!

Thanks

5 Answers 5

5
<?php

$targets = array(
  'test'    => 'http://www.test.com/',
  'example' => 'http://www.theexamplesite.com/',
);

if (isset($_GET['id']) && isset($targets[$_GET['id']]))
{
  header('Location: '.$targets[$_GET['id']]);
  exit;
}

That way you have a good view of your configured redirects and are able to add / remove / update them easily !

Sign up to request clarification or add additional context in comments.

4 Comments

Nice, but you should check whether $_GET['id'] is set too!
I Like this solution best as it shows me the variables easily! - Thanks to ALL
Is it possible to open the site in a new window like target="_blank"
@Gerald no you can't, since all we're doing here is sending an HTTP header to the browser (nor could you using the meta html tag btw)
3

You have a syntax error. It should be:

if ($_GET['id'] == 'test')

But for the actual redirect, you might want to use header:

if(array_key_exists('id', $_GET) {
    if($_GET['id'] === 'test') {
        header('Location: http://www.test.com');
    }
    else if($_GET['id'] === 'example') {
        header('Location: http://www.theexamplesite.com');
    }
}

Reference: array_key_exists

6 Comments

@Wookai: Thanks, I just was about to fix it :D
Thanks a million - I did this so far <?php if ($_GET['id'] == 'site') echo '<meta http-equiv="refresh" content="0;URL=site.com" />' ?> <?php if ($_GET['id'] == 'site1') echo '<meta http-equiv="refresh" content="0;URL=site1.com" />' ?> and it seems to work
@Gerald Ferreira: Yep, but using header is the cleaner way I would say.
I will test your solution just now as it looks more elegant!
The array_key_exists is unnecessary bloat here. First, a $_GET["id"] can be an empty string or NULL at worst. Hence key presence testing is dumb, an isset() is sufficient. But PHP also is a dynamic and weakly typed language, an unset/NULL value will be interpreted as empty string or false. You only need isset if you keep debugging notices enabled on your production server.
|
1

You can simply write:

<?php

if ($_GET[id] == 'test') {
    header('Location: http://www.test.com');
}
else {
    // real output
}

?>

Comments

1

This just looks like a target for exploitation. Make sure you do some validation on the value of $_GET['id'] before you start redirecting the users. If someone does http://www.domain.com?id=http://www.maliciouswebsite.com, will your script take them there?

Just be careful with redirects and do a lot of input validation.

1 Comment

It should not I think: I am writing this to hide affiliate links to not pass Page Rank to the sites I am promoting: the id will equal a value that I pre setup: If value = 1 redirect to site 1 if value 2 redirect to site 2 - so if you put malicious site it is not defined and should not work
1

I fail to see why were not accomplish the task given that you have ASP/JavaScript knowledge. Just tweak your asp script a little bit:

<?php
    if ($_GET["id"] == 'test')
    {
        ?>
        <meta http-equiv="refresh" content="0;URL=http://www.test.com" />
        <?php
    }
    if ($_GET["id"] == 'example')
    {
        ?>
        <meta http-equiv="refresh" content="0;URL=http://www.theexamplesite.com" />
        <?php
    }
?>

There are other constructs available to do the same job such as switch...case or the ?: operator. The ?: operator can shorten up your code quite a bit:

<meta http-equiv="refresh" content="0;URL=<?php echo $_GET['id'] == 'test' ? 'http://www.test.com' : 'http://www.theexamplesite.com'; ?>" />

1 Comment

I Think I struggled with the $_GET function - Thanks for the tip

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.