Source Code Handle localhost and Server?

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Source Code Handle localhost and Server?

Postby zenthoef » 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:

Code: Select all
$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!
Posts: 1
Joined: 02. July 2010 15:43

Re: Source Code Handle localhost and Server?

Postby WilliL » 02. July 2010 19:38

om my xampp 1.7.3 your excample works (standard configuration - no *.ini changed)
Posts: 660
Joined: 08. January 2010 10:54
Operating System: Win7Home Prem 64 SP1

Re: Source Code Handle localhost and Server?

Postby Nobbie » 02. July 2010 21:00

zenthoef wrote:What am I missing to make this work?

First of all, you are missing to show us the REAL and FULL code which does not seem to work.
Posts: 11613
Joined: 09. March 2008 13:04

Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 100 guests