How do I make the include function url absolute?

Discussion in 'PHP' started by Imozeb, Feb 28, 2010.

  1. #1
    How do I make the include function url absolute? For example instead of this:

    <?PHP include("../folder2/folder3/file.php"); ?>

    I want this:

    <?PHP include("http://www.mysite.com/folder1/folder2/folder3/file.php"); ?>


    Can I do this?

    Thanks!

    ~imozeb :)
     
    Imozeb, Feb 28, 2010 IP
  2. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It depends on your server. Most servers have the configuration variable allow_url_include set to off. It's not terribly secure, so relative path (your first example) are highly preferred. However, if it is set to on, you may be able to use the URL includes.

    To check if it's on, create a new php file and place the following information in it
    <?php phpinfo(); ?>
    Code (php):
    Under the "Configuration" heading, allow_url_include is close to the top, the third or fourth down. If it's set to off, you can't use the url includes, and dimes to dollars, your host won't change the setting for you.
     
    Altari, Feb 28, 2010 IP
  3. rockyg

    rockyg Peon

    Messages:
    230
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You could also use the full path to the file you want from the root. On linux this might look somthing like:

    /home/ME/MYSITE/older2/folder3/file.php
     
    rockyg, Feb 28, 2010 IP
  4. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    echo file_get_contents('http://www.mysite.com/folder1/folder2/folder3/file.php');
     
    krsix, Feb 28, 2010 IP
  5. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #5
    if you start it with a / it goes from the root, if you dont include the / it will start at whatever folder is currently open. so you can do:
    
    include("/folder1/folder2/folder3/file.php");
    
    PHP:

    what does that have to do with his question?
     
    killaklown, Feb 28, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It doesn't work. :( It says that there is no such file in my folder. It doesn't seem to want to start from the root (most top) folder, it just takes the folder of the file that is open and checks there. Plz help me!
     
    Imozeb, Mar 1, 2010 IP
  7. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #7
    Did you check what Altari posted?
     
    Sky AK47, Mar 1, 2010 IP
  8. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I did and it is set to off. I was just wondering if there is any workaround like Killaklown tried to do. His code didn't work though. Does anyone know a workaround?
     
    Imozeb, Mar 1, 2010 IP
  9. rockyg

    rockyg Peon

    Messages:
    230
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Are you on linux or windows server? Starting with the / should work on linux
     
    rockyg, Mar 1, 2010 IP
  10. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It will only work if the server holding the file is outputting the source of the PHP file and not the output of a command etc for example echo.

    You can use file_get_contents on the file to get its source and perform an eval() on it if you don't want to worry about external include permissions.
     
    Brandon.Add.On, Mar 1, 2010 IP
  11. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I'm using windows server.
     
    Imozeb, Mar 1, 2010 IP
  12. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    You could also try readfile(), but that might not work either. Usually, if the url access is set to off, you can't get anything with a URL value
     
    Altari, Mar 1, 2010 IP
  13. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #13
    Even for a windows server see the full path of your document root, and use that to include files. For example if your document root is: C:/wamp/www/
    and your script is inside www/codes/file.php you can use:
    include('C:/wamp/www/codes/file.php');

    Thanks :)
     
    JEET, Mar 1, 2010 IP
  14. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #14
    If the fopen wrapper isn't enabled on your server, you cannot use http requests in include.
     
    elias_sorensen, Mar 2, 2010 IP