First off, I have apache server installed on my computer, as well as the newest releases of php and mysql. Ideally, i like to do all site updates on my computer first, and then upload after they are finished.

I'm trying to use php coding instead of traditional html, and maybe I am overdoing things. Perhaps probably.

When doing typical html links, as a habit, I never link directly by <a href="http://www.mydomain.com/blah/blah.html">. Rather, I always link as follows: <a href="../blah/blah.html">, obviously depending on where the file that I am currently on resides within the site.

Anyway, that system works fine, but now that i'm learning php, i thought I'd do things in a fancy smancy type of way, and tried to link to the root of my homepage as follows:
<a href="<?php echo $_SERVER['DOCUMENT_ROOT'] . '/index.php'; ?">Home</a>

Although this may work on the actual live website, as i imagine that $SERVER['DOCUMENT_ROOT'] will link to the root of the domain, on my computer, $SERVER['DOCUMENT_ROOT'] is NOT linking to the root of the "server", that is, it is not linking to http: //localhost/. Rather, it is linking ot the exact path on my hard drive, such as c:\documents and setting\blah\blah\blah\website\index.php. And as such, this is not working correctly.

Is there a better php way of doing this? Or again, maybe on just trying to do too much. Perhaps it is best to just stick with the simple <a href="../blah/blah.html">.

Please advise.

Thanks.

    I have a file called xroot-offset.php in every directory that just defines ROOT_OFFSET, for the root directory it's ./ then one level deeper it's ../ etc. It lets me use menus as includes from deeper directories and I refer to all files using that like

    require_once('xroot-offset.php');
    require_once(ROOT_OFFSET.'lib/parser.class.php');
    require_once(ROOT_OFFSET.'../outofroot/db-connection.php');

    I discussed it with some guy a while ago http://phpbuilder.com/board/showthread.php?t=10315945

    I also use it in links

    echo '<img src="'.ROOT_OFFSET.'images/some-stuff.jpg">';

      wouldn't your xroot-offset.php file have to define ROOT_OFFSET as a variable and thus it would have a $ in front of it?

      So then your echo code would look as such
      echo '<img src="'.$ROOT_OFFSET.'images/some-stuff.jpg">';

      This may just be my like of php knowledge.

      Thanks.

        And as a side note, I guess you are saying that it did make sense that the command $_SERVER['DOCUMENT_ROOT'], when used on my computer, did point the exact path of my directoy (c:\blah\blah\blah\domain) as opposed to linking to the ROOT directory of the SERVER, http://localhost/ ?

          He is using ROOT_OFFSET as a constant, or a global variable whose value does not change. THerefore there is no dollar sign in front of it. See here for more info: define()

          Drakla, I just had a thought:

          Rather than including a file into every file with the directory level defined, why not do the following:

          1) Create an array of your directories and save it in your config file
          2) for each dirctory list the link level back to root
          3) then, in each file, all you have to do is use the value for the indexed array item

          Chances are you will probably need to include your config or functions file into every file anyway, so it would reduce the number of includes in a page, and keep all site settings in one main file...

          Just a thought...

            Rodney, can you do some code, or pseudocode as I'm not really getting you. THe good thing about what I'm currently doing is that the only thing in the file is the definition of root offset and it's absolutely simple as there's nothing to think about. All I have to do is drop a copy into each directory and they're only about 40 bytes and I just include them once. It's let me turn loads of ugly sites with hundreds of files in one directory into a nicely structured system with separate directories for similar tasks. If there's and easier method out there I'll love it.

            EDIT: just in case people don't get the picture, even if the site has 30 directories only one xroot-offset.php gets included / requred, the one in the directory of the currently running script. This is true even if files are included from loads of the other directories. And links that use ROOT_OFFSET must be made as if they're relative to the root, not the current directory.

              Try this;

              take "/" for the root path in your links.
              Ex.
              <a href="/blah/blah.html">Blah</a>

              It's mean that you link to "blah.html" in folder "blah" at your site root ([url]http://localhost)[/url]. It's work for your real site too.

                John B. wrote:

                Try this;

                take "/" for the root path in your links.
                Ex.
                <a href="/blah/blah.html">Blah</a>

                It's mean that you link to "blah.html" in folder "blah" at your site root ([url]http://localhost)[/url]. It's work for your real site too.

                Nope - on my dev server a site might be
                http://localhost/sites/google/public_html/index.php

                I can't just refer to that as /index.php

                  Drakla wrote:

                  Rodney, can you do some code, or pseudocode...

                  Well, I am not sure if you will like what I was thinking, but what if you do something like this:

                  <?php
                  /*
                   * Config. file
                  */
                  
                  define('ENVIRONMENT', 'testing'); // "testing" OR "production"
                  
                  
                  if(ENVIRONMENT == 'testing')
                  {
                  	define('URL', 'http://localhost/sites/google/public_html');
                  	define('ADMIN', 'you@email.com');
                  }
                  
                  else
                  {
                  	define('URL', 'http://www.domain.com');
                  	define('ADMIN', 'admin@domain.com');
                  }
                  
                  ?>
                  

                  Then, on my page, I will do this:

                  <?php
                  include_once('../includes/config.php');
                  
                  if(!isset($_GET['some_needed_value']))
                  {
                  	header('Location: ' . URL . '/contact-us');
                  	exit;
                  }
                  
                  else 
                  {
                  	$var = (int)$_GET['some_needed_value'];
                  
                  echo '<a href="'. URL . '/view.php?var='.$var.'">view</a>';
                  echo '<a href="'. URL . '/edit.php?var='.$var.'">edit</a>';
                  echo '<a href="'. URL . '/delete.php?var='.$var.'">delete</a>';
                  
                  }
                  ?>
                  

                    ahh, I'm with you. Thing is, with my current system if I move a file to a different directory then nothing has to change as long as that directory has a root offset file, all links remain valid. With your suggestion I'd have to change the link to the config file. I do use a similar thing to your suggestion for debugging and data sourcing so I don't (again!) end up supplying all my details (again!) to a site which I really don't want to have it.

                    if (isset($_SERVER['HTTP_HOST']) && ($_SERVER['HTTP_HOST'] == 'localhost' || $_SERVER['HTTP_HOST'] == '127.0.0.1'))
                    {
                    	define('MASTER_ADDRESS', 'http://localhost/home/coregdat/public_html/'); // DEBUG - DON'T send submit data to the real site
                    }
                    else
                    {
                    	define('MASTER_ADDRESS', 'http://realsite.com/');
                    }

                      Drakla: I see what you do. I am lucky, in the sense that if I move a file out of the current directory up one or into a sub-dir, the software/editor will automatically update the links to my included and required files. So, I don't have to worry about that. And, I test a lot locally before moving files to production....

                      But, cool. May the PHP force be with you...

                        Rodney H. wrote:

                        But, cool. May the PHP force be with you...

                        That has so freaked me out. I'm playing Knights of the Old Republic and have been doing all afternoon (Just got last Star Map - Rah!).

                          That is funny, I was purely going on your signature/location...

                            Does anyone wanna know how I do it? :p

                            Okay, I have several sites on my "development server" which is actually just this PC that I use 24/7. How do I manage them with all their links and stuff? Well, read on.

                            At first I actually did it the way of defining a root folder, and then working from there. But that got kinda annoying when bouncing files back and forth between the real thing and the development stage.

                            So, I now use Apache virtual hosts, and some edited DNS stuff.

                            In httpd.conf I have:

                            NameVirtualHost *:80
                            <VirtualHost *:80>
                                ServerName localhost
                                DocumentRoot C:/WAMP/www/
                            </VirtualHost>
                            <VirtualHost *:80>
                                ServerName neon
                                DocumentRoot C:/WAMP/www/neon/
                            </VirtualHost>
                            <VirtualHost *:80>
                                ServerName madf
                                DocumentRoot C:/WAMP/www/madwormer2.free.fr/
                            </VirtualHost>
                            

                            And in my DNS records (in the HOSTS file):

                            127.0.0.1 localhost
                            127.0.0.1 neon
                            127.0.0.1 madf
                            # Some load of ad-blocking IP addresses here.
                            

                            Now when I go to my browser and type in http://madf/ I basically get the local version of http://madwormer2.free.fr/ with all the links acting as they should, when using "/file.htm" in the href part of a link, it works on BOTH servers the same, because if you saw the directory structure from the browser side of things, they are exactly the same, despite the completely different paths in the config, and the subdirectory of the local version.

                              Rodney H. wrote:

                              Drakla: I see what you do. I am lucky, in the sense that if I move a file out of the current directory up one or into a sub-dir, the software/editor will automatically update the links to my included and required files. So, I don't have to worry about that. And, I test a lot locally before moving files to production....

                              But, cool. May the PHP force be with you...

                              See Rodney, my software/editor does the same thing, but I'm trying to stop using it, which means i can get rid of the stupid things it does...but it also means i lose some of the nice features.

                              As metioned on a previous post, I actually do my coding in frontpage. Why? just cause i started using it years ago at a job (when i already coded in notepad), and it has just stuck with me.

                              The frontpage includes work nicely, and as you said, whenever i want to move a file, it always asks "do you want to update links, blah, blah". And this is helpful.

                              But on the same note, i'm trying to stop relying on frontpage for certain things including includes and other such things. This is what brought up this problem. And again, not a problem really, as I can simply reference the link by "thinking" about where the page resides on the "domain" and then linking accordingly as such: "../../catalog/blah.php".

                              When doing this, no matter how you link, you will have to inform the link that blah.php is located within the catalog directory which itself is in the root directory. So what this boils down to, is whether or not we want to put the "extra" effort into making sure we "go back" the appropriate number of directories in order to make it to the root, before we then call a folder. That is where the $SERVER['DOCUMENT_ROOT'] idea came in. I was hoping to use it as such:
                              <a href="<?php echo $
                              SERVER['DOCUMENT_ROOT'] . '/index.php'; ?">Home</a>

                              But again, on my local machine, that doesn't work, as $_SERVER['DOCUMENT_ROOT'] apparently does NOT link to http://localhost/ , the ACTUAL root of the server. Rather, it link to the specific c:\path on my hard drive. And this causes problems.

                              So i was mainly curious if i was doing something wrong with the $_SERVER['DOCUMENT_ROOT'] command and its usage, or if that was just how it worked.

                              If it is too much trouble, then really, as mentioned, this all boils down to the fact that we were trying to avoid "remembering" how many directories deep we were into the site. This is obviously not that big of a deal. But it woudl make life easier nonetheless.

                              Jonathan

                                Just use root-relative paths like /blah/blah.html

                                If your dev server does't have them, because your project root is in a subdirectory, then create it its own virtual host instead. Job done.

                                Mark

                                  Write a Reply...