include problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

lang79james
Forum Newbie
Posts: 13
Joined: Fri Oct 23, 2009 10:40 pm

Re: include problem

Post by lang79james »

thats would work but what is wrong with

Code: Select all

include '/var/www/footer.php';
yea it is a lot to type out but i does work like a champ

I would show you some of my code but I am in updating my server right now........
files are on a virtual machine and I save them right before I killed my main sever same hard drive i killed :P
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: include problem

Post by califdon »

Vegan wrote:So what can I do, grab the footer from the database?
I'm afraid we're not communicating very well. I don't see any need for a database in what we've been discussing. What is it you need to do? You want to have a standard footer for a bunch of web pages, right? That's a common requirement and is typically done using include(). What's the problem? Are these pages part of different web domains? Are these domains being served by the same web server? Perhaps in Virtual Server operation?

Here are the basics: for any Apache server, there is a Document Root that is specified in httpd.conf. The server and any PHP scripts it runs can include files that are within the file system anywhere in Document Root and its sudirectories. You can specify an includes_path or you can just refer to the appropriate path relative to Document Root each time you specify a file to be included. What include doesn't do (to the best of my knowledge) is connect with Internet domains, using http protocol. You could probably do that with cURL because that's what it's for. But I didn't get the impression that you were trying to include a file on some other server.

As I understand your requirement, it's just a standard include that you need. You just have to provide a valid file system path so it can find the file.
lang79james
Forum Newbie
Posts: 13
Joined: Fri Oct 23, 2009 10:40 pm

Re: include problem

Post by lang79james »

Hey Cal from what your saying is that the include function can use any file on the server. For example lets say I got a file on my desktop I want to use and as long as I use the path to that file I can use it there right?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: include problem

Post by califdon »

lang79james wrote:Hey Cal from what your saying is that the include function can use any file on the server. For example lets say I got a file on my desktop I want to use and as long as I use the path to that file I can use it there right?
Yes. Read how include() works: http://php.net/manual/en/function.include.php and read this security explanation: http://phpsec.org/projects/guide/3.html, especially the sentence that says,
Of course, one simple solution is to place all modules outside of document root, and this is a good practice. Both include and require can accept a filesystem path, so there's no need to make modules accessible via URL. It is an unnecessary risk.
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: include problem

Post by Vegan »

I noticed a fair chunk of PHP code dealing with the some document root construct.

Code: Select all

<?php
$documentRoot = null;

if (isset($_SERVER['DOCUMENT_ROOT'])) {
    $documentRoot = $_SERVER['DOCUMENT_ROOT'];
   
    if (strstr($documentRoot, '/') || strstr($documentRoot, '\\')) {
        if (strstr($documentRoot, '/')) {
            $documentRoot = str_replace('/', DIRECTORY_SEPARATOR, $documentRoot);
        }
        elseif (strstr($documentRoot, '\\')) {
            $documentRoot = str_replace('\\', DIRECTORY_SEPARATOR, $documentRoot);
        }
    }
   
    if (preg_match('/[^\\/]{1}\\[^\\/]{1}/', $documentRoot)) {
        $documentRoot = preg_replace('/([^\\/]{1})\\([^\\/]{1})/', '\\1DIR_SEP\\2', $documentRoot);
        $documentRoot = str_replace('DIR_SEP', '\\\\', $documentRoot);
    }
}
else {
    /**
     * I usually store this file in the Includes folder at the root of my
     * virtual host. This can be changed to wherever you store this file.
     *
     * Example:
     * If you store this file in the Application/Settings/DocRoot folder at the
     * base of your site, you would change this array to include each of those
     * folders.
     *
     * <code>
     * $directories = array(
     *     'Application',
     *     'Settings',
     *     'DocRoot'
     * );
     * </code>
     */
    $directories = array(
        'Includes'
    );
   
    if (defined('__DIR__')) {
        $currentDirectory = __DIR__;
    }
    else {
        $currentDirectory = dirname(__FILE__);
    }
   
    $currentDirectory = rtrim($currentDirectory, DIRECTORY_SEPARATOR);
    $currentDirectory = $currentDirectory . DIRECTORY_SEPARATOR;
   
    foreach ($directories as $directory) {
        $currentDirectory = str_replace(
            DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR,
            DIRECTORY_SEPARATOR,
            $currentDirectory
        );
    }
   
    $currentDirectory = rtrim($currentDirectory, DIRECTORY_SEPARATOR);
}

define('SERVER_DOC_ROOT', $documentRoot);
?>
Seems like overkill for a seemingly simple include!

My sites all are folders under /www which is an unorthodox approach but I have used it as its easy to share with SAMBA and backup with gzip etc.
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
lang79james
Forum Newbie
Posts: 13
Joined: Fri Oct 23, 2009 10:40 pm

Re: include problem

Post by lang79james »

ok dude I got my site kinda back up

I use a header and a footer for all my pages. My header for all my pages is this :

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Draco Chapter I</title>
  <? include "data/head.php"; ?>

<body>
<center>
<table width="700" border=".01" cellspacing="0" cellpadding="0">
<tr>
my footer:

Code: Select all

  <?php include "/var/www/data/footer.php";?>
This will achieve what you want.....
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: include problem

Post by Vegan »

so to be clear, I need to not look at the web site document root but rather the file system folder.

is there an easier way to identify the web site's document root? This way I can use the same code for many sites that are in various folders.
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: include problem

Post by califdon »

Vegan wrote:so to be clear, I need to not look at the web site document root but rather the file system folder.

is there an easier way to identify the web site's document root? This way I can use the same code for many sites that are in various folders.
It's always in the Apache configuration file, httpd.conf. Just search for the line that says "Document Root".
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: include problem

Post by Vegan »

All of those paths are in the httpd.conf so I guess I need to parse that file then.

I wonder why an environment variable is not set to allow a program to see what is where.
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
griffinmt
Forum Commoner
Posts: 35
Joined: Sun Jul 16, 2006 8:37 pm
Location: Michigan

Re: include problem

Post by griffinmt »

Use include_dir in the php.ini to reference a specific location on the server, and don't put a path in the include statement.
This location can be outside the scope of the actual website files for security.

If the file that you want to include is needed on several servers in a protected internal network, pick one to do your maintenance on, then sync the path contents across the servers on a regular basis.
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: include problem

Post by Vegan »

The purpose of the include is to eventually compose a document from standard header and footer. This would simplify site development considerably.
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: include problem

Post by califdon »

Vegan wrote:The purpose of the include is to eventually compose a document from standard header and footer. This would simplify site development considerably.
Yes, that's the common use for include. What is your question?
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: include problem

Post by Vegan »

I am simply trying to find a generic way to use an include that is relative to the web site folder.
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: include problem

Post by John Cartwright »

Vegan wrote:I am simply trying to find a generic way to use an include that is relative to the web site folder.
Absolute paths?
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: include problem

Post by Vegan »

Well how is it that phpBB can cope with relative paths OK?
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
Post Reply