开发者

session id value changes

I am writing a PHP website. I have a $_SESSION['id'] that hold the username id of a user, which I will use this value later on in my scripting. However, I notice that the ID changes as I surf the site.

Is id reserved开发者_如何学Python in any way? Or, should I change id to something else?

This problem is happening whenever I click on a button several times. I am 100% sure that I'm not altering $_SESSION['id'] beyond the initial login.

Could someone please help me understand what is happening, and how it can be fixed.


I am going to assume you already use session_start, if not see the other comments :P

If you have register globals on, you may be seeing behavior like that if you use the variable $id in your code. As a test, try:

<?php
session_start();
$_SESSION['testing'] = 'Foo';
$testing = 'bar';
die($_SESSION['testing']);
?>

Reload the page a couple times. If bar prints out rather than Foo, that may be your problem. You'll probably want to make the session variable name something that is less likely to get used elsewhere, or (more correct) turn off register globals.


make sure you start the session - look at session_start();


Make sure you are calling session_start() at the very top of the page(aka before ANYTHING else).

This may be causing the screw up.

A very quick rundown as to why you need to do this:

Most of the time a session id(different from $_SESSION['id'], don't worry) is stored via cookie. The cookie values are sent via the response headers, which needs to be the first thing that is sent back to the client. So if you are outputting ANYTHING before calling session_start(), you are actually forcing php to send the response headers. This will cause the response headers to NOT contain the cookie session value, thus not being able to properly use the session.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜