|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 ================== |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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/ |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
..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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
..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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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> |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
..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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 ================== |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
..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 |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|