Forum Moderators: coopster

Message Too Old, No Replies

referrer link

detect a particular referrer and provide link back to that referrer

         

nunzia

12:15 am on Apr 22, 2010 (gmt 0)

10+ Year Member



I need to detect the referrer for certain pages, and if it's a particular referrer, provide a link back to that referrer. If it's any other referrer, it should do nothing. I was working with this code

$ref = $_SERVER['HTTP_REFERER'];

if ( $ref = 'http://referring.page' ) {
echo ( "Return to <a href="http://referring.page/">Referring Page</a>") ;

} else {
}


but not getting very far ... never tried something quite like this before. And could this be done as a function?

TheMadScientist

12:32 am on Apr 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if ( $ref = 'http://referring.page' ) {
echo ( "Return to <a href="http://referring.page/">Referring Page</a>") ;

} else {
}


The single = in the if, which will evaluate to 'true' seems to be the biggest issue I'm seeing quickly, the best way to avoid this issue is to write your if statement the other direction.

$ref = 'http://referring.page' = true in an if
'http://referring.page' = $ref = false

Either way, it won't work, unless you use == or ===.

if($ref == 'http://referring.page') { }
if($ref === 'http://referring.page') { }

BTW: Welcome to WebmasterWorld!

dreamcatcher

7:13 am on Apr 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, the referer is not available by default. You should check its existence before you use it:

$ref = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');

dc

nunzia

1:54 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



thanks guys ... glad to have found you ... will let you know my progress

nunzia

2:31 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



ok, it's not crashing the page, but it's not displaying the link either.

$ref = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''); 

if ( $ref == 'http://referring.page/' ) {
echo ( 'Return to <a href="http://referring.page/">Referring Page</a>') ; }

else {
}


Any ideas?

Matthew1980

3:09 pm on Apr 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there nunzia,

Well that means as the code is doing exactly as you are asking it to!

$ref = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');

If the $_SERVER['HTTP_REFERER'] isn't set then you are assigning it blank (no value) so $ref will equal: $ref = ""; but if it is set you are assigning it to the var. I think as you need a default value there then you will see the link get populated or you can place some code in the else clause like this:-

else{
echo "No referer set, cannot display link back to wherever you came from :)";
}

Because at the moment you have nothing in the else clause to tell you that the referer isnt set!

Hope I have explained that Ok for you,

Cheers,
MRb

nunzia

3:52 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



Doh! Bingo -- that's the ticket. Many thanks.

rocknbil

5:24 pm on Apr 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or . . . give them something useful . . .

$ref = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'link-to-your-thank-you-or-sitemap-page');

Matthew1980

6:31 pm on Apr 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Rocknbil :),

Yup, I realised that after I posted - at least using that would keep the traffic on your website.

nunzia: Glad your sorted, but as Rocknbil suggests, use the else clause/false side of the ternary to your advantage.

Good luck with the rest of the project.

Cheers,
MRb

nunzia

7:15 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



Beautiful. Thanks for the suggestion!

nunzia

7:35 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



Yikes .. one more thing. The referral could come from anywhere on the website referring.page. It could be referring.page/about.php or something. Or it could be www.referring.page, etc. How do I do the variables?

TheMadScientist

11:08 pm on Apr 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Change the if part of your code to:

if ( strpos($ref,'example.com')!==FALSE ) {
/* Do Stuff */
}

Will 'Do Stuff' every time example.com sends you a visitor.
strpos() [us2.php.net]

nunzia

11:22 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



Thanks Mad Scientist. That did it. Appears to now be doing everything I want it to do.