2016-03-15 77 views
0

目前製造上的html一個小應用程序,其利用PHP爲連接到一個數據庫用戶登錄即無法「後退」到應用程序。用戶登錄按要求工作,用戶可以登錄,註冊,註銷和重新登錄,因爲正常的登錄行爲應該起作用。如何使我的簡單的PHP/HTML登錄安全使用會話?當登出

然而,當用戶註銷時,他們可以立即使用瀏覽器後退按鈕,這是一個明顯的安全漏洞「重新進入」應用程序。

我試圖創建上述問題的解決方案..任何幫助將高度讚賞使用會話。我的應用程序目前分爲以下幾頁。

1)的login.php(簡單的用戶名&密碼輸入到表單,表單執行logincheck.php

2)logincheck.php(見下文)

<?php 
session_start(); 
?> 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<link rel="stylesheet" type="text/css" href="site.css"> 
</head> 
<body> 
<div class="frame"> 
<div class="headerf"></div> 
<div class="contentf"> 
<h3>Incorrect user credentials.</h3>  
<?php 
include("info.inc.php"); 
[email protected]_connect(localhost,$username,$password); 
[email protected]_select_db($database) or die("Unable to select database"); 

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 
$sql="SELECT * FROM users WHERE username='$myusername' and password='$mypassword'"; 
$result=mysql_query($sql); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

// If result matched $myusername and $mypassword, table row must be 1 row 
if($count==1){ 

// Register $myusername, $mypassword and redirect to file "index.php" 
$_SESSION["username"] = $myusername; 
$_SESSION["password"] = $mypassword; 
header("location:../index.php"); 
} 
else { 
echo "Please return to the log-in page."; 
} 
?> 

<form name="form" method="post" action="../login.php"><br> 
<td><input type="submit" name="Submit" value="Back"></td> 
</div> 
</div> 
</body> 
</html> 

3)的index.php(如果登錄成功,用戶將重定向到此頁面,他們可以訪問應用程序,並可以像平常一樣導航)。

4)logout.php(會話結束時,用戶將被重定向到登錄頁面)

我目前在註銷頁面中的登錄會話開始和會話結束。我如何實現一個安全的登錄形式?一直試圖在線的例子,但無濟於事,即http://blog.teamtreehouse.com/how-to-create-bulletproof-sessions

我的理想的修復將是,而不是一個用戶能夠退出後退出到應用程序,他們被重定向到登錄頁面或用戶名和密碼提示再次

任何幫助,高度讚賞!

+0

請使用PHP的[內置函數](http://jayblanchard.net/proper_password_hashing_with_PHP.html)來處理密碼安全性。如果您使用的是小於5的PHP版本。5你可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。 –

+0

請[停止使用'mysql_ *'函數](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [這些擴展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中刪除。瞭解[編寫]​​(http://en.wikipedia.org/ wiki/Prepared_statement)語句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)並考慮使用PDO,[這真的很簡單](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+0

編寫一個檢查會話的'assert_logged_in()'函數。如果會話變量已被正確銷燬,該函數將重定向它們以登錄。 –

回答

0

時需要您從數據庫中檢索數據註冊新「的loggedIn」會話變量,或者您也可以使用現有的會話變量,即用戶名與每一頁的開始,而不是增加一個新的會話變量的檢查相同....

// If result matched $myusername and $mypassword, table row must be 1 row 
if($count==1){ 

// Register $myusername, $mypassword and redirect to file "index.php" 
$_SESSION["username"] = $myusername; 
$_SESSION["password"] = $mypassword; 
$_SESSION['loggedIn'] = true; 
header("location:../index.php"); 
} 
0

檢查用戶的憑據,如果用戶有一個有效的用戶名和密碼,您將設置會話變量,表示他們已經成功地登錄後。

例子(使用從logincheck.php腳本片段) :

// If result matched $myusername and $mypassword, table row must be 1 row 
if($count==1){ 

    // Register $myusername, $mypassword and redirect to file "index.php" 
    $_SESSION["username"] = $myusername; 
    $_SESSION["password"] = $mypassword; 

    // set a session variable that is checked for 
    // at the beginning of any of your secure .php pages 
    $_SESSION["loggedIn"] = true; 

    header("location:../index.php"); 
} 

在每個安全頁面的最開始檢查,看看是否這個會話變量已經被設置爲在記錄的結果如果未設置會話變量,那麼您只需重新定向。用戶回到你的login.php頁面。

例子:

<?php 

    // start session 
     session_start(); 

    // check for logged in session 
     if(!$_SESSION['loggedIn']) 
     { 
      // user is not logged in 
      // re-direct user to login.php 
       header("Location: login.php"); 
       exit; 
     } 

?> 

至於其他的意見提了,我強烈建議移動要麼MySQLiPDO數據庫用作mysql_ *功能已經從更高版本的PHP過時。

+0

您好Urlex,幾乎或多或少我想象的是可能的,謝謝。將現在有一個嘗試。我不熟悉!$ _ SESSION ['loggedIn'],爲了使用它,我需要在我的登錄頁面上識別哪些代碼來識別用戶登錄? – WhoKnew

+0

我對自己的答案做了一些編輯,希望能幫助解決一些困惑。 – urlex

+0

嘗試了以上,檢查會話變量的代碼返回一個頁面錯誤:(serverurl)目前無法處理此請求?嘗試玩耍,目前沒有修復,有什麼想法? – WhoKnew