PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.www.webmaster > $_SERVER[DOCUMENT_ROOT] on home machine
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
$_SERVER[DOCUMENT_ROOT] on home machine

Réponse
 
LinkBack Outils de la discussion
Vieux 28/12/2007, 03h35   #1
sebzzz@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut $_SERVER[DOCUMENT_ROOT] on home machine

Hi,

I'm currently working on a site locally on my machine and I have
includes like this:

include($_SERVER[DOCUMENT_ROOT] . "/includes/header.php");

But it yields an error because $_SERVER[DOCUMENT_ROOT] is /opt/lampp/
htdocs but this folder contains a couple of my sites each in a
separate folder.

To make my site work locally, I have to change the include to
something like this:

include($_SERVER[DOCUMENT_ROOT] . "/mysite/" . "/includes/
header.php");

The only problem with this way of working is that when I'll upload my
site on a server, I'll have to remove all references to /mysite, but
there should be plenty of them so I wonder if there is a way to tell
php when I'm working on that site that $_SERVER[DOCUMENT_ROOT] = "/opt/
lampp/htdocs/mysite"

Thanks in advance
  Réponse avec citation
Vieux 28/12/2007, 03h50   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: $_SERVER[DOCUMENT_ROOT] on home machine

sebzzz@gmail.com wrote:
> Hi,
>
> I'm currently working on a site locally on my machine and I have
> includes like this:
>
> include($_SERVER[DOCUMENT_ROOT] . "/includes/header.php");
>
> But it yields an error because $_SERVER[DOCUMENT_ROOT] is /opt/lampp/
> htdocs but this folder contains a couple of my sites each in a
> separate folder.
>
> To make my site work locally, I have to change the include to
> something like this:
>
> include($_SERVER[DOCUMENT_ROOT] . "/mysite/" . "/includes/
> header.php");
>
> The only problem with this way of working is that when I'll upload my
> site on a server, I'll have to remove all references to /mysite, but
> there should be plenty of them so I wonder if there is a way to tell
> php when I'm working on that site that $_SERVER[DOCUMENT_ROOT] = "/opt/
> lampp/htdocs/mysite"
>
> Thanks in advance
>


You're going about it the wrong way. Each site should have it's own
DOCUMENT_ROOT, and you won't have that problem. All it takes is some
work on the Apache configuration file (httpd.conf).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 28/12/2007, 13h11   #3
John Bokma
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: $_SERVER[DOCUMENT_ROOT] on home machine

sebzzz@gmail.com wrote:

> Hi,
>
> I'm currently working on a site locally on my machine and I have
> includes like this:
>
> include($_SERVER[DOCUMENT_ROOT] . "/includes/header.php");
>
> But it yields an error because $_SERVER[DOCUMENT_ROOT] is /opt/lampp/
> htdocs but this folder contains a couple of my sites each in a
> separate folder.
>
> To make my site work locally, I have to change the include to
> something like this:
>
> include($_SERVER[DOCUMENT_ROOT] . "/mysite/" . "/includes/
> header.php");
>
> The only problem with this way of working is that when I'll upload my
> site on a server, I'll have to remove all references to /mysite, but
> there should be plenty of them so I wonder if there is a way to tell
> php when I'm working on that site that $_SERVER[DOCUMENT_ROOT] = "/opt/
> lampp/htdocs/mysite"


Solution one (recommended if possible):
http://johnbokma.com/windows/apache-...-hosts-xp.html

Solution two:
use Ant ( http://ant.apache.org/ ). I use the latter to fix things, using
the replace task, that I can't solve with virtual hosting for one reason
or another.


--
John Bokma http://johnbokma.com/
  Réponse avec citation
Vieux 28/12/2007, 18h31   #4
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: $_SERVER[DOCUMENT_ROOT] on home machine

..oO(sebzzz@gmail.com)

>I'm currently working on a site locally on my machine and I have
>includes like this:
>
>include($_SERVER[DOCUMENT_ROOT] . "/includes/header.php");


In addition to the other replys:

Make sure your error_reporting is set correctly to E_ALL|E_STRICT in
your php.ini. The line above should throw a notice because of the
undefined constant DOCUMENT_ROOT.

Micha
  Réponse avec citation
Vieux 28/12/2007, 20h50   #5
sebzzz@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: $_SERVER[DOCUMENT_ROOT] on home machine

I created a virtual host in the Apache httpd.conf so when I type
mysite.localhost in the browser it points out to my site.

I encontered a problem however, with something like this:

<a href="<?php print $_SERVER[DOCUMENT_ROOT] . '/contact.php'; ?
>">Click me</a>


With the virtual host, this link points to:

mysite.localhost/opt/lampp/htdocs/mysite

so I defined a variable named site_location in the header include file
whose value is mysite.localhost and changed references to something
like this:

<a href="<?php print $site_location . '/contact.php'; ?>">Click me</a>

And now everything works fine. I'll just have to change the value of
this variable when I'll put my site online. However, I wonder if this
is an elegant solution to the problem, I rarely see absolute URLs when
I look up some site's code. Am I doing something wrong?

Thanks
  Réponse avec citation
Vieux 29/12/2007, 00h44   #6
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: $_SERVER[DOCUMENT_ROOT] on home machine

..oO(sebzzz@gmail.com)

>I created a virtual host in the Apache httpd.conf so when I type
>mysite.localhost in the browser it points out to my site.
>
>I encontered a problem however, with something like this:
>
><a href="<?php print $_SERVER[DOCUMENT_ROOT] . '/contact.php'; ?
>>">Click me</a>


This can't work.

>With the virtual host, this link points to:
>
>mysite.localhost/opt/lampp/htdocs/mysite


Correct. The document root is the physical location of the website's
root directory on the server's disk. This has nothing to do with URLs!

>so I defined a variable named site_location in the header include file
>whose value is mysite.localhost and changed references to something
>like this:
>
><a href="<?php print $site_location . '/contact.php'; ?>">Click me</a>


What about a simple

<a href="/contact.php">Contact</a>

?

Micha
  Réponse avec citation
Vieux 29/12/2007, 00h51   #7
sebzzz@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: $_SERVER[DOCUMENT_ROOT] on home machine

On Dec 28, 7:44pm, Michael Fesser <neti...@gmx.de> wrote:
> .oO(seb...@gmail.com)
>
> >I created a virtual host in the Apache httpd.conf so when I type
> >mysite.localhost in the browser it points out to my site.

>
> >I encontered a problem however, with something like this:

>
> ><a href="<?php print $_SERVER[DOCUMENT_ROOT] . '/contact.php'; ?
> >>">Click me</a>

>
> This can't work.
>
> >With the virtual host, this link points to:

>
> >mysite.localhost/opt/lampp/htdocs/mysite

>
> Correct. The document root is the physical location of the website's
> root directory on the server's disk. This has nothing to do with URLs!
>
> >so I defined a variable named site_location in the header include file
> >whose value is mysite.localhost and changed references to something
> >like this:

>
> ><a href="<?php print $site_location . '/contact.php'; ?>">Click me</a>

>
> What about a simple
>
> <a href="/contact.php">Contact</a>
>
> ?
>
> Micha


The problem is that there are multiple web pages on this site with
some that are nested in sub folders. I want to be able to include the
header and footer on every page, no mater how nested they are.

With this method, if a page2.php is in directory MyFolder and includes
a file with a link to contact.php but it doesn't exits in MyFolder, it
exists in the parent folder.
  Réponse avec citation
Vieux 29/12/2007, 00h55   #8
Scott Bryce
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: $_SERVER[DOCUMENT_ROOT] on home machine

sebzzz@gmail.com wrote:
> With this method, if a page2.php is in directory MyFolder and includes
> a file with a link to contact.php but it doesn't exits in MyFolder, it
> exists in the parent folder.


<a href="../contact.php">Contact</a>
  Réponse avec citation
Vieux 29/12/2007, 01h45   #9
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: $_SERVER[DOCUMENT_ROOT] on home machine

..oO(sebzzz@gmail.com)

>The problem is that there are multiple web pages on this site with
>some that are nested in sub folders.


That's the case on nearly all websites.

>I want to be able to include the
>header and footer on every page, no mater how nested they are.


No problem with that. For includes you can use a path based on
$_SERVER['DOCUMENT_ROOT']. It will work always, regardless of the
nesting level.

>With this method, if a page2.php is in directory MyFolder and includes
>a file with a link to contact.php but it doesn't exits in MyFolder, it
>exists in the parent folder.


Links and includes are entirely different things. Links work with URLs,
while includes work on the physical file system. Just write your link as

<a href="/contact.php">Contact</a>

or soemthing like that (assuming that the contact page resides in the
website's root directory) and include the file with this link from
wherever you like with the method described before.

Micha
  Réponse avec citation
Vieux 29/12/2007, 02h56   #10
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: $_SERVER[DOCUMENT_ROOT] on home machine

sebzzz@gmail.com wrote:
> On Dec 28, 7:44 pm, Michael Fesser <neti...@gmx.de> wrote:
>> .oO(seb...@gmail.com)
>>
>>> I created a virtual host in the Apache httpd.conf so when I type
>>> mysite.localhost in the browser it points out to my site.
>>> I encontered a problem however, with something like this:
>>> <a href="<?php print $_SERVER[DOCUMENT_ROOT] . '/contact.php'; ?
>>>> ">Click me</a>

>> This can't work.
>>
>>> With the virtual host, this link points to:
>>> mysite.localhost/opt/lampp/htdocs/mysite

>> Correct. The document root is the physical location of the website's
>> root directory on the server's disk. This has nothing to do with URLs!
>>
>>> so I defined a variable named site_location in the header include file
>>> whose value is mysite.localhost and changed references to something
>>> like this:
>>> <a href="<?php print $site_location . '/contact.php'; ?>">Click me</a>

>> What about a simple
>>
>> <a href="/contact.php">Contact</a>
>>
>> ?
>>
>> Micha

>
> The problem is that there are multiple web pages on this site with
> some that are nested in sub folders. I want to be able to include the
> header and footer on every page, no mater how nested they are.
>
> With this method, if a page2.php is in directory MyFolder and includes
> a file with a link to contact.php but it doesn't exits in MyFolder, it
> exists in the parent folder.
>


Save yourself a lot of headaches. Configure Apache properly and each
site on your system will have it's own DOCUMENT_ROOT. Don't try to
gimmick up unnecessary work-arounds - they will just complicate matters
more.

Right now I've got seven sites defined on my local development system
(it varies from week to week). And all of them work exactly like the
production systems.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 29/12/2007, 05h58   #11
sebzzz@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: $_SERVER[DOCUMENT_ROOT] on home machine

On Dec 28, 8:45pm, Michael Fesser <neti...@gmx.de> wrote:
> .oO(seb...@gmail.com)
>
> >The problem is that there are multiple web pages on this site with
> >some that are nested in sub folders.

>
> That's the case on nearly all websites.
>
> >I want to be able to include the
> >header and footer on every page, no mater how nested they are.

>
> No problem with that. For includes you can use a path based on
> $_SERVER['DOCUMENT_ROOT']. It will work always, regardless of the
> nesting level.
>
> >With this method, if a page2.php is in directory MyFolder and includes
> >a file with a link to contact.php but it doesn't exits in MyFolder, it
> >exists in the parent folder.

>
> Links and includes are entirely different things. Links work with URLs,
> while includes work on the physical file system. Just write your link as
>
> <a href="/contact.php">Contact</a>
>
> or soemthing like that (assuming that the contact page resides in the
> website's root directory) and include the file with this link from
> wherever you like with the method described before.
>
> Micha


Maybe I do something wrong with Apache or I don't know, but when I do
something like <a href="/contact.php">Contact</a> it tries to find
contact.php at the root of my computer's file system, not the root of
the website. However, my virtual host is configured correctly so when
I type mysite.localhost it runs.

Any idea why?
  Réponse avec citation
Vieux 29/12/2007, 23h37   #12
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: $_SERVER[DOCUMENT_ROOT] on home machine

..oO(sebzzz@gmail.com)

>On Dec 28, 8:45pm, Michael Fesser <neti...@gmx.de> wrote:
>>
>> Links and includes are entirely different things. Links work with URLs,
>> while includes work on the physical file system. Just write your link as
>>
>> <a href="/contact.php">Contact</a>
>>
>> or soemthing like that (assuming that the contact page resides in the
>> website's root directory) and include the file with this link from
>> wherever you like with the method described before.

>
>Maybe I do something wrong with Apache or I don't know, but when I do
>something like <a href="/contact.php">Contact</a> it tries to find
>contact.php at the root of my computer's file system, not the root of
>the website. However, my virtual host is configured correctly so when
>I type mysite.localhost it runs.
>
>Any idea why?


This sounds very strange. It will happen if you open your pages directly
from the disk without the web server (with a file:// URL). But when the
request is sent to a web server, '/' always refers to the website root,
not the file system root.

What's the result of these two URLs on your local machine:

http://mysite.localhost
http://mysite.localhost/contact.php

Do they work as expected?

Micha
  Réponse avec citation
Vieux 04/01/2008, 00h34   #13
sebzzz@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: $_SERVER[DOCUMENT_ROOT] on home machine

On Dec 29 2007, 6:37pm, Michael Fesser <neti...@gmx.de> wrote:
> .oO(seb...@gmail.com)
>
>
>
> >On Dec 28, 8:45pm, Michael Fesser <neti...@gmx.de> wrote:

>
> >> Links and includes are entirely different things. Links work with URLs,
> >> while includes work on the physical file system. Just write your link as

>
> >> <a href="/contact.php">Contact</a>

>
> >> or soemthing like that (assuming that the contact page resides in the
> >> website's root directory) and include the file with this link from
> >> wherever you like with the method described before.

>
> >Maybe I do something wrong with Apache or I don't know, but when I do
> >something like <a href="/contact.php">Contact</a> it tries to find
> >contact.php at the root of my computer's file system, not the root of
> >the website. However, my virtual host is configured correctly so when
> >I type mysite.localhost it runs.

>
> >Any idea why?

>
> This sounds very strange. It will happen if you open your pages directly
> from the disk without the web server (with a file:// URL). But when the
> request is sent to a web server, '/' always refers to the website root,
> not the file system root.
>
> What's the result of these two URLs on your local machine:
>
> http://mysite.localhosthttp://mysite...st/contact.php
>
> Do they work as expected?
>
> Micha


Yes, those work fine.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h00.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 1,77993 seconds with 21 queries