php
  • session
  • variables
  • cookies
  • 2012-04-29 156 views 2 likes 
    2

    我按照以下步骤设置了会话,因为echo会出现,所以这是有效的。但是当我进入下一页或另一页时,会话不在那里?我究竟做错了什么?正在丢失Php会话

    $session_start(); 
    
    if ($username==$dbusername&&$password==$dbpassword) 
        { 
         echo"<b>Login Successful</b><br><a href='systemadmin.html'><br>Click here to access the <strong>System Admin Page</strong></a>"; 
         $_session['username']=$dbusername; 
         if($username == "admin") 
         { 
          $_session['admin'] = true; 
         } 
    

    我试图让下面的这些会议的工作:

    <?php 
    session_start(); 
    if($_session['admin'] == true) 
    { 
    // do nothing 
    }else{ 
        header('Location: home.html') ; 
    } 
    
    ?> 
    

    更新:

    大写会议工作,但现在的会话了摧毁的arent当我使用logout.php

    <?php 
    
    session_start(); 
    
    session_destroy(); 
    
    header("location: home.html"); 
    
    ?> 
    

    回答

    5

    $_session应该是=>$_SESSION

    http://php.net/manual/en/reserved.variables.session.php

    的首部作品,因为你设置一个“正常”变量(可用于要求)。

    UPDATE

    摧毁会议:

    <?php 
    // Initialize the session. 
    // If you are using session_name("something"), don't forget it now! 
    session_start(); 
    
    // Unset all of the session variables. 
    $_SESSION = array(); 
    
    // If it's desired to kill the session, also delete the session cookie. 
    // Note: This will destroy the session, and not just the session data! 
    if (ini_get("session.use_cookies")) { 
        $params = session_get_cookie_params(); 
        setcookie(session_name(), '', time() - 42000, 
         $params["path"], $params["domain"], 
         $params["secure"], $params["httponly"] 
        ); 
    } 
    
    // Finally, destroy the session. 
    session_destroy(); 
    ?> 
    

    http://php.net/manual/en/function.session-destroy.php#example-4368

    Additionaly你应该总是使用exit();你做一个重定向阻止脚本的进一步执行之后。

    +0

    这个工程,但我有一个新问题 – Anicho

    +1

    @Anicho看到我的更新 – PeeHaa

    +0

    谢谢repwhoringpeehaa – Anicho

    0

    超全球的名字是$_SESSION大写字母。尝试改变它,看看它是否有帮助。

    2

    PHP服务器/会话/全局变量区分大小写。对于PHP而言,$_SESSION$_session不是同一个变量,尽管对于英文而言,他们似乎是。您必须使用$_SESSION而不是$_session才能按照您的预期访问PHP会话变量。

    2

    你必须使用exit();在header()之后;因为脚本并不总是在用户重定向到新页面后立即结束。

    相关问题