我想創建一個沒有數據庫的管理員登錄(這是一個非常簡單的網站)這裏是我的代碼,這將是安全的還是易於使用我的會話?我怎樣才能讓它變得更好?PHP +沒有數據庫:登錄 - >這會保密嗎?
的index.php
<?php
include("auth.php");
if(!checkAuthentication()){
?>
<form method="post" action="?login=1">
<label for="username">Username</label>
<input type="text" id="username" name="username">
<label for="password">Password</label>
<input type="text" id="password" name="password">
<input type="submit" value="Login">
</form>
<?php
}else{
header("Location: protected_page.php");
exit();
}
的functions.php
<?php
session_start();
// Check if username and password matches
function authentication($username, $password){
if($username == md5("admin") && $password == md5("password")){
return true;
}else{
return false;
}
}
// Check if session data is still valid
function checkAuthentication(){
if(isset($_SESSION['username']) && isset($_SESSION['password'])){
$result = authentication($_SESSION['username'], $_SESSION['password'], false);
}else{
$result = false;
}
return $result;
}
if(isset($_POST['username']) && isset($_POST['password']) && $_REQUEST['login'] == 1){
$username = md5(htmlspecialchars($_POST['username']));
$password = md5(htmlspecialchars($_POST['password']));
if(!authentication($username, $password)){
echo "Wrong Login.";
}else{
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header("Location: http://localhost/login/protected_page.php");
exit();
}
}
?>
protected_page.php
<?php
include("auth.php");
if(!checkAuthentication()){
header("Location: http://localhost/login/index.php");
exit();
}
var_dump($_SESSION);
?>
謝謝您的幫助!
此問題似乎是無關緊要的,因爲它是一個代碼審查請求。這更適合http://codereview.stackexchange.com –
^這也不要使用'md5()'來輸入密碼。它已經過時,並不是爲了開始密碼而設計的。請參閱:http://www.php.net/manual/en/faq.passwords.php –