2012-08-29 195 views
-2

我從我的數據庫中使用用戶名和密碼作爲會話變量,但是當我註銷時,我仍然可以通過瀏覽器的直接鏈接訪問受保護的頁面,可能是導致此問題的原因。 這裏是我的login.php:低於我無法銷燬會話

//initialize the variables 
      $username=""; 
      $password=""; 
      $_SESSION['username']=""; 
      $_SESSION['password']=""; 
      if(isset($_POST["submit"]) && @$_GET["username"] !==""){ 
       $username=$_POST["username"]; 
       $password=$_POST["password"]; 
       if(isset($_POST["username"]) && $_POST["username"]!=="" && isset($_POST["password"]) && $_POST["password"]!==""){ 
        //sucuring the data 
        $username=htmlentities(mysql_real_escape_string(trim($_POST["username"]))); 
        $password=htmlentities(mysql_real_escape_string(trim($_POST["password"]))); 
        //checking if user does exist 
        $sql="SELECT email, password FROM ".$db_name.".user WHERE email=\"".$username."\" AND password='".md5($password)."' LIMIT 1"; 
        $query=mysql_query($sql,$con); 
        $result=mysql_fetch_assoc($query); 
        //check query to c if is successfully optional 
        if(!$result){ 
         print"No result"; 
        }else{ 
         //if combination found in our database then register session values"; 
         $_SESSION['username']=$_POST['username']; 
         $_SESSION['password']=md5($_POST['password']); 
         //check location 
         $sql="SELECT location FROM ".$db_name.". user WHERE email ='".$_POST['username']."' LIMIT 1"; 
         $query=mysql_query($sql,$con); 
         $result=mysql_fetch_array($query); 
         //no need of loop since we want only one field/single record/row 
         $location=$result['location']; 
         header("Location:".$location.""); 
        } 
       }else{ 
       //do nothing 
       } 

      } 

     ?> 
     <form id="loginFrm" method="post" action="?lgn=getin"> 
      <fieldset> 
       <legend> 
        Inshuz Login 
       </legend> 
       <table> 
        <tr> 
         <td> 
          Username 
          <div id="specify">Your email</div> 
         </td> 
         <td> 
          <input type="text" name="username" size="40" class="text" value="<?php print $username; ?>"> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          Password 
         </td> 
         <td> 
          <input type="password" name="password" size="40" class="text" value="<?php print $password; ?>"> 
         </td> 
        </tr> 
        <tr> 
         <td colspan="2"> 
          <input type="submit" name="submit" class="btn" value="Login"> 
         <td> 
        </tr> 
       </table> 
      </fieldset> 
     </form> 

這個login.php中都包含在我的index.php是主頁

<?php session_start(); require_once("includes/functions/url.php"); require_once("includes/config/config.php");?> 
    <html> 
     <head> 
      <title> 

      </title> 
     <head> 
     <link rel="stylesheet" media="all" type="text/css" href="css/main.css"/> 
     <script type="text/javascript" src="js/jquery-1.8.0.js"></script> 
     <body> 
       <div id="wrapper"> 
        <div id="header"> 
         <div id="nav"> 
          <a href="#">Home </a> | <a href="#">About us</a> | <a href="#">Products</a> | <a href="#">Services</a> | <a href="#">Carrers</a> 
         </div> 
        </div><!--end of header--> 
        <div id="mainContent"> 
        <div id="RighContent"> 
         <?php require_once("includes/pages/"[email protected]$page);?> 
        </div><!---RightCont---> 
        <div id="LeftCont"> 
         afafhkashf 
        </div><!---leftcont---> 
        </div><!---end of maincontent--> 
        <div id="footer"> 

        </div><!--end footer--> 
       </div><!--end of wrapper--> 
     <body> 
    </html> 

這是我的安全頁面:

<?php session_start(); 
    require_once("includes/functions/url.php"); 
    if(!isset($_SESSION['username'])){ 
     header("Location: ../"); 
     exit(); 
    } 
?> 
<html> 
    <head> 
     <title> 

     </title> 
    <head> 
    <link rel="stylesheet" media="all" type="text/css" href="css/main.css"/> 
    <script type="text/javascript" src="js/jquery-1.8.0.js"></script> 
    <body> 
      <div id="wrapper"> 
       <div id="header"> 
        <div id="nav"> 
         <a href="#">Home </a> | <a href="#">About us</a> | <a href="#">Products</a> | <a href="#">Services</a> | <a href="#">Carrers</a> 
         <?php 
          //show logout 
          if(isset($_SESSION['username'], $_SESSION['password'])){ 
           print " | <a href=\"includes/pages/logout.php?log=logout\">Logout</a>"; 
          } 
         ?> 
        </div> 
       </div><!--end of header--> 
       <div id="mainContent"> 
       <div id="RighContent"> 
        <h1>Welcome admin: <?php print @$_SESSION['username']; ?></h1> 
       </div><!---RightCont---> 
       <div id="LeftCont"> 
        afafhkashf 
       </div><!---leftcont---> 
       </div><!---end of maincontent--> 
       <div id="footer"> 

       </div><!--end footer--> 
      </div><!--end of wrapper--> 
    <body> 


</html> 

最後這裏是我的登出頁面:

 <?php 
      ini_set('session.use_trans_sid', false); 
      session_start(); 
      //require_once("includes/functions/url.php"); 
      if(isset($_GET['log']) && $_GET['log']=="logout"){ 
       if(isset($_SESSION['username'] , $_SESSION['password']) && !empty($_SESSION['username']) && !empty($_SESSION['password'])){ 
        unset($_SESSION['username']); 
        unset($_SESSION['password']); 
        header("Location: ../../"); 
        exit(); 
       } 
      } 
     ?> 
+5

我不明白'用戶ID婁代碼session_destroy()'在代碼中的任何地方***調用***。 – Matt

+3

此外,它可能不會幫助回答你的問題,但你應該停止使用'mysql_ *'函數。他們正在被棄用。請使用[PDO](http://php.net/manual/en/book.pdo.php)(自PHP 5.1起支持)或[mysqli](http://php.net/manual/en/book)。 mysqli.php)(自PHP 4.1起支持)。如果您不確定要使用哪一個,請閱讀本文(http://www.deprecatedphp.com/mysql_/)。 – Matt

+0

你有邏輯解除'$ _SESSION ['用戶名']'和'$ _SESSION ['passowrd']'(你爲什麼在會話中存儲密碼???),但如果其中任何一個值是空白,不會跑。不要檢查它們是否是非空的,只需'unset()'它們。 –

回答

2
unset($_SESSION); 
session_destroy(); 

你試過了嗎?

+0

對不起原來的sessin_destroy();是threre我猜我省略了它,當我插入我的代碼 –

3

貝婁是你所說的你的註銷功能。只是因爲你沒有設置Session[username] and Session[password]並不意味着你destroeyd會議。有一個叫做session_destroy的功能可以完成你想要的功能。有關其用法的信息,請參閱this

<?php 
       ini_set('session.use_trans_sid', false); 
       session_start(); 
       //require_once("includes/functions/url.php"); 
       if(isset($_GET['log']) && $_GET['log']=="logout"){ 
        if(isset($_SESSION['username'] , $_SESSION['password']) && !empty($_SESSION['username']) && !empty($_SESSION['password'])){ 
         unset($_SESSION['username']); 
         unset($_SESSION['password']); 
         header("Location: ../../"); 
         exit(); 
        } 
       } 
      ?> 
+0

安全頁上的邏輯檢查是否設置了$ _SESSION ['username']'。如果註銷頁面已經'unset()',那麼這個邏輯應該沒有'session_destroy()'。這些鍵不是'unset()',可能是因爲註銷頁面中的邏輯有問題。 –

+0

@MichaelBerkowski我看到了。我建議'session_destroy'的原因是因爲這是更好的做法。在註銷頁面中,他取消了用戶名和密碼。如果其他任何地方(我沒有看過所有的代碼),他使用'SESSION'來存儲其他東西,然後他可能會發生衝突。 'session_destroy'負責處理所有這些事情,並且更容易,然後取消設置會話中設置的所有內容。 –

+0

請你解釋爲什麼你認爲註銷邏輯是錯誤的(Michael Berkowski),因爲我非常確定我的小理解應該可以正常工作。這可能是http://www.php.net/manual/en/function.session-destroy.php#85743的原因,但我使用的是Firefox 14以上版本 –

0

連同所有對session_unset()session_destroy()上述意見,你可能還需要包括的東西清除緩存在瀏覽器中,以避免在使用後退按鈕,或以其他方式重新加載頁面的人。

header('cache-control: no-cache,no-store,must-revalidate'); // HTTP 1.1. 
header('pragma: no-cache'); // HTTP 1.0. 
header('expires: 0'); // Proxies. 
+0

我認爲Michael Berkowski是我正確更改我的註銷頁面爲ini_set('session.use_trans_sid',false); \t session_start(); \t //require_once("includes/functions/url.php「);如果(isset($ _GET ['log'])&& $ _GET ['log'] ==「logout」){ \t \t \t session_destroy(); \t \t header(「Location:../../」); \t}及其工作正如我預料的那樣,但我無法解釋y –

1

使用此在退出頁面

<?php 
session_start(); 
session_destroy(); 
session_unset(); 
header("location:../index.php"); 

?> 

和安全頁面

if($_SESSION['uid']==true){ 
} 
    }else{ 
     header('Location:../'); 
    } 

我會建議,而不是使用用戶名