Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/257557-define-base-urls/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320118
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320123
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320125
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320135
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320143
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320145
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257557-define-base-urls/#findComment-1320151
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.