【问题标题】:passing username/password to server url将用户名/密码传递给服务器 url
【发布时间】:2015-11-11 17:37:07
【问题描述】:

如何使用 $.post 请求将用户名和密码传递给服务器 url?

我想从那个请求中返回真或假。

$.post("dtbs.php",{username:userName,password:password},function(result){
});

上面的代码表示,但我想从该请求将真或假值传递给文本文件。 我很困惑如何做到这一点?

以下是我的脚本:

<script>
                  $(document).ready(function(){
                      $("#loginBtn").click(function(){
                        var userName = $("#inputlUsername3").val();
                        var password = $("#inputlPassword3").val();
                        if(userName && password) {
                          //your php request goes here and return true or false or any ohter response as per your need
                          $.post("dtbs.php",{username:userName,password:password},function(result){

});
                          $.ajax({url: "response.txt", success: function(result){
                              if(result === 'true') {
                                alert("Redirect it to dashboard");
                              } else {
                                alert("Show error");
                              }
                          }});
                        } else {
                          alert("Plz fill all the field");
                        }
                      });
                  });
            </script>

下面代表dtbs.php

<?php
$uname=$_POST['txtuname'];
$pwd2=$_POST['txtpwd2'];
$con=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("onlineshop",$con);
$r=mysql_query("select password from users where username='$uname'",$con);
$row = mysql_fetch_row($r);
    $val=$row[0];

if($val!="")
{
if($pwd2==$val)
{
    session_start();
    $_SESSION['username']=$uname;
    $myfile=  fopen("response.txt", "w") or die("unable to open file");
    $txt="true";
    fwrite($myfile, $txt);
    fclose($myfile);
}
 else {

      $myfile=  fopen("response.txt", "w") or die("unable to open file");
    $txt="false";
    fwrite($myfile, $txt);
    fclose($myfile);


}
}
 else {

$myfile=  fopen("response.txt", "w") or die("unable to open file");
    $txt="false";
    fwrite($myfile, $txt);
    fclose($myfile);
}
?>

建议我解决方案。

【问题讨论】:

    标签: php mysql ajax twitter-bootstrap modal-dialog


    【解决方案1】:

    如果用户名/密码成功,dtbs.php 应该返回结果,而不是写入文本文件。

    <script>
        $(document).ready(function(){
            $("#loginBtn").click(function(){
                var userName = $("#inputlUsername3").val();
                var password = $("#inputlPassword3").val();
                if(userName && password) {
                     $.post("dtbs.php", username:userName, password:password}, function(result){
                         if(result === 'true') {
                             alert("Redirect it to dashboard");                              
                         }
                         else {
                             alert("Show error");
                         }
                     });
    
                } else {
                    alert("Plz fill all the field");
                }
            });
        });
    </script>
    
    
    <?php
    $uname=$_POST['username'];
    $pwd2=$_POST['password'];
    $con=mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("onlineshop",$con);
    $r=mysql_query("select password from users where username='$uname'",$con);
    $row = mysql_fetch_row($r);
    $val=$row[0];
    
    if($val!="")
    {
        if($pwd2==$val)
        {
            session_start();
            $_SESSION['username']=$uname;
            echo 'true';
        }
        else {
            echo 'false';
        }
    }
    else {
        echo 'false';
    }
    ?>
    

    总体而言,您的代码存在以下问题:

    • 不要使用 msql_ 函数,它们已被弃用。
    • SQL 注入问题。
    • 在数据库中以纯文本形式存储密码。
    • 以 root 身份连接到 mySql,无需密码。

    【讨论】:

    • $.ajax 部分怎么样?
    • 不,为什么?将 $.post 中的结果返回到 dtbs.php。
    • 所以你的意思是说我使用 $.post 而不是 $.ajax?
    • 您不需要进行 2 次 ajax 调用。如果用户名/密码有效,则返回一个。
    • 我更新了我的答案以包含更多您的代码。请在将其投入生产之前研究这些其他问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 2012-07-17
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 2013-04-08
    • 2018-07-15
    相关资源
    最近更新 更多