doufang2228 2015-07-10 01:23
浏览 39

php $ _SESSION不起作用[重复]

This question already has an answer here:

I'm trying to use sessions for learning purposes but it's not working. I got a login page which passes by POST "uname" and "passw" to this page:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<?php session_start(); ?>
<head>
...
if($_POST['uname']=="admin" && $_POST['passw']=="correctpassword"){
$_SESSION['login']="ok";}    
if($_SESSION['login']=="ok"){
echo "<p>Logged in</p>";
} else {
echo "<p>Nothing</p>";
}
...
</html>

And it works until here. Then when I pass on another page:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<?php session_start(); ?>
<head>
...
if($_SESSION['login']=="ok"){
echo "<p>Logged in</p>";
} else {
echo "<p>Nothing</p>";
...
</html>

and here it doesn't work.

I'm working on MAMP and Safari.

Can someone help me please?

</div>
  • 写回答

1条回答 默认 最新

  • douqiao8370 2015-07-10 01:24
    关注

    place the <?php session_start(); ?> on the top of the page without any space before

    Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来