2014-05-17 230 views
0

這是我db.classes登錄和註銷重定向後PHP

public function login($un, $pw) 
    { 
     $result = mysqli_query($this->dbh,"select username, password from users where username='$un' and password='$pw'"); 

     $count=mysqli_num_rows($result); 

     if($count==1){ 
      header("location:home.php"); 
     } 
     else { 
      echo 
      "<script type='text/javascript'> 

      alert('LOG-IN FAILED!'); 

      </script>"; 

      echo '<script language="JavaScript"> window.location.href ="index.php" </script>'; 
     } 
    } 

這是我的PHP

<?PHP 
include("db.classes.php"); 
$g = new DB(); 
$g->connection(); 

    if(isset($_POST['subBtn'])) 
     { 
      $un = $g->clean($_POST["uname"]); 
      $pw = $g->clean($_POST["pass"]); 
      $g->login($un, $pw); 
     } 

$g->close();  
?> 

這裏是我的註銷按鈕

<div id="footer"> 
    <li id="Login"><a href="index.php">Log Out</a></li> 
</div> 

我怎樣才能防止用戶在退出後無需重新登錄即可返回到前幾頁?

+0

檢查會話是否爲空 –

+0

登錄設置會話並註銷表示如果會話爲空,則取消會話重定向至index.php頁面 – Ramki

+0

http://www.makeitsimple.co.in/PHP_loginexmp.php –

回答

0

就在你的腳本最開頭添加

session_start(); 

當登錄成功後,你可以設置一個會話像

$_SESSION['login'] = true; 

,甚至存儲用戶數據就像他的名字或以後的操作ID。

那麼你一定要確保每個頁面上只添加

if($_SESSION['login'] !== true) header("location:index.php"); 

你可以取消設置會話或只是將其設置爲false

$_SESSION['login'] = false; 
unset($_SESSION['login']); 

這可以優化您的需要註銷至於你想....

http://krisnaordinary.wordpress.com/2010/04/24/creating-a-simple-login-logout-session-using-php/

這北極樂似乎走深一點到會話...

0
<?PHP 
session_start(); 
$_SESSION['logged_in'] = false; 

include("db.classes.php"); 
$g = new DB(); 
$g->connection(); 

if(isset($_POST['subBtn'])){ 
    $un = $g->clean($_POST["uname"]); 
    $pw = $g->clean($_POST["pass"]); 
    $g->login($un, $pw); 
} 

$g->close();  
?> 

在登錄時:設置logged_in爲true(如果憑據都ok)

public function login($un, $pw){ 
    $result = mysqli_query($this->dbh,"select username, password from users where username='$un' and password='$pw'"); 

    $count=mysqli_num_rows($result); 
    if($count==1){ 
     $_SESSION['logged_in'] = true; 
     header("location:home.php"); 
    } else { 
     $_SESSION['logged_in'] = false; 
     echo "<script type='text/javascript'>alert('LOG-IN FAILED!');</script>"; 
     echo '<script language="JavaScript"> window.location.href ="index.php" </script>'; 
     die(); 
    } 
} 

在 'home.php':

<?php 
session_start(); 
if(!$_SESSION['logged_in']){ 
    header('Location: index.php'); 
} 
//... 

上登出: 編輯來回答評論的問題:

退出按鈕:

<div id="footer"> 
    <li id="Login"><a href="index.php?logout=1">Log Out</a></li> 
</div> 

在index.php文件(在開頭):

<?php 

session_start(); 
if(isset($_GET['logout']) && $_GET['logout'] == 1){ 
    $_SESSION['logged_in'] = false; 
    header('location: index.php'); 
    die(); 
} 
+0

其中我把最後的代碼放在(註銷)? – jayyyyy

+0

我編輯了我的答案來回答這個問題:-) –

0

,所有的頁面未公開任何事情之前應該檢查會話。

例子:

<?PHP 
session_start(); 
if ($_SESSION['user_id']<1) 
    die('You must login'); //Or redirect from here 

這將防止他們回擊。您可能還需要發送一些緩存頭,以及:

<?PHP 
header ("Expires: Thu, 19 Nov 1981 08:52:00 GMT",true); 
header ("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0",true); 
header ("Pragma: no-cache",true); 

通常不需要緩存頭,但如果問題仍然存在,可能會有幫助。