Page 1 of 1

PHP dynamics

Posted: Mon Aug 27, 2007 6:49 pm
by devinmcmanus
Hi I have a PHP site that currently has a production environment only. I am thinking about adding a staging environment. Here's a sample piece of code:

Code: Select all

<?php
$siteRoot = "http://www.myhost.com/mysite";
?>

<html>
<body>
<img src="<?php echo $siteRoot; ?>/_img/misc/myimage.jpg" />
</body>
</html>
What I would like to do is modify $siteRoot to be dynamic. In other words, I would like to be able to to have $siteRoot automatically detect where it is so that I can develop my site on http://www.myhost.com/stage/mysite/ or http://stage.myhost.com/mysite and deploy the live site to http://www.myhost.com/mysite. Is there any way this can be done?

Posted: Mon Aug 27, 2007 6:55 pm
by feyd
Look at details in $_SERVER.

Posted: Mon Aug 27, 2007 7:01 pm
by devinmcmanus
OK cool feyd. Assuming $siteRoot is in an include file (ex: http://www.myhost.com/mysite/include/php_data.php) is there any way to set this up to work if I have a page like http://www.myhost.com/mysite/about/about.php and a CSS file in http://www.myhost.com/mysite/css/layout.css?

Posted: Mon Aug 27, 2007 7:05 pm
by feyd
Most often you don't need to use full URLs for internal links unless they are across subdomains.

Posted: Mon Aug 27, 2007 7:18 pm
by devinmcmanus
So basically you're saying I should switch everything to use relative paths?

Posted: Mon Aug 27, 2007 7:25 pm
by devinmcmanus
I want to make sure that I don't break anything like http://www.myhost.com/mysite/index.php. Also, I'm using a header.php file in http://www.myhost.com/mysite/includes/header.php. Hope this helps.

Posted: Mon Aug 27, 2007 7:36 pm
by feyd
You can still use absolute paths, but the protocol, domain and port are not required when they are the same as the current URL.

Posted: Mon Aug 27, 2007 7:43 pm
by devinmcmanus
Can you give an example please?

Posted: Mon Aug 27, 2007 7:45 pm
by feyd
They would look like any other absolute path for an operating system (without a drive letter if you're on Windows.)