如何防止PHP的file_get_contents() [英] how to prevent PHP's file_get_contents( )

查看:740
本文介绍了如何防止PHP的file_get_contents()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个php页面返回如下数据:

one of my php page returns data like this:

<?php
  //...
  echo "json string";
?>

但是其他人使用file_get_contents()来获取我的数据并在其他网站上使用。

but someone else use file_get_contents() to get my data and use in other website.

任何人都可以告诉我,我该怎样做才能防止这种事情发生。

can anybody tell me what can i do to prevent such thing happen.

我考虑是否可以获得请求的域名以回应别的东西。但我不知道

i consider if i can get the request's domain name to echo something else.but i dont know

函数来获取请求的域名。如果请求是由服务器发送的,那么

the function to get request's domain name.and if the request is sent by a server,that

将是无益的。我的英语很差,表示怀疑,请耐心等待。

will be unhelpful. My English is poor, to express doubts, please bear with.

推荐答案

你也可以使用会话。如果应用程序中的某个位置,在用户获取json数据之前,您启动会话,然后在此页面中输出json数据,您可以检查会话变量。这种方式只有已通过会话生成器页面的用户才能查看您的输出。
假设您有生成会话的页面A.php。在输出此页面中的任何内容之前使用此代码。

you can also use sessions. if somewhere in your application, before the user gets the json data, you start a session, then in this page where you are outputting json data, you can check for the session variable. this way only users that have passed the session generator page, can view your output. suppose you have page A.php that generates the session. use this code before outputting anything in this page.

session_start();
$_SESSION['approvedForJson'] = true;

然后在您输出json数据的页面中,在输出任何内容之前,再次调用session_start()。 PHP代码的开头是调用它的好地方。
然后在输出json数据之前,检查是否存在已批准用户的会话变量。

then in your page where you are outputting json data, before outputting anything, call session_start() again. the beginning of your PHP code is a good place to call it. then before outputting the json data, check if the session variable for approved users exists, or not.

if ( isset($_SESSION['approvedForJson']) && $_SESSION['approvedForJson'] ) {
    echo "json data";
} else {
  // bad request
}

这篇关于如何防止PHP的file_get_contents()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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