SyntheticShield Posted February 22, 2012 Share Posted February 22, 2012 My goal was to create a constant that could be included on the pages, that no matter where it was located in the structure, would be able to find the template files, css, images, etc. using the following code: if($_SERVER['HTTP_HOST'] == "localhost"){ define('SITEURL', 'http://' . $_SERVER['HTTP_HOST']); define('SITEPATH', $_SERVER['DOCUMENT_ROOT']); define('CSS', $_SERVER['DOCUMENT_ROOT'] . '/css/'); define('IMAGES', $_SERVER['DOCUMENT_ROOT'] . '/images/'); } else{ define('SITEURL', 'http://' . $_SERVER['HTTP_HOST']); define('SITEPATH', $_SERVER['DOCUMENT_ROOT']); define('TEMPLATE', $_SERVER['DOCUMENT_ROOT'] . '/incs/template/'); define('CSS', $_SERVER['DOCUMENT_ROOT'] . '/css/'); define('IMAGES', $_SERVER['DOCUMENT_ROOT'] . '/images/'); } I put the above in a variables.php file that is in the includes folder (named incs) and I call it using the typical include statement. The problem is that when I use the constant to grab the CSS file, its not working when using: <link rel="stylesheet" href="<?php echo CSS . "template.css" ?>" type="text/css" media="screen" /> I can view the page source and it has the path right, but I cannot figure out how to get it to actually pull in the css file. I know the echo command is not correct, at least I dont think it is, but have not been able to figure out any other way. Also, is there a better way to define the constants to the file locations and have it automatically detect whether its on the test server or production and then be able to reference those locations no matter where it is in the structure? Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/ Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 Anything the browser has to request from the server (like a stylesheet or image) should be referenced by a valid url, not a filesystem path. Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320111 Share on other sites More sharing options...
SyntheticShield Posted February 22, 2012 Author Share Posted February 22, 2012 So I should be using HTTP_HOST instead of DOCUMENT_ROOT? Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320116 Share on other sites More sharing options...
AyKay47 Posted February 22, 2012 Share Posted February 22, 2012 So I should be using HTTP_HOST instead of DOCUMENT_ROOT? I would use the relative paths to the files. /path/from/server/to/css/template.css EDIT: since the paths are different for both servers, you will need to use another $_SERVER value as Pika has suggested. Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320118 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 Probably going to need something like this. Echo it and make sure it's correct. define('CSS', 'http://' . $_SERVER['SERVER_NAME'] . '/css/'); Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320119 Share on other sites More sharing options...
SyntheticShield Posted February 22, 2012 Author Share Posted February 22, 2012 Pikachu, that seems to work identically to HTTP_HOST, but would using SERVER_NAME be better since it would work on both test and production? Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320121 Share on other sites More sharing options...
AyKay47 Posted February 22, 2012 Share Posted February 22, 2012 Here is a little comparison, I will refer to the values as HTTP_HOST and SERVER_NAME 1. HTTP_HOST is obtained from the HTTP header request and is completely controlled form the client browser and therefore may not be reliable. 2. SERVER_NAME is defined in the server configuration file (normally httpd.conf), which means the value is more reliable however you must make sure that it is configured correctly and available. Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320123 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 From all I've read, $_SERVER['SERVER_NAME'] is the safer of the two to use. Although both values can be manipulated, $_SERVER['SERVER_NAME'] appears to automagically convert "bad" characters to htmlentities. For absolute control, hard code the constant to the base URL of the server. Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320125 Share on other sites More sharing options...
scootstah Posted February 22, 2012 Share Posted February 22, 2012 For absolute control, hard code the constant to the base URL of the server. This is the best way, I think. Just make a config file and hard code the URL into it. You could always do a check that if the setting isn't set to try to guess the right URL. Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320135 Share on other sites More sharing options...
AyKay47 Posted February 22, 2012 Share Posted February 22, 2012 For absolute control, hard code the constant to the base URL of the server. This is the best way, I think. Just make a config file and hard code the URL into it. You could always do a check that if the setting isn't set to try to guess the right URL. why would you need to make a config file. Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320143 Share on other sites More sharing options...
scootstah Posted February 22, 2012 Share Posted February 22, 2012 For absolute control, hard code the constant to the base URL of the server. This is the best way, I think. Just make a config file and hard code the URL into it. You could always do a check that if the setting isn't set to try to guess the right URL. why would you need to make a config file. Why not? There should be several other things you would need such as database credentials. Why not put them in a common place? Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320145 Share on other sites More sharing options...
AyKay47 Posted February 22, 2012 Share Posted February 22, 2012 For absolute control, hard code the constant to the base URL of the server. This is the best way, I think. Just make a config file and hard code the URL into it. You could always do a check that if the setting isn't set to try to guess the right URL. why would you need to make a config file. Why not? There should be several other things you would need such as database credentials. Why not put them in a common place? an include file? It could be convenient but is not needed for a simple hard coded base url. Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320151 Share on other sites More sharing options...
scootstah Posted February 22, 2012 Share Posted February 22, 2012 So where would you put it then? Quote Link to comment https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320152 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.