SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    printer friendly code help

    I have a printer friendly script that properly displays the requested page if its in the current directory but it won't let me go outside the directory.

    So I have a link like this and it works fine:
    domain.com/print.php?page=file.shtml

    But when I have a link like this on the same domain, it doesn't work; even if I put the full url to the file:
    domain.com/print.php?page=/directory/file.shtml
    or
    domain.com/print.php?page=domain.com/directory/file.shtml

    I got the script from phpbuilder.com http://www.phpbuilder.com/tips/item.php?id=171

    Here's the basic guts of the script:

    PHP Code:
    <?
    ereg
    ('^.*/',$SCRIPT_FILENAME,$tmp);
    $page_path substr($tmp[0],0,-1);

    if (!
    file_exists("$page")) {
    $fcontents join(''file("$page"));
    echo 
    $fcontents;
    }
    ?>
    Any suggestions are appreciated!
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  2. #2
    ********* Addict
    Join Date
    Apr 2000
    Location
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, when you try domain.com/print.php?page=/directory/file.shtml it's trying to find the /directory/file.shtml file on the hard drive - and that's not what you want. The same thing happens when you try domain.com/print.php?page=domain.com/directory/file.shtml except that it is relative from the current directory. What wou probably want is this:
    PHP Code:
    if (!ereg("..",$page)) {
        echo 
    "<h3>What d'you think you're doing?</h3>";
        exit;
    }
    $docroot "/usr/home/www/jim/domain.com/";
    if (!
    file_exists("$docroot$page")) {
        
    $fcontents join(''file("$docroot$page"));
        echo 
    $fcontents;

    I haven't tested the code, by the way.

    Note how it checks the user isn't trying to access some other file on the hard drive outside the docroot. You'll need to make this much safer though, depending on your needs. For example, you don't want users accessing pages protected by .htacess files...

  3. #3
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code makes perfect sense. However, one little glitch that I should have mentioned in my original post.

    I would like to use this script across multiple domains on the same server and the $docroot line would only work on one of them. Is there a way that I can somehow have a "dynamic" $docroot where it is determined automatically?

    Thanks for your time.
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •