這是一個簡化的安全登錄功能,我在許多項目中構建和使用。 它顯示它不僅僅是檢查用戶名和密碼,可以做出更新的哈希值以及與您的用戶名一起檢查。
看看它,你的點子就明白了。
<?php
session_start();
//Checks on all pages, this checks time validity & hased user ip set at login, it will log out if something changes
$_SESSION['user_status'] = auth($_SESSION['user_name'],$_SESSION['user_hash'],"","","check");
//or
if(@auth($_SESSION['user_name'],$_SESSION['user_hash'],"","","check")=="LOGGED_IN" && $_SESSION['user_status']=='LOGGED_IN'){
//Logged _in
}
//LOGIN
if(isset($_POST['user']) && isset($_POST['pass'])){
$_SESSION['user_status'] = auth("","",$_POST['user'],$_POST['pass'],"login");
}
//LOGOUT USER
if(isset($_REQUEST['do']) && $_REQUEST['do'] =='logout'){
auth("","","","","logout");
header('Location: ./index.php');
}
/**
* Auth function for logging in & persistaint user checks
*
* @param $_SESSION string $logged_in_user
* @param $_SESSION string $hash
* @param $_POST string $username
* @param $_POST string $password
* @param string $exe
* @return set $_SESSION string
*/
function auth($logged_in_user,$hash,$username="",$password="",$exe) {
global $db;
if ($exe=='login') {
//LOGIN
$result = mysql_query('SELECT * FROM users WHERE username="'.mysql_real_escape_string($username).'" and password="'.mysql_real_escape_string(sha1($password)).'"',$db);
$num = mysql_num_rows($result);
if($num=='1') {
session_regenerate_id();
$_SESSION['logged_in']=true;
while ($row = mysql_fetch_array($result)) {
$_SESSION['user_id'] = $row['id'];
$_SESSION['user_name'] = $row['username'];
$_SESSION['user_hash'] = sha1($row['username'].$_SERVER['REMOTE_ADDR']);
$_SESSION['user_ip'] = $_SERVER['REMOTE_ADDR'];
$_SESSION['user_date'] = time();
$_SESSION['user_level'] = $row['user_level']; // have different user levels for your users
}
mysql_query('REPLACE INTO users values ("'.mysql_real_escape_string($_SESSION['user_id']).'","'.mysql_real_escape_string($_SESSION['user_name']).'","'.mysql_real_escape_string(sha1($password)).'","'.mysql_real_escape_string($_SESSION['user_hash']).'","'.mysql_real_escape_string($_SESSION['user_ip']).'","'.mysql_real_escape_string($_SESSION['user_date']).'","'.mysql_real_escape_string($_SESSION['user_comment']).'","'.mysql_real_escape_string($_SESSION['user_level']).'")',$db);
$return = 'LOGGED_IN';
return $return;
}elseif($num >='2') {
$_SESSION['logged_in']=false;
$error = 'ERROR:MULTI:USERS';
return $error;
}else{
unset($_SESSION['user_id']);
unset($_SESSION['user_name']);
unset($_SESSION['user_hash']);
unset($_SESSION['user_ip']);
unset($_SESSION['user_date']);
unset($_SESSION['user_level']);
$_SESSION['logged_in']=false;
session_destroy();
$return = 'ACCESS_DENIDE';
return $return;
}
return $return;
}
if($exe=='check') {
//CHECK
$result = mysql_query('SELECT hash,ip,user_date FROM users WHERE username="'.mysql_real_escape_string($logged_in_user).'" and hash="'.mysql_real_escape_string($hash).'"',$db);
if(mysql_num_rows($result)==1) {
$rows = mysql_fetch_row($result);
$timeout = (time()-1800);
if($rows[2]<=$timeout){auth("","","","","logout");
return'ACCESS_TIMEOUT';
}
if($hash==$rows[0] && $_SERVER['REMOTE_ADDR']==$rows[1]) {
$return = 'LOGGED_IN';
mysql_query('UPDATE users set user_date="'.time().'"',$db);
return $return;
}else {
session_regenerate_id();
$_SESSION['logged_in'] = 'ACCESS_DENIDE';
$return = 'ACCESS_DENIDE';
return $return;
}
}else{
session_regenerate_id();
$_SESSION['logged_in']=false;
$return = 'ACCESS_DENIDE';
return $return;
}
}
if($exe=='logout') {
//LOGOUT
unset($_SESSION['user_id']);
unset($_SESSION['user_name']);
unset($_SESSION['user_hash']);
unset($_SESSION['user_ip']);
unset($_SESSION['user_date']);
unset($_SESSION['user_level']);
unset($_SESSION['logged_in']);
session_destroy();
session_regenerate_id();
$return = 'ACCESS_LOGGED_OUT';
return $return;
}
return false;
}
?>
SSL適合反包嗅探,但有cain可以僞造/僞造,md5從來沒有用於加密密碼,用js驗證是無用的,您應該始終驗證服務器端,不需要消化/安全用戶名 –
你能澄清你的意思是「郵件將通過js驗證」嗎? – deceze
這是一個真正的郵件地址,其中[email protected] – Edig