我想讓自己成爲一個簡單的PHP網站與會員登錄系統。我創建了網站的註冊部分,併成功將條目更新/添加到我的Mysql數據庫中。不過,我無法讓我的login.php登錄使用的細節。我的php代碼爲login.php如下;無法登錄到我的腳本在PHP
<?php
if ($_POST['mem_email']) {
//Connect to the database through our include
include_once "storescripts/connect_to_mysql.php";
$email = stripslashes($_POST['mem_email']);
$email = strip_tags($email);
$email = mysql_real_escape_string($email);
$password = ereg_replace("[^A-Za-z0-9]", "", $_POST['mem_password']); // filter everything but numbers and letters
$password = md5($password);
// Make query and then register all database data that -
// cannot be changed by member into SESSION variables.
// Data that you want member to be able to change -
// should never be set into a SESSION variable.
$sql = mysql_query("SELECT * FROM members WHERE mem_email='$email' AND mem_password='$password' AND mem_emailactivated='1'");
$login_check = mysql_num_rows($sql);
if($login_check > 0){
while($row = mysql_fetch_array($sql)){
// Get member ID into a session variable
$id = $row["mem_id"];
session_register('mem_id');
$_SESSION['mem_id'] = $id;
// Get member username into a session variable
$username = $row["mem_username"];
session_register('mem_username');
$_SESSION['mem_username'] = $username;
// Update last_log_date field for this member now
mysql_query("UPDATE members SET mem_lastlogin=now() WHERE mem_id='$id'");
// Print success message here if all went well then exit the script
header("location: member_profile.php?id=".$id);
exit();
} // close while
} else {
// Print login failure message to the user and link them back to your login page
print '<br /><br /><font color="#FF0000">No match in our records, try again </font><br />
<br /><a href="login.php">Click here</a> to go back to the login page.';
exit();
}
}// close if post
?>
和位於同一頁面上的表格如下;
<table align="center" cellpadding="5">
<form action="login.php" method="post" enctype="multipart/form-data" name="logform" id="logform" onsubmit="return validate_form ();">
<tr>
<td class="style7"><div align="right">Email Address:</div></td>
<td><input name="mem_email" type="text" id="mem_email" size="30" maxlength="64" /></td>
</tr>
<tr>
<td class="style7"><div align="right">Password:</div></td>
<td><input name="mem_password" type="password" id="mem_password" size="30" maxlength="24" /></td>
</tr>
<tr>
<td> </td>
<td><input name="Submit" type="submit" value="Login" /></td>
</tr>
</form>
</table>
我的數據庫看起來像這樣
mem_id int(11)
mem_username varchar(255)
mem_country varchar(255)
mem_county varchar(255)
mem_address varchar(255)
mem_email varchar(255)
mem_password varchar(255)
mem_signupdate datetime
mem_lastlogin datetime
mem_accounttype enum('a', 'b', 'c')
mem_emailactivated enum('0', '1')
mem_postcode varchar(255)
mem_city varchar(255)
正確的數據被輸入到我的登錄表單每次,我加工else語句與失敗消息
你是否迴應了你正在生成的SQL語句並在數據庫中運行它?那它有用嗎? – andrewsi 2013-02-23 17:20:47
mysql_ *和ereg_ *都被棄用。事實上,我認爲ereg_ *完全缺少最新版本的PHP。只要你的主機升級他們的軟件,你的腳本就會中斷。 – GordonM 2013-02-23 17:20:49
這首先要求進行基本的調試。究竟什麼時候失敗?找出那個 - 使用調試器或逐行測試輸出 - 將是第一件事。我們無法爲您做到這一點,因爲我們沒有實時輸出。 – 2013-02-23 17:24:11