php
  • session
  • variables
  • cookies
  • 2012-04-29 49 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()之後;因爲腳本並不總是在用戶重定向到新頁面後立即結束。

    相關問題