SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member RogueSkolar's Avatar
    Join Date
    Aug 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pulling My Hair Out Trying To Get PHP includes to Work!

    Firstly just want to say thanks in advance.

    I've just started converting a more or less static site into a php driven site. I haven't previously
    used php to any great length and I'm seriously having problems setting up includes. I'm working
    on localhost at the moment.

    My directory/project structure is as the following:

    Code:
    mysite.com/index.php
    
    mysite.com/includes/
    
    mysite.com/category-1/demo.php
    
    mysite.com/category-2/
    
    mysite.com/category-3/
    
    ...etc
    If at all possible, what I'm trying to do is set the path to the includes via htaccess and then be
    able to use that path on any page, in any directory.

    I've currently got this in my htaccess file in the root directory:

    Code:
    php_value include_path "php"
    The implementation below then works a charm when called from files within the root directory.

    Code:
    <?php require_once("some-include.php"); ?>
    or

    Code:
    <?php include_once("some-include.php"); ?>
    It fails however when includes called in the same manner with files in subdirectories. In order to
    get it to work I've had to create a separate .htaccess file for each directory and place the following:

    Code:
    php_value include_path "../php"
    How can I set the path globally in my main htaccess or php.ini and be able to just implement the
    includes without having to use relative paths?

    Any suggestions are greatly appreciated at this point.

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,368
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    In my case I have full access to the server and I created a folder in the php directory; the path to this folder is in my php.ini file.
    I can include any file I want from any page on the server with a simple include e.g. include ('layout.css'); and not need to worry about any paths.

    Can you do it this way or do you not have enough access?

    I belive php_value include_path "php" is the path to the php folder that is above the server root and I would think you need to define the path to you includes folder in greater detail.

  3. #3
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    It is because you are using relative paths. Use absolute.
    php_value include_path "/start/from/the/root/use/the/full/path/to/php"
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Member RogueSkolar's Avatar
    Join Date
    Aug 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome, thanks for your suggestions @logic and @Rubble.
    I'll implement them and and see how it goes, my brain's fried
    right now.

    Much appreciated.

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,807
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    @RogueSkolar

    Try this before setting your links:

    PHP Code:

    echo getcwd(); die;

    # now set the relative path wherever _file_001.php is stored
     #include_path "_file-001.php"             // this directory
     #include_path "../_file-001.php"          // up one  directory
     #include_path "../../_file-001.php"       // up two  directory
     #include_path "../../../_file-001.php"   // up to wherever directory 

  6. #6
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,117
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    Hi RogueSkolar. Welcome to the forums.

    Quote Originally Posted by RogueSkolar View Post
    I'm working on localhost at the moment.
    Does that mean you have the site running locally, on your computer? Normally, the paths will be different on your local computer from the paths on the remote server, which can be a pain. So you can set up your hosts file to treat your site like it's online, meaning that you don't have to change any code when you put your site on the www. If you are doing this locally, what system are you on? (Mac, PC?)
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Good point @ralph.m ;, it made me read the OP again.

    Going back to the OP, you initially said this was your setup.

    PHP Code:
    mysite.com/index.php

    mysite
    .com/includes/

    mysite.com/category-1/demo.php

    mysite
    .com/category-2/

    mysite.com/category-3/

    ...
    etc 
    There is something I need to warn you about -- that is not how you would normally set things up, an includes directory inside your public website directory structure.

    Include folders normally contain code which you do not want made public so you would normally have REAL (HDD) paths like this:
    Code:
    /var/www/mysite.com/index.php
    
    /var/www/mysite.com/category-1/demo.php
    
    /var/www/mysite.com/category-2/
    
    /var/www/mysite.com/category-3/
    (which of course appear as mysite.com/index.php etc once your server is configured correctly, much as @ralph.m ; is suggesting)

    Then here is the shared place you would keep all of your include files:
    Code:
    /var/www/includes
    Which would then be available to all of your websites.

    (I am using /var/www/ as the default web folder, yours may look more like /xamp/public/http-docs/ or something ... it matters not)

    Also, there is nothing stopping you have a folder WITHIN that /includes folder for just mysite.com

    Then you can set the ini directive to tell PHP that all include files live at
    Code:
    php_value include_path "/var/www/includes"
    This makes life really simple, all include files will initially be searched for in this directory.

    Tradition would then have it that you would have say a file with all of your database connection credentials in a file for this project and name it:
    Code:
    mysite-config.php
    and save it as:

    /var/www/includes/mysite-config.php

    You then include that file FROM ANYWHERE in your website PHP files with the simple instruction:
    PHP Code:
    include 'mysite-config.php'
    Another example might be a really useful piece of PHP code which outputs variables for a date picker. You might use that in more than one project so you'd save it as /var/www/includes/date-picker.php and then from all your projects on different websites on this server you'd simple do:
    PHP Code:
    include 'date-picker.php'
    You want to totally avoid mysite-config.php being accessible from your website, by someone stumbling across it in a publicly accessible folder such as mysite.com/includes/mysite-config.com

    Personally, I prefer to set the include_path directive directly in the php.ini file, you will be able to do the same on your localhost server, but you'd need to check with your hoster if you get to have your own ini file, or if they are willing to set this variable up in your apache conf file for you.

  8. #8
    SitePoint Member RogueSkolar's Avatar
    Join Date
    Aug 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW thanks a bunch guys, I'm much obliged.

    @John_Betong

    Thanks for your solution. I'm sure to play around with this as I drill down on the perfect or at least,
    least cumbersome implementation. Have you got a link where I can find out what these statements
    are doing in particular echo getcwd(); die;. Thanks.

    @ralph.m Thanks for the welcome

    Yes these are all concerns that have not escaped me. It's one of the first things that came into consideration,
    well actually when I started running into problems!

    I'm running Ubuntu. I've got Apache, MySQL and PHP 5 set up on my local (development) environment.

    The root/path for localhost on my machine is: /var/www/

    The path to my web project on localhost is: /var/www/mysite.com/

    This is a screen grab of the directory structure on my webhost a la' the usual cPanel:





    My thinking is that:

    /home/username/ on webhost = /var/www/ on localhost.

    and

    /home/username/public_html/ on webhost = /var/www/mysite.com/ on localhost.

    So now my problem lies in properly defining a directory and path for includes on localhost that will match
    that on my webhost's server which brings me to the points @Cups has raised.

    @Cups, thanks for your insights.

    The php.ini file in public_html/ has the following path set for includes.

    ; UNIX: "/path1:/path2"
    include_path = ".:/usr/lib/php:/usr/local/lib/php"


    I presume this structure is what you are referring to -- that is to not place includes in public_html/
    where it is publicly accessible. I've created a new directory for includes and placed it at the same level
    with this php directory:



    If I've understood this correctly, the absolute path for this will be:

    /home/username/includes/ which can be mapped to /var/www/includes/ on localhost.

    Then what I'd need to do is just change the include path in php.ini to point to this new directory and possibly
    copy over some configuration files in the original php directory (if needed) and all would be ready to go and I
    could simply sync the include directories when I'm ready to deploy?

    I'll play around with this and hopefully get it to work, but I've got a lot of self education ahead I think...

    Big ups to all.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Hey man, big props for taking the time to create such a long reply -- and to so many of us. with screen shots and all!

    I will let you play with your current config and lets hope you get yourself sorted.

  10. #10
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,807
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    @RogueSkolar


    @John_Betong

    Thanks for your solution. I'm sure to play around with this as I drill down on the perfect or at least,
    least cumbersome implementation. Have you got a link where I can find out what these statements
    are doing in particular echo getcwd(); die;. Thanks.

    Details of echo, getcwd() and die() functions, etc can be found by searching:

    http://php.net/

    I find that the site is an excellent resource when developing and well worth the effort of becoming familiar with the layout.

    As somebody previously mentioned "links are a pain". The getcwd() function returns the path of the file the browser requested. The include function can be either an absolute or relative path. Using the script displays the path then halts execution. Once you know where you are then it us up to you to choose the path of the proposed included file and/or image.
    Last edited by John_Betong; Aug 16, 2012 at 21:50. Reason: spelling: not my fortay


Tags for this Thread

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
  •