douzhi3667 2014-04-12 22:29
浏览 80
已采纳

全局$ _SESSION变量

I'm trying to make a login session on PHP, but it appears that the $_SESSION['username'] dies inside the IF sentence (I thought $_SESSION where globals by default), and I cant echo it out of the IF

heres My code

if($name=="admin" && $password=="admin")
{
session_start();
$_SESSION['username'],$_SESSION['sesion'];
$_SESSION['username']=$name;
$_SESSION['sesion']=1;
echo $_SESSION['username'];
echo "<br>";
echo $_SESSION['sesion'];
}
echo "<br>";
echo $_SESSION['username'];

The last echo doesnt print its VALUE, So when I redirect it to another page, the page doesnt take the username value

I'm kind of new in this matter So dont be so harsh on me :P How can I do this??

  • 写回答

3条回答 默认 最新

  • douzhang8840 2014-04-12 22:30
    关注

    Move session_start() to the top of the file:

    // foo.php
    <?php
      session_start();
    
      //....
    
      if($name=="admin" && $password=="admin")
      {
        // $_SESSION['username'],$_SESSION['sesion']; // Remove this line
        $_SESSION['username']=$name;
        $_SESSION['sesion']=1;
        echo $_SESSION['username'];
        echo "<br>";
        echo $_SESSION['sesion'];
      }
      echo "<br>";
      echo $_SESSION['username'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于用abap来解决动态规划的问题,但是要求输出索引值,这个是难点
  • ¥15 在ISIS中什么是IP从地址
  • ¥15 压测时,并发量过高时,响应时间出现尖刺
  • ¥15 关于vmprotect3.8.4虚拟文件一项
  • ¥15 在不用IT调试的情况下怎样能连外网
  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面
  • ¥15 CCS离散化传递函数与仿真不一致
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法