PHP和AJAX安全性问题 [英] PHP and AJAX security question

查看:519
本文介绍了PHP和AJAX安全性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建设一个web应用程序中,PHP文件加载到使用jQuery的$就功能的主文件。然而,PHP文件显然仍是应用程序的外部访问,只需键入文件名在地址栏中。

所以我的问题是什么是做的最好办法是让PHP文件被'ajaxed的人都知道它包含在正确的页面,将正常工作,但如果以其他任何方式访问(即使有人做他们自己的网站和AJAX在我的PHP文件),然后将该文件应该说访问被拒绝或什么的。

在此先感谢

解决方案

谷歌上搜索了很多之后达成的答案!

步骤1:生成令牌系统的所有网络服务:

生成令牌:

 < PHP
  在session_start();
  $令牌= MD5(兰特(1000,9999)); //你可以使用任何加密
  $ _SESSION ['令牌'] = $令牌; //保存它作为会话变量
?>
 

步骤2:用它在发送Ajax调用:

  VAR form_data = {
  数据:$(#数据)VAL(),//你的数据被发送与阿贾克斯
  令牌:'&​​LT; PHP的echo $令牌; ?>',//使用的标记在这里。
  is_ajax:1
};

$阿贾克斯({
  键入:POST,
  网址:yourajax_url_here,
  数据:form_data,
  成功:函数(响应)
  {
    //进一步做
  }
});
 

步骤3:现在,让我们确保AJAX处理PHP文件,

 在session_start(); //大多数人忘记了这一点,同时复制粘贴code;)
如果($ _ SERVER ['HTTP_X_REQUESTED_WITH'] =='XMLHtt prequest'){
  //请求认定为Ajax请求

  如果(@isset($ _ SERVER ['HTTP_REFERER'])及和放大器; $ _ SERVER ['HTTP_REFERER'] = =HTTP:// YOURDOMAIN / ajaxurl)
  {
   // HTTP_REFERER验证
    如果($ _ POST ['令牌'] = = $ _SESSION ['令牌']){
      //做你的AJAX任务
      //不要忘了在这里使用SQL注入prevention。
    }
    其他 {
      头('位置:http://yourdomain.com');
    }
  }
  其他 {
    头('位置:http://yourdomain.com');
  }
}
其他 {
  头('位置:http://yourdomain.com');
}
 

  

注意:对不起嵌套的if..else,但会增加可理解   您可以简化ALL三合一的if else。 85%的安全增强功能!

I am currently building a web app in which PHP files are loaded into a main file using jQuery's $.ajax function. However, the PHP files are obviously still accessible outside of the app, by just typing the files name in the address bar.

So my question is what would be the best way to make it so that the PHP file being 'ajaxed' in knows that it is contained in the correct page and will function correctly, but if it is accessed in any other way (even if someone were to make they're own website and AJAX in my PHP file) then the file should say "access denied" or something.

Thanks in advance

解决方案

Concluded Answer after googling a lot !

Step-1 : Generate Token System For All Web-Service:

Generating Token :

<?php
  session_start();
  $token = md5(rand(1000,9999)); //you can use any encryption
  $_SESSION['token'] = $token; //store it as session variable
?>

Step-2 : Use it while sending ajax call:

var form_data = {
  data: $("#data").val(), //your data being sent with ajax
  token:'<?php echo $token; ?>', //used token here.
  is_ajax: 1
};

$.ajax({
  type: "POST",
  url: 'yourajax_url_here',
  data: form_data,
  success: function(response)
  {
    //do further
  }
});

Step-3 : NOW, Let's secure ajax handler PHP file with,

session_start(); //most of people forget this while copy pasting code ;)
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
  //Request identified as ajax request

  if(@isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']=="http://yourdomain/ajaxurl")
  {
   //HTTP_REFERER verification
    if($_POST['token'] == $_SESSION['token']) {
      //do your ajax task
      //don't forget to use sql injection prevention here.
    }
    else {
      header('Location: http://yourdomain.com');
    }
  }
  else {
    header('Location: http://yourdomain.com');
  }
}
else {
  header('Location: http://yourdomain.com');
}

NOTE: SORRY FOR NESTED IF..ELSE, BUT IT INCREASES UNDERSTANDABILITY. YOU CAN SIMPLIFY ALL THREE IN ONE IF ELSE. 85% Security Enhanced !

这篇关于PHP和AJAX安全性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆