开发者

Php sessions aren't working

I am new to php, and very new to sessions, so I have no idea what I am doing wrong. I followed the tutorial on tizag, and put this code on my site:

<?php

session_start();

echo SID . "<br><br>";

if(isset($_SESSION['views'])) {
    $_SESSION['views'] = $_SESSION['开发者_如何转开发views'] + 1;
} else {
    $_SESSION['views'] = 1;
    echo "views = ". $_SESSION['views']; 
}

?>

The SID changes whenever I refresh, and the number does not count up.

Update: Url: http://121.73.150.105/PIA/

FIXED BY: Putting session_start() before my doctype, title etc.


Are cookies enabled in you're browser ? phpsessid is stored as a cookie , you can set different parameters for it , one that could be usefull in you're case could be session_get_cookie_params() , and see if everithing is oki with the session cookie params .

If anything is wrong like expiration date you can set the params with session_set_cookie_params()


Your PHP setup may have been configured to not save sessions in cookies.

To verify if this is the case, you can take a look at session.use_cookies in your php.ini, or using ini_get, like so:

<?php echo ini_get('session.use-cookies'); ?>

You can correct it at runtime, as well, using ini_set, like so:

<?php ini_set('session.use-cookies', '1'); ?>


either you are outputting something to the browser before calling session start, or you have cookies disabled.


You don't output the $_SESSION['view'] after the if statement. I think that's why it doesn't change.

Try:

<?php

session_start();

echo SID . "<br><br>";

if(isset($_SESSION['views'])) {
    $_SESSION['views'] = $_SESSION['views'] + 1;
} else {
    $_SESSION['views'] = 1;
}

echo "views = ". $_SESSION['views'];     

?>

So you always output the new $_SESSION['views'] value.

EDIT: I think the right answer is that the session is not set. But I'm curious, how can the code always outputs "view = 1"? Can I open a new question referencing this question or just discuss it here?


in your code if you cant see session ID you can write session_id() in place of SID.


ini_set("session.use_cookies",1);
ini_set("session.use_only_cookies",1);

this two parameter must set to gether if you want it work

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜