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 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据