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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制