我可以註冊一個用戶,但是當我嘗試用它登錄,我有2個問題:我的登錄信息將無法正常工作
1:我可以登錄使用的用戶名,但我也能鍵入任何我想要的密碼輸入部分,我仍然登錄(它不檢查數據庫中的真實密碼)
2:當我嘗試使用電子郵件和密碼的組合,然後我無法登錄在我只收到錯誤消息。
我在想問題在於我的$query select FROM members
bla bla ...但我不確定。
對不起,這樣的菜鳥。
這是register.php
<form method="post" action="">
<input type="text" name="username" placeholder="USERNAME">
<input type="password" name="password1" placeholder="PASSWORD">
<input type="password" name="password2" placeholder="CONFIRM PASSWORD">
<input type="text" name="email" placeholder="E-MAIL">
<input type="date" name="age" id="age" >
<input type="radio" value="male" name="gender" checked>
<input type="radio" value="female" name="gender">
<input type="submit" value="SIGN UP" name="create_member">
</form>
<?php
require_once ("core/connect.php");
if(isset($_POST['create_member']))
{
$username = mysqli_real_escape_string($dbc, trim ($_POST['username']));
$password1 = mysqli_real_escape_string($dbc, trim ($_POST['password1']));
$password2 = mysqli_real_escape_string($dbc, trim ($_POST['password2']));
$email = mysqli_real_escape_string($dbc, trim ($_POST['email']));
$age = mysqli_real_escape_string($dbc, trim ($_POST['age']));
$gender = mysqli_real_escape_string($dbc, trim ($_POST['gender']));
if($password1 != $password2)
{
echo 'THE TWO PASSWORDS ARE NOT THE SAME';
}
$hash = hash('sha256', $password1);
function createSalt()
{
$text = md5(uniqid(rand(), true));
return substr($text, 0, 3);
}
$salt = createSalt();
$password = hash('sha256', $salt . $hash);
if(!empty($username) && !empty($email) && !empty($password) && !empty($age) && !empty($gender))
{
$query_ind = "INSERT INTO members VALUES ('', '$username', '$password', '$email', '$age' , '$gender', '$salt', NOW())";
mysqli_query($dbc, $query_ind);
}
else
{
echo "FILL OUT THE FORM PLEASE";
}
}
?>
,這是login.php中
<?php
$error_msg = '';
if (isset($_POST['member_login']))
{
// Grab the user-entered log-in data
$member_username = mysqli_real_escape_string($dbc, trim($_POST['username']));
$member_email = mysqli_real_escape_string($dbc, trim($_POST['username']));
$member_password = mysqli_real_escape_string($dbc, trim($_POST['password']));
if (!empty($member_username) && !empty($member_password))
{
// Look up the username and password in the database
$query = "SELECT * FROM members WHERE member_username = '$member_username' OR member_email = '$member_email' AND member_password = '$member_password'"; // SHA('$member_password')";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 1)
{
// The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page
$row = mysqli_fetch_array($data);
$_SESSION['member_id'] = $row['member_id'];
$_SESSION['member_username'] = $row['member_username'];
$_SESSION['member_email'] = $row['member_email'];
setcookie('member_id', $row['member_id'], time() + (60 * 60 * 24 * 7)); // expires in 7 days
setcookie('member_username', $row['member_username'], time() + (60 * 60 * 24 * 7)); // expires in 7 days
setcookie('member_email', $row['member_email'], time() + (60 * 60 * 24 * 7)); // expires in 7 days
header('Location: ' . $_SERVER['PHP_SELF'] . '?page=mlog_in');
}
else
{
// The username/password are incorrect so set an error message
$error_msg = ' INCORRECT INFOMATION, TRY AGAIN. ';
}
}
else
{
// The username/password weren't entered so set an error message
$error_msg = ' INCORRECT INFOMATION, TRY AGAIN. ';
}
}
mysqli_close($dbc);
if(!isset($_SESSION['member_id']))
{
?>
<div class="sixteen columns">
<h2>LOGIN</h2>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" class="sixteen columns">
<input required type="text" name="username" placeholder="USERNAME/E-MAIL"/>
<input required type="password" name="password" placeholder="PASSWORD" />
<input required type="submit" name="member_login" value="LOGIN" />
<input type="checkbox" name="remember" value="1"><span>REMEMBER ME</span>
<?php
echo '<p>' . $error_msg . '</p>';
?>
<?php
echo '<a href="index.php?page=register" title="CLICK TO SIGN UP">MAKE A PROFILE</a>';
?>
</form>
</div>
<?php
}
else
{
$profile = '';
if(isset($_GET['profile']))
{
$profile = $_GET['profile'];
}
?>
<?php
switch($profile)
{
default :
require_once 'profile/userpage.php';
break;
}
}
?>
您需要使用密碼對鹽進行散列。並且您不重新計算它以驗證密碼。 – 2014-09-21 00:55:22
「重新計算」它來驗證密碼是什麼意思? – 2014-09-21 01:29:19