dongtangu6889 2014-12-30 06:45
浏览 43
已采纳

如何使用散列密码登录页面

i have hashed the password when a user will register ...but when i am trying to login the page it is showing that username or password is wrong...

Here is below the hashing code

// Get values from form
$fullname=$_POST['userid'];
$username=$_POST['username'];
$email=$_POST['uemail'];
$password=$_POST['passid'];
$birthdate=$_POST['birthdate'];
$country=$_POST['mytextarea'];
$hash = hash('sha256', $password);

function createSalt()
{
    $text = md5(uniqid(rand(), true));
    return substr($text, 0, 3);
}

$salt = createSalt();
$password = hash('sha256', $salt . $hash);
// Insert data into mysql
$sql="INSERT INTO tbl_registration(`fullname`,`username`,`email`,`password`,`birthdate`, `country`) VALUES('{$fullname}', '{$username}', '{$email}', '{$password}', '{$birthdate}', '{$country}' )";
$result=mysql_query($sql);

Here is below i am using the login code...

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

// To protect MySQL injection (more detail about 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 ){
    if(crypt($password, $row['Password']) == $row['Password'])
    {       
        header("location:index.php");
        exit();
    }
}
else {
    //echo "Wrong Username or Password";
    header("Location:login.php?errorMssg=".urlencode("Wrong Username or Password"));
}

So what i will do now..

  • 写回答

3条回答 默认 最新

  • duanre4421 2014-12-30 07:10
    关注

    Your are comparing the hashed/salted password with the text-password.

    For example: 5a44e87906e4e5dbf7991d5784fd56f14dc426aafbd8dbb7b1e0953e1399f2ad is not equals foo

    Note: mysql functions are deprecated. You should use PDO or other. You code can be written a little bit cleaner. If you need a better explanation, I can help you with it.

    edited hashing code:

    // Get values from form
    $fullname=$_POST['userid'];
    $username=$_POST['username'];
    $email=$_POST['uemail'];
    $password=$_POST['passid'];
    $birthdate=$_POST['birthdate'];
    $country=$_POST['mytextarea'];
    $hash = hash('sha256', $password);
    
    function createSalt()
    {
        return '2123293dsj2hu2besdbsjdsd';
    }
    
    $salt = createSalt();
    $password = hash('sha256', $salt . $hash);
    // Insert data into mysql
    $sql="INSERT INTO tbl_registration(`fullname`,`username`,`email`,`password`,`birthdate`, `country`) VALUES('{$fullname}', '{$username}', '{$email}', '{$password}', '{$birthdate}', '{$country}' )";
    $result=mysql_query($sql);
    

    edited login code:

    // userName and password sent from form
    $myusername=$_POST['username'];
    $mypassword=$_POST['password'];
    
    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    $salt = createSalt();
    $hash = hash('sha256', $mypassword);
    $mypassword = hash('sha256', $salt . $hash);
    
    $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 ){
        if(crypt($password, $row['Password']) == $row['Password'])
        {       
            header("location:index.php");
            exit();
        }
    }
    else {
        //echo "Wrong Username or Password";
        header("Location:login.php?errorMssg=".urlencode("Wrong Username or Password"));
    }
    
    function createSalt()
    {
        return '2123293dsj2hu2besdbsjdsd';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题