为什么一个已知的PHP文件给出404 ?


Why is a known PHP file giving a 404?

有点奇怪,我有一个PHP文件,有时会给出404错误。这是一个ajax回调页面的wordpress插件我已经做了。

例如:

这个工作:http://ledhdtvtelevisions.com/wp-content/plugins/amazon-affiliate-link-localizer/ajax.php?strTld=co.uk&strAffiliateId=pcrev05&strLinks=B001JKTC9A|B0015TG12Q

但这不是:http://ledhdtvtelevisions.com/wp-content/plugins/amazon-affiliate-link-localizer/ajax.php?strAction=search&strLink=http://www.amazon.com/dp/B000IZGIA8

显然PHP文件在那里或者第一个链接不起作用,那么为什么第二个链接不起作用呢?

有趣的是,这个麻烦的链接在我的服务器上使用完全相同的代码工作得很好:http://petewilliams.info/blog2/wp-content/plugins/amazon-affiliate-link-localizer/ajax.php?strAction=search&strLink=http://www.amazon.com/dp/B000IZGIA8

遗憾的是,我不能直接访问有问题的服务器,但我可以要求进行更改。不只是这个网站有问题,脚本的其他一些用户也有同样的问题。

这是该文件的源代码,没有太多内容:

<?php
header("Content-type: application/javascript");
switch ( $_REQUEST['strAction'] ) {
    case 'search':
        searchLink();
        break;
    case 'version':
        echo "1.7b";
        break;
    default:
        checkLinks();
        break;
}
function checkLinks() {
    // get URL
    $strTld         = $_REQUEST['strTld'];
    $strAffiliateId = $_REQUEST['strAffiliateId'];
    $strLinks       = $_REQUEST['strLinks'];
    $arrLinks       = explode( '|', $strLinks );
    foreach ( $arrLinks as $strAsin ) {
        $strLink = "http://www.amazon.$strTld/exec/obidos/ASIN/$strAsin/$strAffiliateId";
        $arrHeaders = get_headers($strLink, 1);
        // if not found, then search for it
        if ( strpos( $arrHeaders[0], '404' ) || strpos( $arrHeaders[1], '404' ) ) {
            echo "arrLinksToCheck[ '$strAsin' ].searchLink();'n";
        } else {
            echo "arrLinksToCheck[ '$strAsin' ].localiseLink();'n";
        }
    }
}
function searchLink() {
        $strHtml = file_get_contents( $_REQUEST['strLink'], false, null, -1, 100000 );
        $strPattern = '/canonical" href="http:'/'/(.*)'/(.*)'/dp'/([A-Z0-9]{10})/';
        preg_match( $strPattern, $strHtml, $arrMatches );
        $strTitle = str_replace(  '-', '%20', $arrMatches[2] );
        // the canonical ASIN is sometimes different to the original one which confuses the JS, so use the one in the original link
        $strPattern2 = '/'/([A-Z0-9]{10})/';
        preg_match( $strPattern2 , $_REQUEST['strLink'], $arrUrlMatches );
        $strAsin = is_array( $arrUrlMatches ) ? $arrUrlMatches[1] : $arrMatches[3];
        echo "arrLinksToCheck[ '{$strAsin}' ].writeSearchLink( '$strTitle' );'n";
}
有谁知道是怎么回事吗?

感谢皮特

这两个url在脚本中执行不同的代码路径,因为工作的url运行checkLinks函数,而不工作的url运行searchLink

因此,您可以假设服务器上的某些设置不允许searchLink中使用的某些功能。

我的直接怀疑是查看file_get_contents

中使用的文件访问权限

代码看起来不错。看起来代码正在调用searchLink(),并试图确定是使用dom中的规范引用(<link rel="canonical" href="https://rads.stackoverflow.com/amzn/click/com/B000IZGIA8" rel="nofollow noreferrer" />)中可用的url,还是使用url中传递的链接。

我认为最好的办法是跟踪服务器上的php错误日志,看看哪些错误被记录了。如果您拥有对服务器的shell访问权限,您可以发出以下命令:

 php -i | fgrep error_log # this will give you the location of the error file
 tail -f /path/to/error/log

现在您正在跟踪错误日志,运行相同的脚本并查看记录的内容。

—Edit—

对不起,我没有看到您无法访问生产服务器的部分。也许在开发服务器上跟踪错误日志,即使脚本可能看起来工作,它仍然可能在后台记录一些信息。

你有一个错误的URL重写,这是没有得到404:-

ajax.php?strAction=search&strLink=www.amazon.com

但是这些会进入404:

ajax.php?strAction=search&strLink=http://www.amazon.com
ajax.php?strAction=search&strLink=http%3A%2F%2Fwww.amazon.com

似乎/(即使是在urlencoded)已被考虑作为重写的一部分,
签出您的重写。