Cool Referring URL Script Not Working

6 replies
This is frustrating

The below is a awesome script I've put together with a coding friend used for tracking form submissions. If the person came from a Google, MSN, or Yahoo search, it inserts the searched keywords. If the person came from another source, it inserts the URL.

Whenever I test it I look in the source code and it inserts properly! But 8 of 9 submissions since I put it up have returned "No search term or referring URL", which should only be the case if they type in the url directly or bookmark the site - extremely, extremely unlikely.

You can test it and see the site at recruitshop.com.au

<input type="hidden" mandatory="false" validation="" sf_title="Search Term" value="<?php echo ReferringSourceLarge(); ?>">

Code:
<?php
function ReferringSourceLarge()
{
$refer = parse_url($_SERVER['HTTP_REFERER']);
$host = $refer['host'];
if($host != 'www.recruitshop.com.au' && $host != 'recruitshop.com.au') {
	$querycal = $refer['query'];
	$_SESSION['query'] = explode("&", $querycal);
	$_SESSION['extrefer'] = $_SERVER['HTTP_REFERER'];
}
if (isset($_SESSION['query'])) {
	foreach($_SESSION['query'] as $q)
	{
		list($key, $value) = explode("=", $q);
		$a[$key] = urldecode($value);
	}
}
if(!isset($_SESSION['extrefer'])) return 'No search term or referring URL';
if(strstr($host,'google'))
{
	$_SESSION['a'] = $a['q'];
	return $_SESSION['a'];
}
elseif(strstr($host,'yahoo'))
{
	$_SESSION['a'] = $a['p'];
	return $_SESSION['a'];
}
elseif(strstr($host,'msn'))
{
	$_SESSION['a'] = $a['q'];
	return $_SESSION['a'];
}
else if (isset($_SESSION['a']))
{
	return $_SESSION['a'];
}
else { return $_SESSION['extrefer']; }
}
?>
If you can't solve it, any direction would be greatly appreciated!
#cool #referring #script #url #working
  • Profile picture of the author AdwordsMogul
    Do you have
    Code:
    <?php session_start(); ?>
    at the beginning of the page?

    Without it session variables won't work. It has to be on every page that uses sessions.
    Signature
    "Those who can - DO IT. Those who can't, say it's impossible."
    Jean Paul a.k.a AdwordsMogul
    PHPDevelopers.net - Top of the range PHP developers

    Easy Link Saver - Are you tired of the pain of constantly searching for your affiliate links? ( Chrome extension - FREE )
    {{ DiscussionBoard.errors[4089800].message }}
    • Profile picture of the author Joshua Uebergang
      I doubt it's a database error because it hasn't been working over a month's time.

      Do you have <?php session_start(); ?> at the beginning of the page?
      Yes.
      {{ DiscussionBoard.errors[4091821].message }}
  • Profile picture of the author AdwordsMogul
    This could also be because HTTP_REFERER isn't always passed by the user's browser.
    Signature
    "Those who can - DO IT. Those who can't, say it's impossible."
    Jean Paul a.k.a AdwordsMogul
    PHPDevelopers.net - Top of the range PHP developers

    Easy Link Saver - Are you tired of the pain of constantly searching for your affiliate links? ( Chrome extension - FREE )
    {{ DiscussionBoard.errors[4094390].message }}
    • Profile picture of the author Joshua Uebergang
      Isn't always? What, are there certain browsers that reject this session variable?

      What's something more reliable then Mogul Sir?
      {{ DiscussionBoard.errors[4101975].message }}
      • Profile picture of the author Tim Brownlaw
        G'day Joshua

        I've had a quick play with this, so just a few questions.
        What did you do to test the script and what keywords did you use?

        I did a search for the URL on Google to come in that way and it showed the URL - so I'm not sure if that was indeed the "keyword" or just the URL being caught. I couldn't come up with any keywords that gave me a hit on your site.

        Here's an exercise for you - read through your source code and see of you can find out , clearly, what you site is about. Ie what are the search engines seeing.

        When do you call this function?

        I found the site on trulocal (again using the URL) and came in via the link to the home page but I suspect you are not capturing this information if I land on a "non form" page.
        Once I got to a form on the Candidates page I got the URL of the candidates page. Not very useful!

        Each page should be running this function so when someone does come in from outside it captures it. Then it should flag that it has been detected to disable it on each internal page view so it doesn't get clobbered and instead have the info readable where required.

        Hope that helps some

        Cheers
        Tim
        Signature

        Great Success is built from many little successes!

        http://www.timbrownlaw.com - My Wee Part of the World.

        http://www.LookingOverMyShoulder.com

        {{ DiscussionBoard.errors[4140632].message }}
        • Profile picture of the author Joshua Uebergang
          Hey Tim,

          Thanks for your help mate.

          What I've done to test it is Google Search something like "site:recruitshop.com.au about". As for a referring link, you can test on Human Resource Trends of 2011

          I found the site on trulocal (again using the URL) and came in via the link to the home page but I suspect you are not capturing this information if I land on a "non form" page.
          The function is on EVERY page. Just on form pages, it's inserted into a form field.

          Are the variables treated the same if they're in two functions? I should of brought this up in my original post because I suspect this has something to do with it partly working and not working. I've had to define two functions for the following page because the functions are defined in two separate include files: Recruitment Agency | Onine Recruitment | Retail Recruitment | Flat Fee Recruitment.

          So home page has function B, all other pages use function A, and that employers page has functions A and B. The functions are the same, just different names.
          {{ DiscussionBoard.errors[4236208].message }}

Trending Topics