我決定移動到mysqli的,因爲我不斷收到熄火,當人們發現我的MySQL代碼:-)移動到mysqli的
可能有人驗證下面是正確的,它所有的作品,但我只是想確認在我進入網站的其他部分之前,我沒有做一些愚蠢的事情或者存在安全風險。 這是登錄時用於用戶名/密碼檢查的php。
<?php
//Start session
session_start();
//Include database connection details
require_once('db_connect.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
// cleanup POST variables
$username = mysqli_real_escape_string($mysqli, stripslashes(trim($_POST['username'])));
$password = mysqli_real_escape_string($mysqli, stripslashes(trim($_POST['password'])));
//Input Validations
if($username == '') {
$errmsg_arr[] = 'Username required';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password required';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: logon.php");
exit();
}
//Load and run query
$result = mysqli_query($mysqli, "SELECT * FROM auth WHERE username='$username' AND password='$password'");
if ($result->num_rows) {
//Login Successful
session_regenerate_id();
//Set session variables
$member = $result->fetch_assoc();
$_SESSION['SESS_MEMBER_ID'] = $member['ID'];
$_SESSION['SESS_USERNAME'] = $member['username'];
$_SESSION['SESS_FIRST_NAME'] = $member['fname'];
$_SESSION['SESS_PASSWORD'] = $member['password'];
$_SESSION['SESS_AUTH_LEVEL'] = $member['auth_level'];
session_write_close();
header("location: index");
exit();
}else {
//Login failed
$errmsg_arr[] = 'user name or password not found';
$errflag = true;
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: logon.php");
exit();
}
}
mysqli_close($mysqli);
?>
非常感謝!
這個問題會更適合代碼審查。 – Kermit
你真的應該使用準備好的陳述和加密你的密碼 – Fabio
爲什麼不移動到PDO?:http://php.net/manual/en/book.pdo.php –