Why is a known PHP file giving a 404?

Bit of an odd one this, I’ve got a PHP file which sometimes gives a 404 error. It’s an ajax callback page for a wordpress plugin I’ve made.

For example:

Read More

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

But this doesn’t: http://ledhdtvtelevisions.com/wp-content/plugins/amazon-affiliate-link-localizer/ajax.php?strAction=search&strLink=http://www.amazon.com/dp/B000IZGIA8

Obviously the PHP file is there or the first link wouldn’t work, so why doesn’t the second link work?

What’s interesting is that the troublesome link work fine on my server using the exact same code: http://petewilliams.info/blog2/wp-content/plugins/amazon-affiliate-link-localizer/ajax.php?strAction=search&strLink=http://www.amazon.com/dp/B000IZGIA8

Sadly I don’t have direct access to the server that is having the problem, but I can ask for changes to be made. It’s not just this site that is having the problem, some other users of the script are having the same issue.

Here’s that file’s source code, there’s not much to it:

<?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";

}

Anyone got any ideas as to what’s up?

Thanks

Pete

Related posts

Leave a Reply

3 comments

  1. The two URLs execute different code paths in your script as the one that works run the checkLinks function as the one that doesn’t work runs searchLink.

    Therefore you can assume that some setting on the server wouldn’t allow some functionality used in searchLink.

    My immediate suspect would be to look at file access permissions used in file_get_contents

  2. Code looks ok. Looks like the code is calling searchLink(), and tries to determine whether to use the url available in a canonical reference in the dom, (<link rel="canonical" href="https://rads.stackoverflow.com/amzn/click/com/B000IZGIA8" rel="nofollow noreferrer" />), or the link passed in the url.

    I think your best bet is to tail the php error log on the server and see which errors are being logged. If you have shell access to the server you can issue the following commands:

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

    Now that you are tailing the error log, run the same script and see whats being logged.

    — Edit —

    Sorry didn’t see the part where you don’t have access to the production server. Maybe tail the error log on your dev server, even though the script may appear to work it still may be logging some info in the background.

  3. You have a faulty URL rewrite, this is not getting 404 :-

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

    But these will hit into 404:

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

    It seems the / (even is in urlencoded) have been taken into consideration as part of the rewrite,
    checkout your rewrite (.htaccess or in apache configuration, or if you are using PHP script)