Initiating PHP Sessions Without Sacrificing Your Page Rank

By  on  

This post was authored by Mark Sanborn. To learn more about Mark, click here.

A while back I had been working on a website project that required an authentication login system. In order for the login system to properly operate every page of my site needed to check to see if there was a session created and the user was logged in. Some of you may know that in PHP you cannot view session variables without in fact initiating a session. So, for example if you wanted to provide a simple, "you are logged in as Mark click here to logout." on each and every page you would have to actually start a session to display this message properly.

At first this task may seem trivial. The code to start a session is one line:

session_start();

After changing an existing website that was already indexed in Google quite well to include sessions I was shocked to find out what effect this might have in the Google index. A few weeks after making the change most if not all of my pages were no longer indexed by Google and for awhile I had no idea what was causing it.

After awhile I would notice that sometimes my URLs were taking on an additional string of numbers. They would look like this:

http://www.yourdomain.com/?PHPSESSID=61ca9c60b10cc8f481ac9c1eacbee797

When I saw this I immediately knew this was the reason Google was dropping my pages. This string of numbers inherently does two drastic SEO mistakes. For one this number changes every time you go to the site. What this means is that Google thinks it is a different URL with the same content as the last URL it indexed. Many know that Google penalizes pages for duplicate content. This extra string of numbers essentially makes your entire website a bunch of duplicates.

If that isn't enough already it also makes another SEO mistake. Google doesn't weigh pages that appear to be static the same as they do a page that appears to be a one time randomly generated page. To Google the above URL appears to be a page only meant to be seen by one person. Google gives less indexing weight to it.

So what did I do to fix this issue?

Well after a little bit of searching I found that this is actually a common problem and there is a simple solution to fix it. I sure wish I knew about this before I sacrificed my page rank and indexing. To save you the trouble in the future please learn from my mistake and use the following code before starting any session variable.

//These commands must be set BEFORE the session is started
ini_set('session.use_trans_sid', false);
ini_set('session.use_only_cookies', true);
ini_set('url_rewriter.tags', '');
// start session
session_start();

Moral of the story?

Be very careful what you do to your pages your Google indexes may count on it.

About Mark Sanborn

Hi, I am Mark Sanborn. I am 22 and just graduated from the University of Montana. I have had an interest in computers for as long as I can remember. I still remember the DOS days and really started getting into computers when Windows 3.1 was around. I made my first webpage in the 7th grade. My passion for web design and computers has only progressed from there.

How did I learn to program?
In college I was approached by a group that wanted a realty website created for the University business plan competition. I gladly took the job and forced myself to learn PHP/mysql while working on the project. By the time the project was complete I was fairly fluent in PHP and was confident I could build pretty much anything I set my mind to. I later used these skills to create a corporate website with a custom ecommerce shopping cart for a client.

Computer Related Education
I just received my B.S. in Business Administration Information Systems. I scored nearly a perfect score on the MCP test allowing my to achieve the Microsoft Certifed Professional status. While attending college I worked for the University as tech support for dorm students and faculty. I have a lot of experience working with computers but there is still a lot I can learn from others. I also believe that you learn the most by teaching it to others. This is why I created my own blog.

What Languages Do You Use?
HTML, CSS, MySQL, PHP, Java, XML, Bash. For some reason I want to learn python yet I don't know what I would use it for. Some experience in: ASP, Visual Basic, ColdFusion.

What Do You Do In Your Spare Time?
I spend a lot of time in front of the computer but I am not your typical programmer that loves Star Trek and converses with their friends about what to do in the event of zombie attacks. All stereotypes aside, I love to play guitar I am interested in all types of music, mostly blues and classic rock. I am an avid weight lifter and practice Judo. I don't consider myself a fan of team based sports; however, I love individual sports. Last summer I learned to paraglide and for the past two winters I have been enjoying the fabulous sport of kiteboarding on snow.

About

Recent Features

Incredible Demos

  • By
    Create a Dynamic Flickr Image Search with the Dojo Toolkit

    The Dojo Toolkit is a treasure chest of great JavaScript classes.  You can find basic JavaScript functionality classes for AJAX, node manipulation, animations, and the like within Dojo.  You can find elegant, functional UI widgets like DropDown Menus, tabbed interfaces, and form element replacements within...

  • By
    Morphing Elements Using MooTools and CSS

    Morphing an element between CSS classes is another great trick the MooTools JavaScript library enables you to do. Morphing isn't the most practical use of MooTools, but it's still a trick at your disposal. Step 1: The XHTML The block of content that will change is...

Discussion

  1. Very good quality comment above :P, someone hates you.

    You say:

    # //These commands must be set BEFORE the session is started
    # ini_set('session.use_trans_sid', false);
    # ini_set('session.use_only_cookies', true);
    # ini_set('url_rewriter.tags', '');
    # // start session
    # session_start();
    

    But this assumes that googlebot uses cookies, how can you be sure it uses cookies ?

    The best way is not to use sessions at all, why would you need sessions in public space ?

    I recommend to use sessions just when they are absolutely needed (like displaying content based on user login)

  2. I think adding the following line to your .htaccess does the same thing:

    php_flag session.use_trans_sid off

  3. Aloha! I thought I would drop a quick comment since I’ve spent the greater part of the last half hour perusing through your site posts. Now i’m always amazed at the good quality of writing that I can discover on the web by punching the “I’m Feeling Lucky” option on Google! Well that’s all I have to say! Many thanks and very nice to ‘meet’ you :)

  4. Sylvin (France)

    Good article ! However how to be sure that cookies are enabled in the user’s browser ? Is there a way to test this ?
    Thanks

    Sylvin

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!