Error control handling

Discussion in 'Co-op Advertising Network' started by Cygnus, May 11, 2005.

  1. #1
    The coop is working fine for me, but I miss the benefits of 404 redirect traffic -- now if a non-existant HTML file is requested, the page returned is the error...
    Warning: file_get_contents(non_exist.html): failed to open stream: No such file or directory

    Going into the code I'm trying to think of an ELSE statement to add; essentially, if the file doesn't exist, redirect elsewhere. How do I do that? Here's the normal code.

    <?php

    if (!function_exists('file_get_contents')) {
    function file_get_contents($url) {
    $handle = fopen($url, 'r');
    $string = fread($handle, 4096000);
    fclose($handle);
    return $string;
    }
    }

    Thank you to those who know PHP better than me.
     
    Cygnus, May 11, 2005 IP
  2. Cygnus

    Cygnus Cat Herder

    Messages:
    172
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #2
    I tried...

    <?php

    if (!function_exists('file_get_contents')) {
    function file_get_contents($url) {
    $handle = fopen($url, 'r');
    $string = fread($handle, 4096000);
    fclose($handle);
    return $string;
    } else {
    header("Location: http://test.com");
    }

    but that just sends the user on to test.com no matter what...
     
    Cygnus, May 11, 2005 IP
  3. Cygnus

    Cygnus Cat Herder

    Messages:
    172
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #3
    I realize that this isn't the sexiest of subjects, but there is a really good reason why I want to figure this out. Allow me to explain.

    1. Some hosts are case sensitive; Index.html may not exist when index.html does exist.
    2. I noticed that MSN has a nasty habit of caching Coop error pages because there are more references to a particular page in one case over another.
    3. A malicious individual could toy with Coop members, testing to see if the host is case-sensitive -- if so, many valid pages could be cached with invalid information by pointing links to the opposite case filenames.

    Hopefully someone else out there sees this as I do; my knowledge of PHP is limited, but if there is an easier way to write the passthru.php to test for a file existing first, and on an error, skipping everything else, that'd be wonderful.

    Cygnus
     
    Cygnus, May 12, 2005 IP
  4. chachi

    chachi The other Jason

    Messages:
    1,600
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Doesn't look like you are getting much help here. :)

    I thought everything after the "/" was case sensitive no matter what kind of web server you are running...I have never seen anything different. I am sure Shawn is going to want to be the one to make the changes to the PHP as far as the coop is concerned.

    Did you ever make that trip out to Sonoma?
     
    chachi, May 12, 2005 IP
  5. Cygnus

    Cygnus Cat Herder

    Messages:
    172
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Been learning some PHP in order to fix the problem on my end (good learning experience).

    Sonoma and Napa will be July 30-Aug 3...looking forward to it.
     
    Cygnus, May 12, 2005 IP
  6. Cygnus

    Cygnus Cat Herder

    Messages:
    172
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #6
    Not that there was really any interest in this, but I solved the problem at the mod_rewrite level so that only valid files are sent through to passthru.php in the first place.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} ^(.*)\.htm [NC,OR]
    RewriteCond %{REQUEST_FILENAME} ^(.*)\.html [NC]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(.*) /passthru.php?file=$1
    </IfModule>
     
    Cygnus, May 13, 2005 IP
    jazzylee77 likes this.
  7. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #7
    You can do that nice an clean with curl. There are controls for how long the call for the remote page should wait and such.

    There is an example here. It would be much cleaner and efficent code then the mod rewrite engine.
     
    noppid, May 13, 2005 IP
  8. jazzylee77

    jazzylee77 Peon

    Messages:
    578
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks Cygnus...I've spent the past few hours looking at this problem!
     
    jazzylee77, Sep 8, 2005 IP