doujieyu7062 2014-04-03 09:25 采纳率: 0%
浏览 192
已采纳

将sh1添加到我的脚本后出现错误登录或用户名错误

i have made my own login authentication script into php, it's working fine when i use it without using "sh1"... and successfully uploaded username and password into my database, but i want to encrypt password into database due to security reason, now i'm using $hash_key=sha1($_POST['password']); into script, when i check into database it successfully encrypted see my pic..

enter image description here

But as i logged in, it's giving me an error

enter image description here

So, i decided to remove "sh1" and directly save my password into database once to check..

enter image description here

and it's working fine:

enter image description here

But using sha1 it's working and showing an error message invalid username and password..

enter image description here

Please help, what's wrong into my code??

Here is a code for insert.php

<?php
$con=mysqli_connect("localhost","root","Bhawanku","members");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$hash_key=sha1($_POST['password']);
$sql="INSERT INTO admin
VALUES
('', '$_POST[username]','$hash_key')";

if (!mysqli_query($con,$sql))
{
    die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?> 

and here is a code for login.php

<?php
include("config.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{

// username and password sent from Form
$myusername=addslashes($_POST['username']);
$mypassword=addslashes($_POST['password']);

$sql="SELECT * FROM admin WHERE username='$myusername' AND passcode='.sha1[$mypassword]' ";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];
$count=mysql_num_rows($result);


// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
$_SESSION["myusername"];
$_SESSION['login_user']=$myusername;

header("location: welcome.php");
}
else
{
echo "Your Login Name or Password is invalid";
}
}
?>
<form action="" method="post">
    <label>UserName :</label>
    <input type="text" name="username"/><br />
    <label>Password :</label>
    <input type="password" name="password"/><br/>
    <input type="submit" value=" Submit "/><br />
</form>
  • 写回答

3条回答 默认 最新

  • douzhan1238 2014-04-03 09:28
    关注

    This line in select query

    passcode='.sha1[$mypassword]'
    
    $sql="SELECT * FROM 
    admin WHERE username='$myusername' 
    AND passcode='.sha1[$mypassword]' ";
    

    should be

    $sql="SELECT * FROM 
    admin WHERE username='$myusername' 
    AND passcode='".sha1($mypassword)."'";
    

    UPDATE

    The problem appears to be while saving the password the field was set to varchar(30) sha1() will return string longer than that. so that needs to be increased. And the password needs to be re-inserted since old passwords will not work.

    Now

    $myusername=addslashes($_POST['username']);
    $mypassword=addslashes($_POST['password']);
    

    Should be replaced as

    $myusername=mysql_real_escape_string(trim($_POST['username']));
    $mypassword=mysql_real_escape_string(trim($_POST['password']));
    

    The query is already mentioned above.

    And if there are more that one user with the same username and password then the condition

    if($count==1)
    

    will fail

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置