【问题标题】:Why is a known PHP file giving a 404?为什么一个已知的 PHP 文件会给出 404?
【发布时间】:2012-01-17 18:17:04
【问题描述】:

有点奇怪,我有一个 PHP 文件,有时会出现 404 错误。这是我制作的一个wordpress插件的ajax回调页面。

例如:

这行得通: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";

}

有人对发生了什么有任何想法吗?

谢谢

皮特

【问题讨论】:

    标签: php javascript ajax wordpress http-status-code-404


    【解决方案1】:

    这两个 URL 在您的脚本中执行不同的代码路径,因为有效的 URL 运行 checkLinks 函数,而无效的 URL 运行 searchLink

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

    我的直接怀疑是查看file_get_contents 中使用的文件访问权限

    【讨论】:

      【解决方案2】:

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

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

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

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

      -- 编辑--

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

      【讨论】:

        【解决方案3】:

        你有一个错误的 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)已被考虑为重写的一部分,
        检查您的重写(.htaccess 或在 apache 配置中,或者如果您使用的是 PHP 脚本)

        【讨论】:

          猜你喜欢
          • 2023-03-05
          • 2020-09-24
          • 1970-01-01
          • 2019-10-08
          • 2018-12-23
          • 1970-01-01
          • 2023-03-28
          • 1970-01-01
          • 2011-09-11
          相关资源
          最近更新 更多