1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

include function help

Discussion in 'PHP' started by Sanzbar, Dec 29, 2007.

  1. #1
    So, I just got a programmer to write a great mapping script for one of my sites. His code requires PHP5 for some of the functions which it uses. This seemed like no problem, but when I switched over, it has broken some of the nested includes on some of my PHP pages.

    I think I have identified the issue. For PHP5, my server does not allow full URL references:

    so, include("http://www.site.com/file.php");

    has to be

    include("file.php");

    for security concerns.

    I've decided it would be best to replace the full references and start doing it the 'proper' way with relative references; however, I have noticed a very odd impact...I now have variable dependencies that were not there before.

    I tend to use very common naming conventions in my different files which are often the same ($i, $row, $sql_query, etc). This was never a problem before, it actually helped w/ code re-use, but now it seems to be a major issue.

    So, for example, below is a scenario where this messes things up:

    <?php
    $i = "what i want to print";

    // an include of file.php is called which might be a common file i use a lot but which also creates an $i variable which is different than above
    include("file.php");

    // the code now prints the $i from file.php rather than the one called above, which never happened with the full URL include reference path

    echo $i;
    ?>

    This is not an exact example, as what I'm dealing with is much more complex, but it should give you the idea. Basically, a full URL reference seems to parse the code before brining it in, whereas a relative reference seems to bring in the code and then parse it. This is a subtle difference, but one that impacts much of the code on my site.

    Any ideas of how I can either parse the relative include files before bringing them in or use other functions so that I don't have to majorly re-write a ton of my code?

    Thanks!
     
    Sanzbar, Dec 29, 2007 IP
  2. DarkMindZ

    DarkMindZ Guest

    Messages:
    175
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you will need to rewrite your code, sorry.

    PHP takes the latest variable declaration, and once you include a file that has $i, it will save that instead of the one above it.
     
    DarkMindZ, Dec 29, 2007 IP
  3. Sanzbar

    Sanzbar Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks.

    Do you have any idea why it works with the full URL, though?
     
    Sanzbar, Dec 29, 2007 IP
  4. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Do you need only output from the include file? Can you use fopen()?
     
    hogan_h, Dec 29, 2007 IP
  5. Sanzbar

    Sanzbar Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I should only need output, so that might work. I can't seem to get fopen to do anything...

    When I do fopen("file.php","r"); it seems to return nothing...even for standard html files w/ no php...ie fopen("text.hml","r");

    Any ideas as to what parameter to use or as to why it would return nothing when the include works (even when it's just standard html)?

    What's the main difference between fopen and include?
     
    Sanzbar, Dec 29, 2007 IP
  6. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you want to include files using http then you will need to go into your php file and set allow_url_fopen to on.

    This isn't a good approach though because you're opening a http connection for every file which puts unnecessary load on your webserver, ideally your programmer would have known this. As for your variables, maybe you can use different naming conventions. Traditionally $i is only used for counters and loops.

    In your case, fopen won't read any contents, it just creates a handler that you CAN use to read the contents... Try this instead:

    $output = file_get_contents('file.php');

    The differences are that include parses the php inside a file. file_get_contents actually reads the contents of the file so you'll actually have the php code inside $output.

    Hope this helps :)
     
    tonybogs, Dec 29, 2007 IP
  7. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Just use full url path, actually you must use full path if you only want the code output.

    Try this actually (file_get_contents should be suited better for what you want):
    
    $include_content = file_get_contents("http://www.site.com/file.php");
    echo $include_content;
    
    Code (markup):
    If that doesn't work, you could always use cUrl.
     
    hogan_h, Dec 29, 2007 IP
  8. Sanzbar

    Sanzbar Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I do need it to parse before bringing it in, which file_get_contents and fopen() don't seem to do for me.

    I might have to just suck it up and rewrite the code that I'm including to eliminate the variable redundancies. :mad:
     
    Sanzbar, Dec 29, 2007 IP
  9. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #9
    Try using the IP address instead of the url.

    Peace,
     
    Barti1987, Dec 29, 2007 IP
  10. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I think that your fopen() is ok, otherwise PHP should output an E_WARNING notice !!!

    To allow PHP to fopen/include files on remote servers, you have to set allow_url_fopen/allow_url_include to 1 on your php.ini file
     
    selling vcc, Dec 30, 2007 IP