PHP dynamics

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devinmcmanus
Forum Newbie
Posts: 5
Joined: Mon Aug 27, 2007 6:29 pm

PHP dynamics

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Look at details in $_SERVER.
devinmcmanus
Forum Newbie
Posts: 5
Joined: Mon Aug 27, 2007 6:29 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Most often you don't need to use full URLs for internal links unless they are across subdomains.
devinmcmanus
Forum Newbie
Posts: 5
Joined: Mon Aug 27, 2007 6:29 pm

Post by devinmcmanus »

So basically you're saying I should switch everything to use relative paths?
devinmcmanus
Forum Newbie
Posts: 5
Joined: Mon Aug 27, 2007 6:29 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
devinmcmanus
Forum Newbie
Posts: 5
Joined: Mon Aug 27, 2007 6:29 pm

Post by devinmcmanus »

Can you give an example please?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

They would look like any other absolute path for an operating system (without a drive letter if you're on Windows.)
Post Reply