SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member Games Junction's Avatar
    Join Date
    Dec 2004
    Location
    England
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Doesn't like absolute paths?

    Hey,

    Ok i have a script that im creating. At the moment it includes 3 files:

    includes/session.php - contains a counter and starts the session
    includes/config.php - contains lots of variables
    includes/connection.php - connects to the database

    In config.php i have noticed that it works fine when i try to do something like:

    PHP Code:
    $langfile 'language/lang-english.php'
    but as soon as i make it link to the site using the url:

    PHP Code:
    $langfile 'http://www.example.com/gamescript/language/lang-english.php'
    It wont load the variables from it. This also happens when i try to include any other script using an url path. Any ideas on how to fix! I need to use an url path. And no im not using any htaccess.

    Thanks in advance
    Beaver6813 SRC Hosting CEO
    www.srchosting.com - Safe. Reliable. Low Cost Web Hosting

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Location
    UK
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this is a security feature, to stop people from getting your script to include a file on their server.

  3. #3
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're using an url, not an absolute path. Absolute paths are independent of the location of the file which use them. E.g. /main.php or C:/Websites/main.php are absolute paths, but you're using an url.

    Quote Originally Posted by PHP Manual
    If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Appendix M for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.

  4. #4
    SitePoint Member Games Junction's Avatar
    Join Date
    Dec 2004
    Location
    England
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah i fixed it! Read up on the PHP.net documenation. Because of the way PHP parses the script it means that you have to rename your included files, if your going to use a url to something like config.inc.
    Something not previously stated here - but found elsewhere - is that if a file is included using a URL and it has a '.php' extension - the file is parsed by php - not just included as it would be if it were linked to locally.

    This means the functions and (more importantly) classes included will NOT work.

    for example:

    include "http://MyServer.com/MyInclude.php";

    would not give you access to any classes or functions within the MyInclude.php file.

    to get access to the functions or classes you need to include the file with a different extension - such as '.inc' This way the php interpreter will not 'get in the way' and the text will be included normally.
    Case Solved!

    EDIT: Thanks for the quick replies guys
    Beaver6813 SRC Hosting CEO
    www.srchosting.com - Safe. Reliable. Low Cost Web Hosting

  5. #5
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I would strongly suggest against doing that. As if you rename the file ending then everyone can read your php script, and that can be a huge security risk.

    Instead use real abselute paths as mentioned by Floriam.

  6. #6
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Concur with RedDevil. Very, very bad idea.

    Much better idea would be to include the file like: /home/example/public_html/gamescript/language/file.icn

  7. #7
    SitePoint Member Games Junction's Avatar
    Join Date
    Dec 2004
    Location
    England
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, point taken. So how would it know that its an absoloute path?

    PHP Code:
    <?
    include "/home/example/public_html/gamescript/language/file.icn"?>
    Like the above?
    Beaver6813 SRC Hosting CEO
    www.srchosting.com - Safe. Reliable. Low Cost Web Hosting

  8. #8
    SitePoint Wizard bronze trophy Tyssen's Avatar
    Join Date
    Oct 2005
    Location
    Brisbane, QLD
    Posts
    4,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may find that this is better:

    Code:
        <?php include $_SERVER['DOCUMENT_ROOT']."/gamescript/language/file.icn"; ?>

  9. #9
    SitePoint Member Games Junction's Avatar
    Join Date
    Dec 2004
    Location
    England
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok,

    Went to php.net again :P

    This appears to work, anyone found any problems with this, this gets the absolute path to my directory that the script is in, regardless of my mod_rewrite:

    PHP Code:
    <?
    $absolpath 
    dirname($_SERVER['SCRIPT_FILENAME']); 
    require 
    $absolpath."/includes/config.php";?>
    EDIT: How would i link to a stylesheet using an absolute path? Ive tried doing
    Code:
    <link href="<?=$absolpath."/css/";?>gamepoint.css" rel="stylesheet" type="text/css">
    But it doesn't work
    Beaver6813 SRC Hosting CEO
    www.srchosting.com - Safe. Reliable. Low Cost Web Hosting

  10. #10
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    You only use absolute path on files that the script will use to parse the content to the browser. For css, images etc that is read and displayed by the browser you should use either relative placement or the full url.
    I.e. "./css/gamepoint.css" or "http://www.siteurl.com/css/gamepoint.css"

  11. #11
    SitePoint Member Games Junction's Avatar
    Join Date
    Dec 2004
    Location
    England
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, is there anyway i can use php to generate the url path to my directory?

    Maybe by modifying $absolpath = dirname($_SERVER['SCRIPT_FILENAME']); into a url... somehow? :S
    Beaver6813 SRC Hosting CEO
    www.srchosting.com - Safe. Reliable. Low Cost Web Hosting

  12. #12
    SitePoint Member Games Junction's Avatar
    Join Date
    Dec 2004
    Location
    England
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok i got it

    $relpath = dirname($_SERVER['SCRIPT_NAME']);

    Thanks everyone for your help! Im sorted
    Beaver6813 SRC Hosting CEO
    www.srchosting.com - Safe. Reliable. Low Cost Web Hosting


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
  •