小编Jee*_*ong的帖子

通过ajax登录和注册是否安全?

我的 JS 代码(举个例子,可能有一些语法错误)

$(document).on('click','#btnEdit',function(e){ 
    var id = $("#inp_id").val(), ps = $("#inp_ps");     
    $.ajax({
        type: 'POST',
        url: 'http://example.php',
        data: {act:'#LogIn',id:id,ps:ps},
        success: function(response){
            if($.trim(response) === "success"){
                alert("Login Successful");   
            }
            else{
                alert("Invalid ID or Password");
            }
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

在 php 里面

if($_POST[act] == "#LogIn"){
    $userid = Encrypt($_POST[id],$key1);
    $userps = hashPS($_POST[ps],$salt);
    //query...
    if(result > 0){
         $_SESSION['id'] = Encrypt($userid,$key2);
         $_SESSION['token'] = //random code;
    }
}
Run Code Online (Sandbox Code Playgroud)


我的问题:

有没有办法使代码安全?因为我认为攻击者可以编写自己的脚本并将数据发送到 php 以获取和 id。或者也许使用ajax登录和注册只是一个坏主意。

javascript php ajax

5
推荐指数
1
解决办法
568
查看次数

使用PHP将数据发送到API

选项1:

$data= array(
    "Code" => "abcde",
    "Id" => "A007",
    "RefNo" => "123456",
    "UserName" => "QWE",
    "UserEmail" => "qwe@gmail.com",
);
$url="https://testing.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result=curl_exec($ch);
curl_close ($ch);
echo $result;
}
Run Code Online (Sandbox Code Playgroud)


选项2:

<form method="post" action="https://testing.php">
    <input type="hidden" value="abcde" name="Code">
    <input type="hidden" value="A007" name="Id">
    <input type="hidden" value="QWE" name="UserName">
    <input type="hidden" value="qwe@gmail.com" name="UserEmail">
    <input type="hidden" value="123456" name="RefNo">
    <input type="submit" name="submit">
</form>
Run Code Online (Sandbox Code Playgroud)


A和B之间有什么区别吗?因为我尝试了两者,但是curl只从api中获得“失败”响应。

php curl

0
推荐指数
1
解决办法
391
查看次数

标签 统计

php ×2

ajax ×1

curl ×1

javascript ×1