0

I am a complete PHP newbie, and I'm not even sure if I should be using PHP for what I'm doing, but here goes. Basically all I want to do is based on where a user comes from, change a link on the page to link to another location. I feel like this is very basic, but I'm not sure exactly how to phrase my search to get the best results. How would I do this?

3 Answers 3

1

You probably want something along the lines of

<?php if ($_SERVER['HTTP_REFERER'] === 'http://www.example.com') { ?>
  <a href="http://www.example.com/1">1</a>
<?php } else { ?>
  <a href="http://www.example.com/2">2</a>
<?php } ?>
Sign up to request clarification or add additional context in comments.

3 Comments

Personally, I always like to do strpos on these things since that 'www' is not always guaranteed to be present.
Will this work for determining the exact page of a website a user came from?
@Jumhyn When it is set, HTTP_REFERER does contain the full path to the url that directed the user to the page, yes.
1
$_SERVER['HTTP_REFERER'];

This will give you the url of client requesting the page. As said in this post: "Note that it is provided by the client so it may be empty or faked, so don't trust it security-wise."

source of REQUEST

Comments

0

Not sure exactly how you would best google that, but hopefully this will get you started:

To figure out where a user came from, you need $_SERVER['HTTP_REFERER']. Here's a tutorial based on doing a header redirect on that: http://bytes.com/topic/php/answers/7406-how-redirect-based-_server-http_referer

But you'll want to substitute echoing out a link instead of using header().

So quick snippet it would be something like this:

if (!empty($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'stackoverflow.com')) {
    echo "<a href='http://thatplaceiwanttogoto.com'>Here</a>";
} else {
    echo "<a href='http://thatotherplace.com'>There</a>";
}

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.