【问题标题】:Safely Passing AJAX Values To PHP安全地将 AJAX 值传递给 PHP
【发布时间】:2018-04-05 15:30:42
【问题描述】:

我正在制作一个允许用户通过 google 登录的网站,我需要能够将姓名、电子邮件和用户图像 URL 发送到服务器。我目前正在使用 AJAX 来执行此操作。以下是我使用的功能:

function onSignIn(googleUser){
  var profile = googleUser.getBasicProfile();
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if(this.readyState == 4 && this.status == 200){
      document.getElementById('php').innerHTML = this.responseText;
    }
  };
  xhttp.open('POST', "google_sign_in.php", true);
  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.send('name=' + profile.getName() + '&image=' + profile.getImageUrl() + '&email=' + profile.getEmail());
}

这会将所有信息发送到服务器。没有什么可以阻止用户使用诸​​如 Firebug 之类的网络检查工具将 xhttp.send 函数中的值更改为:

xhttp.send(name=fakename&imagefakeimg.jpg&email=fake@information.com);

我做了一些阅读并得出结论,我不应该依赖客户端安全性(试图阻止 Web 检查器),应该进行更多的服务器端验证。

我可以访问 HTML、CSS、JS 和 PHP。如何防止用户输入无效信息?

注意:如果需要的话,我愿意学习其他将客户端信息发送到服务器的方法。另外,我无法访问命令行。

【问题讨论】:

标签: php ajax validation security


【解决方案1】:

您可以使用您制作的 Javascript,但只会将 Google 令牌发送给您的 php 脚本。

仅供参考,令牌在这里:googleUser.getAuthResponse().id_token;

在您的 php 脚本中,使用 Google php API 客户端库:https://developers.google.com/api-client-library/php/auth/web-app

并验证令牌,检索用户信息并将其保存在您的数据库中。

【讨论】:

  • 谢谢!这帮助很大!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-05
  • 1970-01-01
相关资源
最近更新 更多