Constantin
Constantin

Reputation: 1

How to make a JavaScript or PHP conditionally redirect?

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?

Upvotes: 0

Views: 177

Answers (5)

Konrad Rudolph
Konrad Rudolph

Reputation: 545518

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.

Upvotes: 1

Nathan Q
Nathan Q

Reputation: 1902

In PHP:

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

Upvotes: 1

kirb
kirb

Reputation: 2049

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

Upvotes: 0

Ibolit
Ibolit

Reputation: 9720

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

Upvotes: 0

Breezer
Breezer

Reputation: 10490

using php you use

Header("Location: theurltoredirectto.com");

the javascript solution would be

window.location = "http://www.theurltoredirectto.com/"

Upvotes: 1

Related Questions