+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: PHP include external file!!

  1. #1
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    PHP include external file!!

    Hello I am using php include on my site to save both time and work.

    My website:

    file1.php
    index.php
    file2.php
    /exempl/index.php
    /exempl/super.php
    /exempl/super2.php

    How can I use the php include to include file1.php and file2.php in /exempl/index.php???

    I have tried this:

    Code:
    <?php
    
    include 'http://www.example.com/file1.php';
    
    ?>
    BUT IT DO NOT WORK

    I DESPERATELY NEED HELP QUICK!!!

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: PHP include external file!!

    Including from a remote URL is not allowed, even if that URL is actually local. The simplest way is to use either relative or absolute paths to the files:
    Code:
    //This is relative, '../' means the parent directory
    include('../file1.php');
    Code:
    //This is absolute, '/' means the base directory
    include('/file1.php');
    //If the base is not setup to the document root, it may actually be:
    include('/home/YOURCPANELUSERNAME/public_html/file1.php');
    Both of these will work in the '/exempl/index.php' file.
    Last edited by lemon-tree; 09-13-2010 at 01:51 PM.

  3. #3
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: PHP include external file!!

    http://uk.php.net/manual/en/function.include.php
    http://uk.php.net/manual/en/function.require.php

    The only difference between require() and include() is that if an include fails, it errors. If a require fails, it freaks out and stops the page loading (in a good way). Therefore, you should use require() for includes files.

    You can also use require_once() and include_once(). These are pretty much the same, but it will only include or require the file once, even if you tell it to require it twice. I can't actually see the point in _once, but it's good to know about.

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  4. #4
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    Re: PHP include external file!!

    Thank you very very veyr much!!!!!!!!
    Last edited by gaptrast; 09-13-2010 at 02:12 PM.

  5. #5
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: PHP include external file!!

    Quote Originally Posted by Alex Mac View Post
    You can also use require_once() and include_once(). These are pretty much the same, but it will only include or require the file once, even if you tell it to require it twice. I can't actually see the point in _once, but it's good to know about.
    This can be useful when you have a complex system that is set to include or require a class's file each time it must be used. If you used the same class over and over, and it's a quite big file, it can slow down the script processing. However, if you called a _once method, the class will be loaded in the main script only once, saving some considerable overhead.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  6. #6
    AngusThermopyle is offline x10Hosting Member AngusThermopyle is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    84

    Re: PHP include external file!!

    Quote Originally Posted by Alex Mac View Post
    I can't actually see the point in _once, but it's good to know about.
    If you require or include a file, that file actually runs. ie, if you include foo.php and foo.php opens a log file, database connection, etc, then include-ing it twice will repeat the action, often not what you want.

    Also, if foo.php defines a function, you will get a nasty little warning message like:

    Fatal error: Cannot redeclare foobar() (previously declared in /home/zeke/public_html/foo.php:6) in /home/zeke/public_html/foo.php on line 7

  7. #7
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: PHP include external file!!

    It's better to not rely on _once - the code should be fixed so that it doesn't include the file more than once.

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  8. #8
    AngusThermopyle is offline x10Hosting Member AngusThermopyle is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    84

    Re: PHP include external file!!

    Quote Originally Posted by Alex Mac View Post
    It's better to not rely on _once - the code should be fixed so that it doesn't include the file more than once.

    ~Callum
    In some cases it is just not feasible. You include B.php and C.php which both include A.php.
    And if you have several conditional requires, using require_once is simpler and less error prone than manually keeping track of which files have already been called.

  9. #9
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: PHP include external file!!

    Fair enough, I guess.

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  10. #10
    sunnyshane9230 is offline x10Hosting Member sunnyshane9230 is an unknown quantity at this point
    Join Date
    Sep 2010
    Posts
    2

    Re: PHP include external file!!

    Code:
    <?php
    include('exempl/index.php');
    include('exempl/super.php');
    ?>
    I think you should code like that.Because some servers were configured to process your code like "../somefile.php" or "/somefile.php" to "root/somefile.php" but another think "/somefile.php" is the peering file

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. how to include a file
    By piorivas in forum Programming Help
    Replies: 2
    Last Post: 12-29-2009, 10:36 AM
  2. Include file
    By triviata in forum Free Hosting
    Replies: 9
    Last Post: 10-19-2009, 10:25 AM
  3. PHP Include, file help!
    By stillDOLL in forum Programming Help
    Replies: 8
    Last Post: 10-07-2008, 04:26 AM
  4. External Font File
    By kbjradmin in forum Programming Help
    Replies: 2
    Last Post: 07-11-2008, 01:31 AM
  5. include html from another file?
    By galaxyAbstractor in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 11-17-2007, 01:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers