Hello!
My first post on SitePoint, and many thanks to anyone who can help me out here, whether it be direct code, or even a point in the right direction!
My problem: I need to convert a server-side PHP call tracking script into client-side JavaScript so that it will function in any language (ASP, .NET, HTML, etc.) I will post the finished PHP script below and do my best to comment each section clearly, but please don’t hesitate to ask if anything is unclear.
The functionality: The basic function of this script is to display different phone numbers on a website, depending on how the user enters the site. For example, if a user enters the site from a search engine, 111-111-1111 will be displayed, but if the user enters from direct URL entry, 333-333-3333 will be displayed. There are a few basic scenarios that will need to be met, which I will list here:
- If a specified query is in the URL, display number 000-000-0000.
- If the user comes from a search engine and the client name is in the query string, display number 111-111-1111.
- If the user comes from a search engine and the client name is NOT in the query string, display number 222-222-2222.
- Otherwise, display number 333-333-3333.
I have a pretty good understanding of PHP and was able to create the script below from scratch, with help of course from W3Schools, PHP.net, and various other awesome tutorials throughout the 'net. I also have a lot of experience with ActionScript, which seems to have a lot in common with JS, but this is my first attempt at JS and I am still trying to wrap my head around most of it.
If any of you JavaScript experts out there could help me out with converting some of the functions (strstr, parse_url, etc.), and any other best practices, I would be so grateful!
I will be continuing to work hard on this all week, and will update this thread with my progress.
Thank you all in advance for your help!
<?php
/* Parse the referring url and, if it is one of the search engines
listed, return the specified name. */
function GetReferringHost() {
$refer = parse_url($_SERVER['HTTP_REFERER']);
$host = $refer['host'];
if (strstr($host,"google")) {
return "Google";
} elseif (strstr($host,"yahoo")) {
return "Yahoo";
} elseif (strstr($host,"bing")) {
return "Bing";
} elseif (strstr($host,"aol")) {
return "AOL";
} elseif (strstr($host,"ask")) {
return "Ask";
} elseif (strstr($host,"altavista")) {
return "AltaVista";
}
}
/* Parse the referring url query string and, if specified
ClientName keyword is present, return "ClientName". */
function CheckQueryString() {
$refer = parse_url($_SERVER['HTTP_REFERER']);
$query = $refer['query'];
if (strstr($query,"clientname")) {
return "ClientName";
}
}
/* Check if the referring host is a search engine (called
"natural" here) and if so, return "Natural". */
if (isset($_SERVER['HTTP_REFERER'])) {
$refHost = GetReferringHost();
if ($refHost == "Google" || "Yahoo" || "Bing" || "AOL" || "Ask" || "AltaVista") {
$refHost = "Natural";
}
$refKey = CheckQueryString();
}
/* If the query string contains a direct referral, return
number 0, if the referring host is "Natural" (i.e., a search
engine) and "ClientName" is NOT present, return number 1,
if the "ClientName" IS present, return number 2, and
otherwise return number 3. */
if ($ref == "DirectReferralSite.com") {
$num = "000-000-0000";
} elseif ($refHost == "Natural" && $refKey <> "ClientName") {
$num = "111-111-1111";
} elseif ($refKey == "ClientName") {
$num = "222-222-2222";
} else {
$num = "333-333-3333";
}
/* Check for a cookie or set a cookie for the phone number. */
if (isset($_COOKIE['user'])) {
$num = $_COOKIE['user'];
} else {
$expire = time()+60*60*24*30*6; // 6 months
setcookie("user",$num,$expire);
}
?>