Source Code Handle localhost and Server?
Posted: 02. July 2010 16:08
Hi -
I recently began using XAMPP for development and I love it!
One thing I just can't figure out is how to have one set of source code that works both on my local machine for development and on the server for production. What I mean by "works" is that my code as viewed in a browser should look and function the same.
I started by using the super global variable $_SERVER["SERVER_NAME"] in PHP to find out if I am on my development machine or on the production server. Then, I take the value this variable gives me and try to use it to set the hyperlinks on my site correctly. For example:
This works in the production environment, but not in the XAMPP environment because the code is stored in the "htdocs/mySiteFolder" directory.
I then tried to edit my $hostName variable to be concatenated with the '/mySiteFolder' portion that was missing if $hostName == "localhost". I was expecting to get "http://localhost/mySiteFolder/fun/1", but instead I always get "http://localhost/fun/1", which doesn't work...
I am trying to do this with clean URLs since that is what I use on my site and I would like my production and development code to be as similar as possible. I'm sure someone has made this work before. What am I missing to make this work?
Thanks in advance!
I recently began using XAMPP for development and I love it!
One thing I just can't figure out is how to have one set of source code that works both on my local machine for development and on the server for production. What I mean by "works" is that my code as viewed in a browser should look and function the same.
I started by using the super global variable $_SERVER["SERVER_NAME"] in PHP to find out if I am on my development machine or on the production server. Then, I take the value this variable gives me and try to use it to set the hyperlinks on my site correctly. For example:
- Code: Select all
<?php
$hostName = $_SERVER["SERVER_NAME"];
echo '<a href = "http://'.$hostName.'/fun/1">Fun Link</a>';
?>
This works in the production environment, but not in the XAMPP environment because the code is stored in the "htdocs/mySiteFolder" directory.
I then tried to edit my $hostName variable to be concatenated with the '/mySiteFolder' portion that was missing if $hostName == "localhost". I was expecting to get "http://localhost/mySiteFolder/fun/1", but instead I always get "http://localhost/fun/1", which doesn't work...
I am trying to do this with clean URLs since that is what I use on my site and I would like my production and development code to be as similar as possible. I'm sure someone has made this work before. What am I missing to make this work?
Thanks in advance!