简体   繁体   English

限制对php文件的访问

[英]Restricting access to php file

I'm currently writing an Android app at the moment, that accesses a PHP file on my server and displays JSON data provided by my MYSQL database. 目前,我目前正在编写一个Android应用程序,该应用程序访问服务器上的PHP文件并显示由MYSQL数据库提供的JSON数据。

Everything works great and I love the simplicity of it, but I'm not too comfortable with the fact that someone could just type in the URL of this PHP file and be presented with a page full of potentially sensitive data. 一切都很好,我喜欢它的简单性,但是我对有人可以只输入此PHP文件的URL并显示一个充满潜在敏感数据的页面这一事实不太满意。

What advice would you give me to prevent access to this PHP file from anyone except those using my android app? 您会给我什么建议,以防止除使用我的Android应用程序的用户以外的任何人访问此PHP文件?

Thanks very much for any information. 非常感谢您提供任何信息。

The keyword is authentication. 关键字是认证。 HTTP-Authentication is designed just for that purpose! HTTP认证就是为此目的而设计的!

There are 2 forms of HTTP-auth: HTTP-auth有两种形式:

  1. Basic: easy to setup, less secure 基本:易于设置,安全性较低
  2. Digest: harder to setup, more secure 摘要:较难设置,更安全

    • Here is the php manual. 是php手册。
    • And this is what you can do in your android app. 就是您可以在android应用中执行的操作。

There isn't really a fool-proof way to do this. 确实没有做到这一点的简单方法。 However you can require the user agent to match that of your application. 但是,您可以要求用户代理与您的应用程序匹配。 You can also hide a private key in your application that is passed as POST data to your PHP file. 您还可以在应用程序中隐藏私钥,该私钥作为POST数据传递到PHP文件。 Now, neither of these will stop someone who is determined to get at the raw output, but it will slow down the people who are just screwing around killing a little time seeing what they can accomplish. 现在,这一切都不会阻止一个决心获得原始产出的人,但是,这只会减慢那些只是费时费力地花些时间看看自己能完成什么的人。

Why not only enable a valid response if the request is sent with the following header: 如果使用以下标头发送请求,为什么不只启用有效的响应:

Content-Type=application/json

If the request doesn't pass it as the Content-Type , then you just terminate the script (as regular browsers usually want to get text/html or similar things). 如果请求没有将其作为Content-Type传递,则您只需终止脚本(因为常规浏览器通常希望获取text/html或类似内容)。 It's not really worth locking everything tight shut, as if your app can get the data from your server, any user would have the opportunity too. 锁定所有内容并不是真正值得的,就像您的应用程序可以从服务器中获取数据一样,任何用户也有机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM