The Core

Why We Are Here => Web Development => Topic started by: ukgimp on August 25, 2011, 04:00:07 PM

Title: Preventing PHP Session Vars appearing in Google
Post by: ukgimp on August 25, 2011, 04:00:07 PM
I have been cranking out a few sites recently.

Submitting sitemaps etc.

But I am finding that I am getting session vars indexed and visible sometimes. I have a simple tracking script that I use to see how people got to the site, which I then write to a DB on the way out.

//set tracking
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
}


While this, I know is a basic Q, how can I track but not have the session vars appearing.

Any pointers appreciated
Title: Re: Preventing PHP Session Vars appearing in Google
Post by: Rooftop on August 25, 2011, 04:06:22 PM
The easiest way is to tell google to ignore them via webmaster tools.
Alternatively do a user-agent check and don't assign the variable to robots.  Or, maybe force cookie storage, don't give the get var option.
Title: Re: Preventing PHP Session Vars appearing in Google
Post by: BoL on August 25, 2011, 06:04:59 PM
You can try setting this php.ini option to 1

session.use_only_cookies

I *think* that's the correct .ini option. If you're only interested in setting cookies and not generating session ID's in URL's then this will stop the root cause of them appearing.
Title: Re: Preventing PHP Session Vars appearing in Google
Post by: ukgimp on August 26, 2011, 11:39:48 AM
I have loads of urls indexed now, ho hum, what a bummer

Found this though, which is an easy fix:

http://davidwalsh.name/initiating-php-sessions-without-sacrificing-page-rank

I now just have to work out the regex for 301'ing something with  (i hate regex)

?PHPSESSID=d7342555795366f03577e953d87370e0

on the end and 301 to the non ? version.
Title: Re: Preventing PHP Session Vars appearing in Google
Post by: ukgimp on August 26, 2011, 12:30:24 PM
As ever, more than one way to do the deed.

I was just about to post this solution

QuoteRewriteCond %{QUERY_STRING} PHPSESSID=
RewriteRule (.*) http://www.url.co.uk/$1? [R=301,L]

But thank you sir :-)
Title: Re: Preventing PHP Session Vars appearing in Google
Post by: ukgimp on August 26, 2011, 12:42:07 PM
>>yours works better

I dont think i have heard that from you very often :-)