Simon
Simon

Reputation: 5698

Where's the script located? Localhost or on the Server?

Like many programmers I have a server where I host my scripts, and a localhost where I make them. Is there in PHP a secure way to let the script tell the difference between them? SERVER_NAME works, but can be injected, so isn't secure. Does someone have a solution? (Non-Static Solution preferable)

Upvotes: 1

Views: 194

Answers (6)

user229044
user229044

Reputation: 239312

You can use php_uname('n') to return the current host's name, and use that to decide which environment you're in, but there isn't much benefit to this over simply defining your environment in a config file. You only have to do so once per installation.

I personally find the easiest way is to define an ENVIRONMENT symbol as 'development', 'testing', or 'production' in a config file which is excluded from version control. I include that file in some part of my project and then assert that the ENVIRONMENT symbol is defined. The program will tank if I've checked out a copy of my project and forgot to create the config file or specify the environment within it.

You could combine these approaches to define ENVIRONMENT based on host name.

Upvotes: 2

Pekka
Pekka

Reputation: 449525

Because my development and production paths are always different, I usually tend to check dirname(__FILE__) against an array of possible values.

Depending on which value I end up with, the correct config file will be loaded.

Upvotes: 0

TeAmEr
TeAmEr

Reputation: 4773

create a file somewhere out of the www . the do a if(file_exists('YOUR FILE PATH')) ...

give it a strange name . in the c:\ if ur on windows or in the /usr/home/ if u are on linux

Upvotes: 0

bkilinc
bkilinc

Reputation: 989

this works for me

if ($_SERVER['HTTP_HOST'] == 'localhost')
{
   // it is  local host
}

Upvotes: 0

Paul Dixon
Paul Dixon

Reputation: 300895

Generally in Apache each VirtualHost will have a specific set of hostnames and aliases defined for it. This is the name you'll retrieve from $_SERVER['HTTP_HOST'] and is essentially the data from the Host: HTTP header

The only time you might need further checks for this is when your vhost is the default vhost, and so fields requests for domains not associated with host vhosts, or your vhost uses wildcards for the ServerName or ServerAlias.

Upvotes: 0

ceejayoz
ceejayoz

Reputation: 180023

I tend to check the IP of $_SERVER['SERVER_ADDR'], or just have a config setting indicate which is which..

Upvotes: 0

Related Questions