is PHP $_SESSION variable a cookie?

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

I want to set a session variable in case cookies are disabled.

Is the session variable also a cookie?

When I use the Firefox Web Developer tool to disable all cookies, or delete the path cookies, the session does not work either.

http://netsperience.org/age_check/index.htm

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Yes, the default method is by storing a cookie, but the cookie only has the session ID, not the contents of the $_SESSION variable. There are alternate ways to keep the session alive. More information here: http://us.php.net/manual/en/session.idpassing.php

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

I think you meant the section about saving the session info in a database

That is not really an option here.

If I turn off accept cookies in Firefox options, the session does not work either.

Any suggestions for setting some kind of flag without cookies? AJAX? (what if JavaScript is turned off?)

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I think you meant the section about saving the session info in a database

Nope, not what I was talking about. The manual page I linked to talked about an alternate way to keep the session alive, which is by passing the session ID manually. To get the 'PHPSESSID=asfi76as8f76asd' parameter, use the SID constant, or to just get the session id by itself, use the session_id() function.

There is an easier way to do all of that...with --enable-trans-id enabled, the session ID is automatically passed either in any link clicked, or as a hidden form field in a submitted form.

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

ok ic - the session ID is passed in the URL instead of stored in the cookie

session.use_trans_sid boolean
session.use_trans_sid whether transparent sid support is enabled or not. Defaults to 0 (disabled).

Note: For PHP 4.1.2 or less, it is enabled by compiling with --enable-trans-sid. From PHP 4.2.0, trans-sid feature is always compiled. URL based session management has additional security risks compared to cookie based session management. Users may send a URL that contains an active session ID to their friends by email or users may save a URL that contains a session ID to their bookmarks and access your site with the same session ID always, for example.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

yeah, unfortunately making for ugly URL's.

Dare I mention ... turning register globals ON ? ...
*runs away and hides waiting for the flurry of comments*
This of course wont work as of PHP 6 anyway, so it's not a good idea.

Other options:
You could use a DB, or a file to store some temp data, but that all starts getting complex having to check when users leave the site etc.

Use a form with hidden-type data and forward it each time, GET still has data in the url of course, and I keep well away from post data wherever possible as the pop-up is annoying.

I had this issue with my last client, they wanted a major visitor tracking system with time/date/page and visitors city or town.
I decided to just show a message if the user didn't have cookies turned on.
"Sorry, to use this site you need to turn on cookies, they are safe .. yadda yadda"

The number of people without cookies is low, and even then a high percentage of those people will know they have turned them off and will just stick them back on for your site if they wish/require.

You could incorporate two methods.
Test if user has cookies, if yes just set cookie as required, if no use GET.
How your site would deal with the choice of GET or cookies depends on the site. What functions, pages etc you have.

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

greg wrote:
Use a form with hidden-type data and forward it each time, GET still has data in the url of course, and I keep well away from post data wherever possible as the pop-up is annoying.

what pop-up? I use post on forms all the time I have not seen any pop-up.

greg wrote:
I decided to just show a message if the user didn't have cookies turned on.
"Sorry, to use this site you need to turn on cookies, they are safe .. yadda yadda"

There's a great page on Google that explains how to turn on cookies in various browsers - I have decided to display a link to this page if cookies are disabled. (yeah, and I just had a bright idea to use some browser sniffer code to display the appropriate instructions - maybe next time, or I can add it to the project when I get a chance)

Since I posted this I had to rework the code because of license issues, so I hope I "improved" it.

I also considered a db (cumbersome - from where I plugged into the project, I do not think a db exists for the site and getting authorization etc was more complicated than it was worth) or a flat file (not so good when there are a lot of visitors) but I finally decided to stay with cookies.

@pr0gr4mm3r - I do not want to add anything to the URL, putting sessions in the URL is an SEO no-no.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

decibel.places wrote:
greg wrote:
I keep well away from post data wherever possible as the pop-up is annoying.

what pop-up? I use post on forms all the time I have not seen any pop-up.

When you use browser back button to a page that had data POSTed to it, you get "The page you are trying to view contains POSTDATA... etc"
You must have SURELY had that before.

decibel.places wrote:
I do not want to add anything to the URL, putting sessions in the URL is an SEO no-no.
This is becoming less of an issue these days, certainly with Google. SE's are always improving their systems, and as URL data is very useful, if not a necessity in some cases, they are always trying to improve to incorporate this.
Although static URL's are easier to index, they can index dynamic ones. Statics ones 'in general' may get a better rank, but the difference between them is decreasing all the time.

Just don't try to mask them (mod_rewrite etc) or hide them or make them appear to be static in any way, and keep them as short as possible,
I don't think it's a "no-no" though, especially if you need to use them.
Of course, don't just use them for the sake of it when it's avoidable, but if you need to use them, use them!

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Just don't try to mask them (mod_rewrite etc) or hide them or make them appear to be static in any way, and keep them as short as possible,

mod_rewrite would be a bad idea. Google will visit your site one day and be sent to example.com/page/df98g76adf986asd8f5as8g5d9f8g one day, and then when it returns, it will be directed with a different session id and be something like example.com/page/vb8966sd865sd8fs9gh79sdf - big problem with duplicate content.

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

greg wrote:
When you use browser back button to a page that had data POSTed to it, you get "The page you are trying to view contains POSTDATA... etc"
You must have SURELY had that before.

Yeah, reloading a page that has data posted asks you if you want to repost the data.
1. Why would the user be using the BACK button if your navigation is clear and functional? If the user habitually uses the browser buttons for nav s/he will be used to this message.
2. In this case the PHP page that gets posted to is an interstitial page that does not get displayed and sends a fresh header location. Clicking on the BACK button after verification brings up the verification form, not the PHP processing URL.

greg wrote:
decibel.places wrote:
I do not want to add anything to the URL, putting sessions in the URL is an SEO no-no.
This is becoming less of an issue these days, certainly with Google.

As pr0gr4mm3r pointed out, temporary tokens such as a session id in the URL can cause confusion for search bots, as well as a problem if the user bookmarks that page and tries to return to an expired session.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

decibel.places wrote:
1. Why would the user be using the BACK button if your navigation is clear and functional?
You're asking me to speak on behalf of potentially a billion people and explain their habits?
People use it. I use it. Having a pop-up on pages each time you click back that doesn't go away unless you choose an answer is a pain, and a confusing technical question to some.

decibel.places wrote:
If the user habitually uses the browser buttons for nav s/he will be used to this message.
Hmm, that's the attitude that allows industry standards and customer service to drop like a dead overweight rhino from space.
Just because people are accustomed to an annoyance doesn't alleviate any of their frustration when it happens to them on your site.

decibel.places wrote:
2. In this case the PHP page that gets posted to is an interstitial page...
Which is exactly how I avoid sending POST data with headers.
But that only allows for checking of user input from page A to page B (and potentially a Page C from redirect on Page B).
It doesn't resolve your issue of checking on each page if the user has entered their age.

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

greg wrote:
decibel.places wrote:
2. In this case the PHP page that gets posted to is an interstitial page...
Which is exactly how I avoid sending POST data with headers.
But that only allows for checking of user input from page A to page B (and potentially a Page C from redirect on Page B).
It doesn't resolve your issue of checking on each page if the user has entered their age.

Actually, the other pages have a JavaScript file loaded that does the checking and redirects to the verification page if cookies are enabled but the verification cookie is missing. Without cookies, the verification page redirects directly to a specific page without posting the form.

And if cookies are enabled and the BACK button is used, the posted-to page does not display at all, the trail leads back to the verification page...

So in this application, the issues with POST are avoided.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

What if they have JS disabled? Sticking out tongue

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

greg wrote:
What if they have JS disabled? :tongue:

yeah, and what if they are using a screen reader, or a 14.4 kbps phone modem? Evil

In this case, if JS is disabled, they will not be directed to the verification page... I believe that "best effort" has been made to secure self-verification of age (and there is nothing preventing a 10 year old from averring he is 30).

greg's picture

He has: 1,581 posts

Joined: Nov 2005

decibel.places wrote:
I believe that "best effort" has been made..
Yeah, that's all you can do.

decibel.places wrote:
there is nothing preventing a 10 year old from averring he is 30..
Well, apart from the parents, but that's another argument altogether..

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

I added a link that will only display if js is disbled -

click here if you are 21 or older to enter the site

I think that should cover it...

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Well, I'm definitely glad it's all sorted, 'cos it's getting a bit squashed here now..

--------> <--------

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

There should be an indent limit.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I wonder if we could get it down to one word per line?

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I bet it's possible.

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

is this the equivalent of forum "limbo"? How low can you go? Sticking out tongue

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

what_happens_when_a_word_is_too_long_anyway?

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

pr0gr4mm3r wrote:
what_happens_when_a_word_is_too_long_anyway?

looks like it gets clipped...

but edit/quote still shows the text

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

taking it down another notch

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

I think this will be one word per line - maybe not short words

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

now it should be one word per line

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

can we take it all the way to the vanishing point, like drawing perspectives?

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

aha, fluid layout adjusts to container

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I'm sorry ... did you say something?

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

I commented on how the layout switched from horizontal to vertical

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Dare I mention ... turning register globals ON ? ...
*runs away and hides waiting for the flurry of comments*

Yeah, you better run. Wink When the session ID is included in the URL, there is nothing the developer has to do to fetch that ID, it's handled by PHP.

They have: 10 posts

Joined: May 2009

Session state variables if uses cookie as its underlying mechanism then its a big flaw in PHP i am ASP.NET developer and clearly both are distinct things. Session uses the server memory while a cookie uses the clients computer memory. I think you faced this problem due to some configuration maybe in which the server memory is given the priority. I dont know it sounds so very unfair.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I believe by default PHP will use some memory, referencing the sess_id (maybe) but regardless, what's wrong with using files and user storage?
I mean, why waste precious RAM Wink

There are options in PHP to use any of the three states.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.