它不工作,我花了幾個小時和一半的頭髮試圖找出原因。我已經在我的HTML索引文件嵌入這個代碼的形式:代碼登錄和數據庫連接在PHP
我想創建登錄和註銷會話
我在MySQL數據庫表中的代碼看起來像這樣
CREATE TABLE member (id int(10) NOT NULL auto_increment, userName varchar(50) NOT NULL, passWord varchar(50) NOT NULL, PRIMARY KEY (id)) ENGINE=MyISAM utf8_general_ci AUTO_INCREMENT=3 ;
<?php
// Inialize session
session_start();
// Include database connection settings
$hostname = 'localhost'; // Your MySQL hostname. Usualy named as 'localhost', so you're NOT necessary to change this even this script has already online on the internet.
$dbname = 'database'; // Your database name.
$username = 'root'; // Your database username.
$password = ''; // Your database password. If your database has no password, leave it empty.
// Let's connect to host
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
// Select the database
mysql_select_db($dbname) or DIE('Database name is not available!');
// Retrieve username and password from database according to user's input
$userName=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$passWord=md5($password); // Encrypted Password
//*********retrieving data from Database**********
$query = "SELECT * FROM member WHERE userName='$userName' and passWord='$passWord'";
//$login = mysqli_query("SELECT userName,password FROM 'member' WHERE userName= $_post['username'] AND passWord= $_post['password'])");
// Check username and password match
$res = mysql_query($query);
$rows = mysql_num_rows($res);
if ($rows==1) {
// Set username session variable
$_SESSION['userName'] = $_POST['username'];
// Jump to secured page
header("Location: securedpage.php");
}
else {
// Jump to login page
echo "user name and password not found";
}
exit;
?>
而登錄它直接找到用戶名和密碼,甚至找不到用戶名和密碼,甚至用戶名和密碼都是正確的
這應該很容易調試。你到目前爲止做了哪些故障排除? –
這裏有很多紅旗 - 'mysql_'棄用,純文本密碼存儲,沒有MySQL root密碼等。我建議在谷歌搜索「PHP MySQL登錄」之後從頭開始重新考慮這個 – jterry
@ jterry密碼可能已被刪除而不是把它粘貼到世界上去看......但是,這裏有很多問題。 – naththedeveloper