使用curl代替fopen的问题

发布于 2024-09-25 09:14:11 字数 1821 浏览 1 评论 0原文

因此,我一直在尝试修复远程访问页面上的 php 脚本。最初它会使用 fopen 来判断站点是否可用。如果是,用户将被定向到该站点,如果它已关闭,则将测试下一个 url,如果成功则将用户重定向到那里,等等。

出于安全原因,我们新的共享托管站点不允许 fopen,因此脚本具有有待修改。我已经尽力拼凑一些东西,但感觉我遗漏了一些东西,可能非常基本。

该脚本是三个简单的 php 文件。第一个是index.php(它在Joomla 上运行,所以我假设这就是它的设置方式),它看起来像这样:

<?php
error_reporting(E_ALL);
ini_set('display_errors',TRUE);
$counter = 1;
include ("URLs.php");
include ("functionscript.php");

error_reporting(0);

while ( $counter <= 5 ) {
$webURL = "URL".$counter;
redirectIfValid( ${"URL".$counter} );
$counter = $counter + 1;
}
?>

URLs.php 只是要按顺序尝试的4 个站点的列表。 functionscript.php 是我认为的主要内​​容,我尝试用 CURL 而不是 fopen 进行黑客攻击:

<?
function redirectIfValid($webURL)
{
ini_set("default_socket_timeout","05");
set_time_limit(5);

$ch=curl_init();

curl_setopt ($ch, CURLOPT_URL, $webURL);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch,CURLOPT_VERBOSE,false);

curl_setopt($ch, CURLOPT_TIMEOUT, 5);

$page=curl_exec($ch);

//echo curl_error($ch);

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

if($httpcode>=200 && $httpcode<300) return true;

else return false;

if($httpcode==true) {
    header('Location:'.$webURL);
    exit;
        }
       else {
    }
}
?>  

这个 scipt 运行的最终结果是 zip。如果我右键单击并查看加载的页面上的源代码,它只会在页面下方显示“1, 2, 3”。我确信我错过了一些非常简单的东西,但我的编程能力只是垃圾,并且会很感激有人关注这一点。我认为curl 的东西应该没问题,但想知道我是否缺少index.php 和functionscript.php 之间的一些空白链接。

编辑:因此,在阅读下面的贝琳达建议后,我更改了代码来阅读此内容(我只打印了已更改的行,这些行全部位于脚本末尾):

if($httpcode>=200 && $httpcode<300) {

    header('Location:'.$webURL);

    exit;
    }
        else {
        }
}
    ?>

代码似乎仍然失败,尽管奇怪的是现在是当您查看源代码时,将在前面提到的“1、2、3”中添加“4”。我只能认为这是积极的!有没有人可能还有更多的宝石可以寄给我,因为我必须承认,我发现这是一次真正的考验,尽管是一次愉快的考验。

So, I have been lumped with trying to fix a php script we have on our remote access page. Originally it would use fopen to if a site was available. If it was the user would be directed to that site, if it was down the next url would be tested and the user redirected there if it was successful etc etc.

Our new shared hosting site does not allow fopen for security reasons so the script has to be ammended. I have tried my best to cobble something together but feel I am missing something, probably quite fundamental.

The script is three simple php files. The first is index.php (this is running on Joomla so I assume this is how it has to be setup) and it looks like this:

<?php
error_reporting(E_ALL);
ini_set('display_errors',TRUE);
$counter = 1;
include ("URLs.php");
include ("functionscript.php");

error_reporting(0);

while ( $counter <= 5 ) {
$webURL = "URL".$counter;
redirectIfValid( ${"URL".$counter} );
$counter = $counter + 1;
}
?>

URLs.php is just a list of the 4 sites that are to be tried in order. functionscript.php is the main meat and veg of the thing I suppose and where I have tried my dab hand at hacking in CURL instead of fopen:

<?
function redirectIfValid($webURL)
{
ini_set("default_socket_timeout","05");
set_time_limit(5);

$ch=curl_init();

curl_setopt ($ch, CURLOPT_URL, $webURL);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch,CURLOPT_VERBOSE,false);

curl_setopt($ch, CURLOPT_TIMEOUT, 5);

$page=curl_exec($ch);

//echo curl_error($ch);

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

if($httpcode>=200 && $httpcode<300) return true;

else return false;

if($httpcode==true) {
    header('Location:'.$webURL);
    exit;
        }
       else {
    }
}
?>  

The end result of this scipt running is zip. If I right click and view source on the page that does load it just has a "1, 2, 3" going down the page. I am sure I have missed something dead simple but my programming ability is just shy of rubbish and would be grateful of someone casting an eye over this. I think the curl stuff should be fine but wonder if I am missing some gaping link between the index.php and functionscript.php.

EDIT: So after reading Belindas suggestion below I have changed the code to read this (I have only printed the lines that have changed which were all at the end of the script):

if($httpcode>=200 && $httpcode<300) {

    header('Location:'.$webURL);

    exit;
    }
        else {
        }
}
    ?>

The code still seems to fall over though oddly it is now to be adding a "4" to the previously mentions "1, 2, 3" going down the page when you view the source. I can only view this as a positive! Does anyone possibly have any more gems they can send my way as I must admit I am finding this a real trial by ordeal, though an enjoyable one.

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

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

发布评论

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

评论(2

雪化雨蝶 2024-10-02 09:14:11

不会到达 header 语句,因为您在到达之前返回 true 或 false。而不是在 200 到 300 之间的 httpcode 上返回 true 重定向。不确定这是否能解决问题,但应该有所帮助。

The header statement won't be reached because you are returning either true or false before it is reached. Instead of returning true redirect on httpcode being between 200 and 300. Not sure if this will solve it but should help.

揽清风入怀 2024-10-02 09:14:11

添加此内容,curl 将遵循服务器指示:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Add this, curl will follow the server indications:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

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