How to recognize visitors who came from a redirected domain

I purchased a smaller competitor website and redirected that domain to my website with 301 moved permanently. Is there a way I can acknowledge  and greet visitors who came from that domain to my site using PHP?
greenerpasturesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Have a look at the HTTP_REFERER key of the server array:

$referer = $_SERVER['HTTP_REFERER'];
if (strpos ($referer, "somedomain.com") {
    echo "Visitor was referred from somedomain.com"
}

Open in new window

0
greenerpasturesAuthor Commented:
For some reason even after I corrected a few mistakes from the example above it is still not welcoming the visitor from the referred domain. Here's the code I used:

<?php 						
$referer = $_SERVER['HTTP_REFERER'];
if (strpos ($referer, "somesite.com")) {
    echo "Welcome visitor from Somesite.com!";
}						
?>

Open in new window


I Placed this into the body, should any of this go into the head?
0
Ray PaseurCommented:
Into the "head" of what?  It's a PHP script, so it would be run on the server and could set a variable that is used in the HTML document.

I believe your 301 redirect could also add a parameter to the URL, something like &from=somesite.com
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Chris StanyonWebDevCommented:
You can put the code wherever you want. If you use the code exactly as you've posted it then you put it wherever you want the output to be (the welcome message)
0
Chris StanyonWebDevCommented:
Hmmm. Looking into this, the referrer won't contain the original domain - it will contain the page that referred TO the original domain! Sorry about that.

You may need to follow Ray's idea and add a querystring to your redirect and then check for that in your new site. For example, redirect to:

newdomain.com?ref=1

and then check with this:

<?php
if (isset($_GET['ref'])) {
   echo "Welcome from the old domain";
}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Actually now that I think about the question a little more the answer about "head" would be "no, it would go into the body of the HTML document."  Because it is using echo, it will write to the browser output stream, and adding that message to the head of the HTML document wouldn't be appropriate.  Two things to check...

Have you used var_dump($_SERVER) to see if you're getting something meaningful in HTTP_REFERER?

When you use "view source" do you see the welcome message in the HTML source?
0
Ray PaseurCommented:
@ChrisStanyon: or even something like this, maybe

newdomain.com?ref=olddomain.com
0
Chris StanyonWebDevCommented:
@Ray - i was thinking of using the querystring more as a flag so the content would be  somewhat irrelevant! If there's more than one 'old' domain, then adding that to the querystring would certainly give more flexibility..
0
Ray PaseurCommented:
Yeah, it also seems like there should be a little thought given to whether there are GET-method requests that influence the content of the URL arguments from the old domain.
0
greenerpasturesAuthor Commented:
That worked
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.