Php 会话不起作用
我对 php 很陌生,对会话也很陌生,所以我不知道我做错了什么。我按照tizag上的教程进行操作,并将以下代码放在我的网站上:
<?php
session_start();
echo SID . "<br><br>";
if(isset($_SESSION['views'])) {
$_SESSION['views'] = $_SESSION['views'] + 1;
} else {
$_SESSION['views'] = 1;
echo "views = ". $_SESSION['views'];
}
?>
每当刷新时,SID都会更改,并且数字不会累加。
更新:网址:http://121.73.150.105/PIA/
修正:将 session_start() 放在我的文档类型之前、标题等
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的浏览器启用了 cookie 吗? phpsessid 存储为 cookie,您可以为其设置不同的参数,在您的情况下可能有用的参数可能是 session_get_cookie_params() ,并查看会话 cookie params 是否一切正常。
如果有任何错误,例如过期日期,您可以使用 设置参数session_set_cookie_params()
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()
您的 PHP 设置可能已配置为不在 cookie 中保存会话。
要验证是否是这种情况,您可以查看 session.use_cookies 在你的 php.ini 中,或者使用 ini_get,如下所示:
您也可以使用 ini_set,如下所示:
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:
You can correct it at runtime, as well, using ini_set, like so:
您要么在调用会话开始之前向浏览器输出某些内容,要么禁用了 cookie。
either you are outputting something to the browser before calling session start, or you have cookies disabled.
您不会在 if 语句后输出
$_SESSION['view']
。我认为这就是它不会改变的原因。尝试:
因此您始终输出新的
$_SESSION['views']
值。编辑:
我认为正确的答案是会话未设置。但我很好奇,代码怎么总是输出“view = 1”?我可以打开一个引用此问题的新问题或只是在这里讨论它吗?
You don't output the
$_SESSION['view']
after the if statement. I think that's why it doesn't change.Try:
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?
在你的代码中,如果你看不到会话 ID,你可以写
session_id()
代替SID
。in your code if you cant see session ID you can write
session_id()
in place ofSID
.<代码>
ini_set("session.use_cookies",1);
ini_set("session.use_only_cookies",1);
如果你想让它工作,这两个参数必须设置为一起
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