通过 PHP 保护 zip + SQL

发布于 2024-09-30 10:55:27 字数 545 浏览 1 评论 0原文

无需深入了解网站其余部分的太多细节,我有一个非常标准的用户设置,其中包含用户表、ID、名称、密码等。有些用户是“免费”用户,有些用户是通过 paypal 付费的,这是由“user_premium”表为 1 或 0。

我想要做的是仅允许在用户有高级版的情况下下载 zip 文件。显然,如果他们不这样做,我可以隐藏页面上的链接,但他们仍然可以直接访问domain.com/myfile.zip。

我尝试阻止通过 htaccess 直接访问 zip,并在 PHP 脚本中使用 fpassthru 来访问该文件,例如(动态示例代码)

if($user->can_download()) {
     $fp = fopen('myfile.zip', 'rb');

     header("Content-Type: application/zip");

     fpassthru($fp);
} else {
     redirect('domain.com/premium.html');
}

但是每次都会出现内存耗尽错误(文件为 4GB)。

还有其他办法解决这个问题吗?

Without going in to, too much detail about the rest of the site I have a pretty standard user setup with a users table, id, name, password etc. Some users are "free" users and some have paid through paypal this is set by the "user_premium" table as a 1 or 0.

What I want to do is only allow the download of a zip file if the user has premium. Obviously I can hide the link on my pages if they don't, but they can still access domain.com/myfile.zip directly.

I tried blocking direct access to the zip via htaccess and used fpassthru in a PHP script to get access to the file e.g. (on the fly example code)

if($user->can_download()) {
     $fp = fopen('myfile.zip', 'rb');

     header("Content-Type: application/zip");

     fpassthru($fp);
} else {
     redirect('domain.com/premium.html');
}

However I got memory exhausted errors each time (the file is 4GB).

Is there another way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放手` 2024-10-07 10:55:27

您应该考虑使用 Apache mod_xsendfile 而不是 fopen。使用 xsendfile,您可以使用 HTTP 标头进行响应,其中包含服务器上文件的路径,Apache 会直接将该文件获取并提供给用户的浏览器,从而允许您的 PHP 脚本完成执行。

您可以在此处找到有关 mod_xsendfile 的入门指南:http://codeutopia.net/blog/2009/03/06/sending-files-better-apache-mod_xsendfile-and-php/

You should look into using Apache mod_xsendfile instead of fopen. With xsendfile, you respond with an HTTP header with the path to the file on your server, which Apache scoops up and serves the file to the user's browser directly, allowing your PHP script to complete execution.

You can find a great guide for getting started with mod_xsendfile here: http://codeutopia.net/blog/2009/03/06/sending-files-better-apache-mod_xsendfile-and-php/

老旧海报 2024-10-07 10:55:27

来自 php 文档中的注释。

虽然在 PHP 源代码上调用“./configure”之前添加 CFLAGS="-D_FILE_OFFSET_BITS=64" 将启用对大文件(大于 2 GB)使用 fopen() 的支持,但请注意 -- 如果这样PHP 的安装与 Apache HTTPD [2.x] 结合使用,即使不提供 PHP 应用程序的输出,Apache 也会变得完全无响应。

为了获得对非 Web 应用程序的大文件支持,同时保持 Apache 的可操作性,请考虑进行两种不同的 PHP 安装:一种在配置期间指定了上述 CFLAGS(用于非 Web 使用),另一种则没有此设置标志(与 Apache 一起使用)。

From comments in the php doc.

While adding CFLAGS="-D_FILE_OFFSET_BITS=64" immediately before calling "./configure" on the PHP source will enable support for using fopen() on large files (greater than 2 GB), note that -- if such an installation of PHP is used in conjunction with Apache HTTPD [2.x], Apache will become completely unresponsive even when not serving output from a PHP application.

In order to gain large file support for non-web applications while maintaining the operability of Apache, consider making two distinct PHP installations: one with the above CFLAGS specified during configuration (for non-web uses), and the other without this flag (for use with Apache).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文