Tuesday 15 May 2012

php - how to deal with changing DOCUMENT_ROOT in an html website -


I am a newbie for web development and I am trying to learn the best practice while walking together. I run my website locally by using Dreamweaver on the local code; Then I use it remotely and I test it there. I use for my website (HTML, PHP and some JS)

Since I started it, I had to face a problem and I did not know any good method to solve it. > DOCUMENT_ROOT is a PHP variable that changes; Locally, this indicates xampp / htdocs (and not my actual website root xampp / htdocs / myWebsite) and remotely it indicates my actual document root.

That's why I left it now because of this I stopped searching the files using the full path on the remote server.

Is there any good practice to avoid this?

  • I know that I can change the php.ini config file, where DOCUMENT_ROOT indicates, but I do not want .. < Besides, I can not code strict relative paths (such as commentators have suggested) because I have a script running from different directories ... so I Hard code can not be relative path

    Saturate Create a configuration file to determine if the site is running on the local site or live site, then add the configuration file on each page. You can also set error reporting and database connections for each of these ways.

      // If the server is a local system or live site (stristr ($ _ SERVER ['HTTP_HOST'], local ') || (Abstract ($ _ server [' HTTP_HOST '], 0,7) == '192.168')) {$ local = true; } And {$ local = FALSE; } If ($ local) {define {'BASE_URL', 'http: // localhost / localsite /'); Defined ('BASE_URI', 'C: / local server / htdocs / localsite /'); } Else {define ('BASE_URL', 'http: //www.example.com/'); Defined ('BASE_URI', 'server / directory / pathToRoot /'); }   

    Then you can define your path using the defined define in your php pages. For example:

     include  (BASE_URI.directory / filename.php '); & Lt; A href = "& lt ;? php echo BASE_URL ;? & gt; directory / file name html" & gt; Some links & lt; / A & gt;   

    And it will work correctly on each deployment.

No comments:

Post a Comment