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

  • By
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

Incredible Demos

  • By
    Create a NoScript Compatible Select Form Element with an onChange Event

    I wouldn't say that I'm addicted to checking Google Analytics but I do check my statistics often. I guess hoping for a huge burst of traffic from some unknown source. Anyway, I have multiple sites set up within my account. The way to...

  • By
    CSS Rounded Corners

    The ability to create rounded corners with CSS opens the possibility of subtle design improvements without the need to include images.  CSS rounded corners thus save us time in creating images and requests to the server.  Today, rounded corners with CSS are supported by all of...

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!