I assume this must be trivial for the PHP experts, but since I'm not one of them...
On my Apache/PHP website, the links to other pages on the site are coded as relative paths, e.g. 'main/page01.php'. However, for various reasons, I would like to code some of them as full paths, e.g. 'http://www.mysite.com/main/page01.php'.
Now, I am developing and testing this website on a PC where the corresponding address is something like 'http://localhost:8080/mysite/test/main/page01.php'. So, in order to allow local testing, I do not want to code full server-side paths. Instead, I would like to use a variable that, according to the context (local PC or server), delivers the correct path.
I thought that some coding with $_SERVER would allow me to do so, but it seems more complex than that. So, is there anyone who knows how to code this or who has some other suggestion for doing this?
Coding the full path as a variable
Moderator: General Moderators
-
rosbaldeston
- Forum Newbie
- Posts: 2
- Joined: Mon Sep 06, 2010 8:31 am
Re: Coding the full path as a variable
One option is to declare the path in a global variable, in a script which is included by every page in your application.
For example, in a file called "settings.php":
And then in each of your pages, put this at the top:
To use this variable, do something like this:
For example, in a file called "settings.php":
Code: Select all
<?php
$GLOBALS['APPLICATION-PATH'] = 'http://www.yourdomainname.com/folder1/folder2/myapplication/';
?>Code: Select all
<?php
require('settings.php');
?>Code: Select all
<a href="<?php echo($GLOBALS['APPLICATION-PATH']); ?>subfolder/file.php">Click this link to go somewhere</a>Re: Coding the full path as a variable
Thanks for your reply. However, I fear that I wasn't clear enough in my initial question. Using your approach does not really solve my problem since this code will only work correctly on the server, but not while I'm testing on the PC. I want to keep all coding identical in both environments, so there is no risk of forgetting certain differences when I upload a revision.
So, currently, I use the following piece of code that solves the problem. I fear this is not the greatest piece of PHP code written, but it does the job. For the server part, I think the code is generic. For the PC environment, the code works as long as I do not change the names of the folders. However, since that's my stuff, it's OK. If better ideas...
So, currently, I use the following piece of code that solves the problem. I fear this is not the greatest piece of PHP code written, but it does the job. For the server part, I think the code is generic. For the PC environment, the code works as long as I do not change the names of the folders. However, since that's my stuff, it's OK. If better ideas...
Code: Select all
if ($_SERVER['SERVER_NAME'] == 'localhost') {
$w = substr ($_SERVER['PHP_SELF'],0,23);
$content_path_full = 'http://localhost:8080'.$w.$language.'/content/';
$skin_path_full = 'http://localhost:8080'.$w.$language.'/'.$skin.'/';
$server_path = 'http://localhost:8080'.$w;
} else {
$content_path_full = 'http://'.$_SERVER['SERVER_NAME'].'/'.$language.'/content/';
$skin_path_full = 'http://'.$_SERVER['SERVER_NAME'].'/'.$language.'/'.$skin.'/';
$server_path = 'http://'.$_SERVER['SERVER_NAME'].'/';
}