1

I need to do some basic if URL statement stuff but I know nothing about PHP. I've tried a few examples I've found online but they don't seem to work.

Note: this is for a wordpress blog if that makes a different.

I want a "PHP IF URL" statement to do the following:

If URL referrer = twitter.com then echo

"Follow us on <a href="twitter.com/example">Twitter</a>

If URL referrer = facebook.com then echo

"Follow us on <a href="facebook.com/example">Facebook</a>

If no url referrer or none of the above echo "Follow us on Nothing"

1
  • 1
    Please share the few examples you've tried, and you could learn where it went wrong. Commented Mar 22, 2012 at 23:48

3 Answers 3

2

You can check your referrer by accessing $_SERVER['HTTP_REFERER']. You can parse out the url using parse_url() if necessary.

Read carefully the warning on php.net regarding the referrer:

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

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

1 Comment

Asker should also note that when coming from https sites, the HTTP referrer is not set. Facebook and Twitter use https AFIK.
1

You can retrieve url from $_SERVER['HTTP_REFERER'], parse it with parse_url and then echo your response based on what is in the response from parse_url. It returns an associative array where the host key is the hostname.

<?php
if ( empty( $_SERVER['HTTP_REFERER'] ) )
    echo 'Follow us on Nothing';
else {
    $ref = parse_url( $_SERVER['HTTP_REFERER'] );

    switch( $ref['host'] ) {
    case 'twitter.com':
        echo 'Follow us on <a href="http://twitter.com/example">Twitter</a>';
        break;
    case 'facebook.com':
        echo 'Follow us on <a href="http://facebook.com/example">Facebook</a>';
        break;
    default:
        echo 'Follow us on Nothing';
        break;
    }
}
?>

Remember to check if there is anything at all in the referrer. Not all browsers send it and it will not be sent if the user was not sent here from another site, ex: the url was manually typed in.

Also note that if the user navigated from a page using https there wont be sent any reference either.

Comments

0

The output will be stored in $str.

$url = $_SERVER["HTTP_REFERER"];
$url_parts = parse_url($url);

switch($url_parts["host"])
{
  case "twitter.com":
    $str = "Follow us on <a href=\"twitter.com/example\">Twitter</a>";
    break;
  case "facebook.com":
    $str = "Follow us on <a href=\"facebook.com/example\">Facebook</a>";
    break;
  default:
    $str = "";
    break;
}

Comments

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.