php - save the referring site's url

bizbez

Registered Member
Joined
Jan 26, 2011
Messages
86
Reaction score
3
I?d like to save the referring page to my site ? only external sources and then pass it in a session to another page where I log this url.
My site uses the index.php file to connect to all pages of the site (like a global include). So my problem is that I do record the referring url, but it is erased if a user passes from page to page ? my script writes the previous page, instead of just saving the entrance url and that?s it.

Anyone can help here?
 
something like

if(strstr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST'])){
echo "It's same domain";
} else {
echo "Different domain";
}

?
 
index.php:
PHP:
<?php
session_start();
if (!strstr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST'])){
     $_SESSION['ref']=$_SERVER['HTTP_REFERER'];
}
anotherpage.php:
PHP:
<?php
session_start();
if (!empty($_SESSION['ref'])){
     log_referer($_SESSION['ref']); //your logging mechanism
}
 
Code:
$_SESSION['ref'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

if there is a refferer and not direct access $_SESSION['ref'] will hold the value if not will be empty
 
as usual BHW members do the trick, thanks guys.

here's the final code i've used - checks for 2 things:
1. if the domain is the same - thanks to @nytrolic!!
2. if there is already a session that holds the http referer - thanks to @misulicus

@tripper_john_md thanks to you too - same solution.

here's the final code
Code:
if(strstr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST'])) {
       if(!isset($_SESSION['ref']) && isset($_SERVER['HTTP_REFERER'])) $_SESSION['ref'] = $_SERVER['HTTP_REFERER'];

		echo "It's same domain   ".$_SESSION['ref'];
		} 
else {
	if(!isset($_SESSION['ref']) && isset($_SERVER['HTTP_REFERER'])) $_SESSION['ref'] = $_SERVER['HTTP_REFERER'];
       echo "Different domain   ".$_SESSION['ref'];
}
 
Back
Top