dongxiao9583 2016-07-21 13:36
浏览 55
已采纳

使用Ajax将密码发送到PHP

I'm trying to implement an authentication from.
My current version works. However, is it consider a bad practice to send the password unencrypted, though I'm using POST?

JavaScript:

$.ajax({
    type : "POST",
    url : "script.php",
    data : {
        q : "login",
        user: $("#user").val(),
        pass: $("#pass").val()
    },
    success : function(data) {
        if(data){
            alert("VALID")
        }else{
            alert("INVALID")
        }
    }
});

PHP:

if ($_POST ["q"] == "login") {
    $user = $_POST ["user"];
    $pass = $_POST ["pass"];

    $sql = "SELECT user, pass FROM users WHERE user='" . $user . "'";

    $stmt = sqlsrv_query ( $conn, $sql );
    if ($stmt === false) {
        die ( print_r ( sqlsrv_errors (), true ) );
    }

    $arr = array ();
    $row = sqlsrv_fetch_array ( $stmt );
    $hash = $row ["pass"];

    if (password_verify ( $pass, $hash )) {
        die ( true );
        exit ();
    } else {
        die ( false );
        exit ();
    }

    sqlsrv_free_stmt ( $stmt );
    sqlsrv_close ( $conn );

    exit ();
}

Is there a best practice for sending password from JS (Ajax) to PHP?

  • 写回答

1条回答 默认 最新

  • dtnrsmi824877 2016-07-21 13:45
    关注

    is it consider a bad practice to send the password unencrypted

    Yes, very bad.

    Is there a best practice for sending password from JS (Ajax) to PHP?

    Whether the request is made via a standard page load or an Ajax request is irrelevant. Whether the request is made to PHP or another system is irrelevant. Whether you're using GET or POST is irrelevant. If you're sending passwords, use HTTPS, always. If the cost of an SSL certificate is stopping you, you can get free ones at letsencrypt.org

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器