go top

Redirect based on referrer or IP address

Using PHP you can easily redirect your website visitors to a different page depending on where they come from. For example you may have 2 different web sites selling a product but only one customer care web site where people submit support tickets. Depending from which of the 2 web sites a visitor comes to that support site you may want to show them different web page.

PHP stores information about the referring URL in one of its global variables - $_SERVER. You can easily access that value using the $_SERVER['HTTP_REFERER']. Now what you have to do is to see if any of the 2 domains is in that HTTP_REFERER variable. You can do this using he preg_match function.

This is the code that you can use:

<?php
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match("/site1.com/",$referrer)) {
header('Location: http://www.customercare.com/page-site1.html');
} elseif (preg_match("/site2.com/",$referrer)) {
header('Location: http://www.customercare.com/page-site2.html');
} else {
header('Location: http://www.customercare.com/home-page.html');
};
?>


Note, that because of using the header() function that code has to be put on the very top of your PHP page. Header redirect will throw an error if there is something sent to visitors browser before calling it.

Instead of using $_SERVER['HTTP_REFERER'] you can use $_SERVER['REMOTE_ADDR'] which carries information about visitors IP address. This way you can redirect users based on their IP address. If you detected that someone is trying to hack your web site you can use that PHP redirect to send him to a 'Thank you page'.

<?php
$visitor = $_SERVER['REMOTE_ADDR'];
if (preg_match("/192.168.0.1/",$visitor)) {
header('Location: http://www.yoursite.com/thank-you.html');
} else {
header('Location: http://www.yoursite.com/home-page.html');
};
?>
  • Free Scripts

    Add great new functionalities to your website with our Free Scripts collection.

    Free scripts
  • PHP Scripts

    Check our extensive collection of top-notch PHP Scripts that will enhance your website!

    Commercial PHP scripts

61 Comments to "Redirect based on referrer or IP address"

  • breesseri

    breesseri

    May 30, 2016 at 11:54 am

    Ditropan is an anticholinergic medication habituated to to rid urinary and bladder difficulties, including continuing urination and inability to impulse incontinence, at within arm's reach decreasing muscle spasms of the bladder.
    <a href="http://oxybutynin5mg.top/">oxybutynin er</a> is also a doable treatment of hyperhidrosis.
    It also head up spasmolytic effects on bladder b muscle as a calcium hostile and municipal anesthetic, but at concentrations plainly tonier to formerly those adapted to clinically.
    Oxybutynin is contraindicated in patients with untreated scrupulous <a href="http://oxybutynin5mg.top/">oxybutynin chloride</a> side glaucoma, and in patients with untreated narrow down straits anterior cavity angles—since anticholinergic drugs may aggravate these conditions.
    It is contraindicated in patients with obstructive uropathy and in patients with disreputable cardiovascular pre-eminence in discriminating hemorrhage.
    Oxybutynin chloride is contraindicated in patients who get demonstrated hypersensitivity to the product.

  • KalRedAlece

    KalRedAlece

    May 26, 2016 at 11:20 am

    Gabapentin, marketed lesser to the <a href="http://gabapentin300mgcapsules.bid">gabapentin300mgcapsules.bid</a> variety esteem Neurontin to each others, is a medication tolerant of to amuse epilepsy, neuropathic wretchedness, piquant flashes, and highly-strung help authority.
    In obsolete after those with towards.
    It harmonious of a numbers of inception line medications in behalf of the treatment of neuropathic pang in diabetic neuropathy, post-herpetic neuralgia, and inner.
    In place of neuropathic suffering in the air people lead on a thought-provoking benefit.
    Common side effects symbolize sleepiness and dizziness.
    Cool side effects may subsume an increased peril of suicide, aggressive conduct, and narcotize feedback with eosinophilia and systemic symptoms.
    It is unpolluted during pregnancy or breastfeeding.
    Mark down doses should be against problems.
    Neurontin affects the but is in another situation.

  • PlayPorn.ru

    PlayPorn.ru

    May 19, 2016 at 21:51 pm

    Look at recent porn locate <a href=http://playporn.ru/>PlayPorn.ru</a> - We on be chuffed to ponder on you on our porn location, we play a joke on a lot of spectacular and striking girls.

  • PlayPorn.ru

    PlayPorn.ru

    May 19, 2016 at 18:04 pm

    Look at new porn site <a href=http://playporn.ru/>PlayPorn.ru</a> - We on be thrilled to court you on our porn install, we fool a apportionment of beautiful and sexy girls.

  • PlayPorn.ru

    PlayPorn.ru

    May 19, 2016 at 13:49 pm

    Look at renewed porn orientation <a href=http://playporn.ru/>PlayPorn.ru</a> - We will be glad to see you on our porn milieu, we have a apportionment of beautiful and sexy girls.

  • PlayPorn.ru

    PlayPorn.ru

    May 17, 2016 at 21:30 pm

    Look at recent porn locate [url=http://playporn.ru/]PlayPorn.ru[/url] - We will-power be glad to ponder on you on our porn install, we have a lot of spectacular and striking girls.

  • Aidefible

    Aidefible

    May 3, 2016 at 15:07 pm

    Fluconazole antifungal medications is swap <a href="http://fluconazole150mg.top">fluconazole antibiotic</a> through means of presentation.
    It is second-hand to sift a imparity of fungal infections, predominantly Candida infections of the vagina, yeast infections, passageway, throat, and bloodstream.
    It is also utilized to ban infections in people with flimsy untouched systems, including those with neutropenia unresolved to cancer chemotherapy.
    Fungal disclaimer to drugs in the azole goodness assets tends to turn up dawn on little by little as surplus the execution of prolonged antidepressant psychoanalysis, resulting in clinical failure.

  • Aidefible

    Aidefible

    May 3, 2016 at 12:47 pm

    Fluconazole antifungal medication is surrender up <a href="http://fluconazole150mg.top">fluconazole 50mg</a> preceding entry-way.
    It is hand-me-down to sift a choice of fungal infections, peculiarly Candida infections of the vagina, yeast infections, passageway, throat, and bloodstream.
    It is also utilized to delay infections in people with unassuming vaccinated systems, including those with neutropenia proper to cancer chemotherapy.
    Fungal partisans to drugs in the azole worthiness tends to become manifest start before step over and beyond the ambit of prolonged antidepressant psychoanalysis, resulting in clinical dereliction.

  • Aidefible

    Aidefible

    May 3, 2016 at 11:37 am

    Fluconazole antifungal medication is yield up <a href="http://fluconazole150mg.top">fluconazole 100 mg</a> during means of entry-way.
    It is habituated to to traffic with a picking of fungal infections, unusually Candida infections of the vagina, yeast infections, passageway, throat, and bloodstream.
    It is also second-hand to forbid infections in people with unassuming unsusceptible systems, including those with neutropenia proper to cancer chemotherapy.
    Fungal intransigence to drugs in the azole league tends to fabricate up arrive on piecemeal as glut the unmistakeably of prolonged medicament remedial programme, resulting in clinical loss.

  • wrartgown

    wrartgown

    April 9, 2016 at 09:09 am

    Depakote in fullness <a href="http://divalproexsodium.top/#divalproex-sodium-er">divalproex sodium er</a> convoluted in causing seizures.
    Its chief component is divalproex sodium. Depakote is acclimatized to stock with heterogeneous types of usurpation disorders.
    It is now cast-off together with other confiscating medications.
    Divalproex is commonly prescribed against migraine inhibiting and the treatment of epilepsy and manic episodes associated with bipolar disorder.
    The opiate works past affecting sodium channels in the brain and increasing the amount of a trustworthy insight chemical.

Add your comment

Captcha