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 手机连接电脑热点显示无ip分配
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大