Grabbing Referrer / IP / Keyword Information

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markandcha
Forum Newbie
Posts: 5
Joined: Tue Aug 12, 2008 1:56 pm

Grabbing Referrer / IP / Keyword Information

Post by markandcha »

Hi.

First time post, hope you can help..

I manage several domains, most with highly ranked .html landing pages in Google, which when the form is submitted a .php file is called which performs very basic validation :oops: , + fires an email to me and my client. I would love to add at the bottom information about the referring domain / keyword used and maybe the IP..

I have found some code for the landing page, but I don't know if I can just insert it into the .html code, and where, aobove the DOCTYPE, or in the head, or body.

I don't know whether I should change the file extension of the .html files to .php, because then the SEO ranked pages wont be loaded/work until they are re-indexed.


< ?php
session_start();
if(!isset($_SESSION[’referrer’])){
//get the referrer
if ($_SERVER[’HTTP_REFERER’]){
$referrer = $_SERVER[’HTTP_REFERER’];
}else{
$referrer = “unknown”;
}
//save it in a session
$_SESSION[’referrer’] = $referrer; // store session data
}
?>

Then on the .php email/form file I could insert this

session_start();
echo “referrer = “. $_SESSION[’referrer’]; //retrieve data
?>

How could I get the keywords and IP?

Thanks..
markandcha
Forum Newbie
Posts: 5
Joined: Tue Aug 12, 2008 1:56 pm

Re: Grabbing Referrer / IP / Keyword Information

Post by markandcha »

oh and I don't know if you can toughen up my validation? it's very basic..

I would like som pointers towards UK telephone number validation and also preventing rogue email addresses, I get a lot from .ru

// validation
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
//if (Trim($Mobile_Tel)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}



// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
$success = mail($EmailFrom, $Subject, $Body, "From: <$Email_Mail>");

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}

Thanks..
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Grabbing Referrer / IP / Keyword Information

Post by flying_circus »

markandcha wrote: How could I get the keywords and IP?
Unless the keywords are passed in the querystring of the $_SERVER['REFERER'] value, I'm not sure you CAN get them.

For the IP, you can use $_SERVER['REMOTE_ADDR'];

if you want to see ALL (most?) of the $_SERVER superglobal vars, create a new file called phpinfo.php and paste this into it:

Code: Select all

<?php
  phpinfo();
?>
Navigate to the page and scroll towards the bottom.
markandcha
Forum Newbie
Posts: 5
Joined: Tue Aug 12, 2008 1:56 pm

Re: Grabbing Referrer / IP / Keyword Information

Post by markandcha »

thanks for that , looking at it it might be called "QUERY_STRING"..

Can I still use this code at the top of a html page...?
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Grabbing Referrer / IP / Keyword Information

Post by flying_circus »

markandcha wrote:Can I still use this code at the top of a html page...?
No, the page must have a .php extension and your webserver must parse php files.

Other than that, you can place your PHP code anywhere inside the file, since it doesnt produce output. If your code DOES produce output, then you want to place it within the <BODY> of the page.
markandcha
Forum Newbie
Posts: 5
Joined: Tue Aug 12, 2008 1:56 pm

Re: Grabbing Referrer / IP / Keyword Information

Post by markandcha »

i found this code on the net, not quite sure on how to integrate/call it though

PHP function to get search query

function get_search_phrase($referer){

$key_start = 0;
$search_phrase = "";

// used by dogpile, excite, webcrawler, metacrawler
if (strpos($referer, '/search/web/') !== false) $key_start = strpos($referer, '/search/web/') + 12;

// used by chubba
if (strpos($referer, 'arg=') !== false) $key_start = strpos($referer, 'arg=') + 4;

// used by dmoz
if (strpos($referer, 'search=') !== false) $key_start = strpos($referer, 'query=') + 7;

// used by looksmart
if (strpos($referer, 'qt=') !== false) $key_start = strpos($referer, 'qt=') + 3;

// used by scrub the web
if (strpos($referer, 'keyword=') !== false) $key_start = strpos($referer, 'keyword=') + 8;

// used by overture, hogsearch
if (strpos($referer, 'keywords=') !== false) $key_start = strpos($referer, 'keywords=') + 9;

// used by mamma, lycos, kanoodle, snap, whatuseek
if (strpos($referer, 'query=') !== false) $key_start = strpos($referer, 'query=') + 6;

// don't allow encrypted key words by aol
if (strpos($referer, 'encquery=') !== false) $key_start = 0;

// used by ixquick
if (strpos($referer, '&query=') !== false) $key_start = strpos($referer, '&query=') + 7;

// used by aol
if (strpos($referer, 'qry=') !== false) $key_start = strpos($referer, 'qry=') + 4;

// used by yahoo, hotbot
if (strpos($referer, 'p=') !== false) $key_start = strpos($referer, 'p=') + 2;

// used by google, msn, alta vista, ask jeeves, all the web, teoma, wisenut, search.com
if (strpos($referer, 'q=') !== false) $key_start = strpos($referer, 'q=') + 2;

// if present, get the search phrase from the referer
if ($key_start > 0){
if (strpos($referer, '&', $key_start) !== false){
$search_phrase = substr($referer, $key_start, (strpos($referer, '&', $key_start) - $key_start));

} elseif (strpos($referer, '/search/web/') !== false){

if (strpos($referer, '/', $key_start) !== false){
$search_phrase = urldecode(substr($referer, $key_start, (strpos($referer, '/', $key_start) - $key_start)));
} else {
$search_phrase = urldecode(substr($referer, $key_start));
}

} else {
$search_phrase = substr($referer, $key_start);
}
}

$search_phrase = urldecode($search_phrase);
return $search_phrase;

} // end get_search_phrase
markandcha
Forum Newbie
Posts: 5
Joined: Tue Aug 12, 2008 1:56 pm

Re: Grabbing Referrer / IP / Keyword Information

Post by markandcha »

i guess i shd have coded this in when i did the site..

i'll have to change all pages to .phpfrom .html and take a seo hit..shame.
Post Reply