开发者

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 differen开发者_运维知识库ce between them? SERVER_NAME works, but can be injected, so isn't secure. Does someone have a solution? (Non-Static Solution preferable)


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.


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


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.


this works for me

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜