duanli8391 2013-05-29 02:06
浏览 59

使用cookie进行登录:代码在不同的页面中不起作用

I have 2 pages that both have the same code for checking whether if someone's logged in or not. In the index page it shows the username if the cookie for username is set. But and on the second page, the login page, it redirects back to index page if the username cookie is set. Here's the if statement that checks it:

login page:

if(isset($_COOKIE['username'])){
    header("Location: ../index.php");
    die();
}

index page:

if(isset($_COOKIE['username'])){
    echo '<p style=""> '.$_COOKIE['username'].'</p>';
}else{...}

that's pretty much the code in a nutshell. in the login page the redirect works fine, I've checked it with different pages. But in the index page, even though it's the exact same if statement, it doesn't work.

Is there a problem with the code, or is it a folder/server related issue?

Thanks, O.N.

  • 写回答

1条回答 默认 最新

  • duanlinma5885 2013-05-29 02:12
    关注

    On the login page you are using a header to redirect to index.php and on the index.php you are not, so I’m not sure why you are confused?

    UPDATE

    You need to add a space on either side of your . concatenation . or remove it entirely.

    if(isset($_COOKIE['username'])){
        echo '<p style=""> ' . $_COOKIE['username'] . ' </p>';
    }else{...}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line