我无法使用注册时使用的SQL凭据登录

时间:2015-12-23 21:19:38

标签: php mysql

晚上好,

我在家用电脑上安装了XAMPP,我正在学习构建自己的网站。我为用户帐户设置了php脚本。

我遇到了一个我不知道如何解决的问题。我会一直尝试自己解决问题,但我无法理解这是什么......

我的登录/注册php脚本全部设置。

我可以完全注册,我的SQL数据库会更新注册详情(用户名,密码,电子邮件,电话,城镇)。

我的问题是,当我尝试登录到我注册的用户帐户时,将我发送到我的login_fail.php页面,我无法理解为什么这样做。 我认为这可能是由于我在checklogin底部的ELSE声明?

我的checklogin.php脚本如下所示:

<?php
// setting the variables
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection 
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file main login
session_start();
$_SESSION["loggedin"] = 'true';
header("location: main_login.php");
die();
}

else {
header("location: login_fail.php");
die();
}
?>

我的register.php如下:

<?php  
// Connects to your Database   
mysql_connect("localhost", "root", "") or 
die(mysql_error());   
mysql_select_db("test") or die(mysql_error());
//This code runs if the form has been submitted  
if (isset($_POST['submit']))     
 //This makes sure they did not leave any fields blank  
if (!$_POST['username'] | !$_POST['password'] | !$_POST['email'] | !$_POST['phone'] | !$_POST['town'] ) {  
header("location: login_fail.php");  
die();
}    
// checks if the email is in use  
if (!get_magic_quotes_gpc()) {  
$_POST['email'] = addslashes($_POST['email']);  
}  
$emailcheck = $_POST['email'];  
$check = mysql_query("SELECT email FROM members WHERE email = '$emailcheck'")   
or die(mysql_error());  
$check2 = mysql_num_rows($check);    
//if the name exists it gives an error  
if ($check2 != 0) {  
header("location: login_fail.php");  
die(); 
}   
// here we encrypt the password and add slashes if needed  
$_POST['password'] = md5($_POST['password']);  
if (!get_magic_quotes_gpc()) {  
$_POST['password'] = addslashes($_POST['password']);  
$_POST['username'] = addslashes($_POST['username']);  
}    
// now we insert it into the database  
$insert = "INSERT INTO members (username, password, email, phone, town)  
VALUES ('".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '".$_POST['phone']."', '".$_POST['town']."')";  
$add_member = mysql_query($insert); 
header("location: register_success.php");
die();
?>  

我的登录表(不知道它有用吗?

            <table width="300" border="0" margin-bottom= "5%" align="left" cellpadding="0" cellspacing="1" bgcolor="none">
                        <tr>
                            <form name="form1" method="post" action="checklogin.php">
                                    <td>
                                        <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                                        <tr>
                                        <td colspan="3"><strong>Member Login </strong></td>
                                        </tr>
                                        <tr>
                                        <td width="78">Username</td>
                                        <td width="6">:</td>
                                        <td width="294"><input name="myusername" type="text" id="myusername"></td>
                                        </tr>
                                        <tr>
                                        <td>Password</td>
                                        <td>:</td>
                                        <td><input name="mypassword" type="text" id="mypassword"></td>
                                        </tr>
                                        <tr>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                        <td><input type="submit" name="Submit" value="Login"></td>
                                        </tr>
                                        </table>
                                    </td>
                            </form>
                    </tr>
            </table> 

在我的SQL数据库中,密码仍然是加密的。

如果我在mySQL中更改了用户密码,我可以正常登录,但只是在注册后(使用原始凭据)我无法登录...

有什么想法吗?

非常感谢。

斯坦。

4 个答案:

答案 0 :(得分:1)

您说您的数据库正在存储加密的密码,但您没有检查加密的密码。

您正在获取密码:

$mypassword=$_POST['mypassword']; 
$mypassword = stripslashes($mypassword);

并直接检查。

您需要首先加密通过登录提交的密码,然后根据存储在数据库中的密码检查加密版本。

基本上,使用您在注册时使用的相同代码。

答案 1 :(得分:1)

在您登录时使用md5()函数:

$mypassword = mysql_real_escape_string($mypassword); 
$mypassword=md5($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

因为您在注册时将密码字段保存为md5()

答案 2 :(得分:0)

$mypassword = stripslashes($mypassword);替换为$mypassword = md5($mypassword);

答案 3 :(得分:0)

登录时你应该这样做:

$mypassword=md5($_POST['mypassword']);

您可以将代码升级到MySQLi_ *函数,因为MySQL_ *函数已被折旧。

另外,我想提一下使用MD5哈希的安全性。 MD5($ password)不再被认为是一种安全的哈希密码方式,因为它很容易破解这些哈希值,特别是没有附加任何盐。有数十亿个MD5哈希值与在线比较的数据库,请不要使用MD5哈希函数来识别密码。查看password_hash函数。

相关问题