Simple PHP Include Question

Discussion in 'PHP' started by PalSys, Nov 20, 2008.

  1. #1
    I'm at the very low-end of the totem pole when it comes to PHP programming and I'm finding myself stuck on a simple include.

    Let's say I have a file at http://www.mysite.com/1/2/test.php. In this file I want to be able to echo some variable, so the only code in the file is:

    <?php echo $variable; ?>

    Now, I want to include this variable from another file that will hold all variables; let's call it config.php. That file is located at http://www.mysite.com/3/4/config.php.

    When I add this code to the top of test.php to include the file I get no output:

    <?php include("http://www.mysite.com/3/4/config.php"); ?>

    And, of course, when I throw the config file into the same directory as test.php and call it as:

    <?php include 'config.php'; ?>

    the output is perfect.

    So, assuming the above makes sense, my problem is that I'm not able to pass my variables via the URL include. I need an easy way to store a bunch of variables in one file and include that variables file on many other pages, spread over the structure of the site, preferably via an URL include.

    So: is this possible or is there an even better alternative?

    Thanks in advance!
     
    PalSys, Nov 20, 2008 IP
  2. SEOAnalytic.com

    SEOAnalytic.com Member

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #2
    You should use relative paths in include and require procedures.
    Instead of include("http://smth.tld/include.php"); use include("./include.php");
    I found a better alternative to use external php files in your script with the procedure readfile . I didn't work with it, but you can read about it on php.net.
     
    SEOAnalytic.com, Nov 20, 2008 IP
  3. PalSys

    PalSys palsys.io

    Messages:
    2,628
    Likes Received:
    224
    Best Answers:
    0
    Trophy Points:
    230
    #3
    Thanks for the info! I understand now that this is a parsing problem - a bit of experimenting lead me to use absolute paths so I think I'll stick with that far now but I'll definitely look into the readfile function :)
     
    PalSys, Nov 20, 2008 IP
  4. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Its because inclusion of remote file is prohibited in most server because of RFI vulnerability.
     
    harrisunderwork, Nov 20, 2008 IP
  5. itliberty

    itliberty Peon

    Messages:
    1,173
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes, you have to rememer that php is server side code. Therefore it takes on the perspective of the server it is running on. A URL (http://) is a client side request.

    So in php you have to use ./ (same directory) or ../ (one directory back) as that is how the server relates things.
     
    itliberty, Nov 20, 2008 IP
  6. itliberty

    itliberty Peon

    Messages:
    1,173
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Now you could do what you wanted to do but you need to use things like file_get_contents and then http is acceptable.
     
    itliberty, Nov 20, 2008 IP
  7. alpha42

    alpha42 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    But even then, if your host has locked down fopen to not allow http:// requests, it will still fail. :(

    Alot of places I've seen have disabled http fopens, due the number of scripts that were written poorly and could be exploited via it. :(
     
    alpha42, Nov 20, 2008 IP
  8. eric90

    eric90 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    use a relative link.

    so have all the variables defined in say config.php

    and on each page just use include("config.php");
     
    eric90, Nov 21, 2008 IP
  9. gzav

    gzav Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    In config.php I think you would have to do
    include('../../1/2/test.php');
    Code (markup):
     
    gzav, Nov 21, 2008 IP
  10. AdultProfiles

    AdultProfiles Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    allow_url_fopen needs to be ON if u want to use an external file
     
    AdultProfiles, Nov 21, 2008 IP