0

basically the page receives varaibles via the url http://sitename.com?c1=xxx&c2=yyy.

I want to redirect to one link if c1 is less than 40 and otherwise go to a main link.

How do I program something like this?

1
  • Just make sure that if you use PHP for this, you buffer your output (see: php.net/manual/en/book.outcontrol.php), because if ANY output is sent before your redirect instructions, the redirection won't work - the headers will have already been sent. Commented Sep 2, 2011 at 9:12

5 Answers 5

1

using php you use

Header("Location: theurltoredirectto.com");

the javascript solution would be

window.location = "http://www.theurltoredirectto.com/"
Sign up to request clarification or add additional context in comments.

Comments

1

In PHP:

if ($_GET['c1'] < 40) {
   Header("Location: http://sitename.com/onelink");
} else {
   Header("Location: http://sitename.com");
}

Comments

1

In PHP, it’s essentially:

<?php
$c1 = int($_GET['c1']);

if ($c1 < 40)
    header('Location: http://new-location');
?>

After executing this code, just exit the script.

1 Comment

@Breezer Typo. Thanks for noticing.
0

In javascript you can use top.location.href='http://your.url.here' or window.location.href=...

Comments

0

In PHP, you'll want to use this code at the top of the script, before anything that outputs data to the page (such as echo, print, etc), as headers must be sent before any other data:

<?php
if (is_numeric($_GET["c1"]) and $_GET["c1"] < 40) { //Checks if the c1 GET command is a number that is less than 40
    header("Location: /path/to/page2.php"); //Send a header to the browser that will tell it to go to another page
    die(); //Prevent the script from running any further
}

You can set the /path/to/page2.php bit to anything that you would usually use for an <a> tag.

I don't recommend doing redirects in JavaScript or HTML, because if someone clicks Back in their browser, they'll be taken back to the page and be re-redirected to the next page.

Ad@m

1 Comment

@Breezer Sorry, my bad, I've fixed it now.

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.