Skip to main content

web的测试与正式环境切换

做了个正式网站 与测试网站的切换功能,大家看看帅不帅?

<?php


if(!empty($_REQUEST['act']))
{
$act = empty($_REQUEST['act']) ? die('你在开玩笑吗?') : $_REQUEST['act'] ;
$hosts = new UpdateHosts();
if ($act == 'test') {
$hosts->add_test_server_ip();
}else if($act == 'official'){
$hosts->del_test_server_ip();
}else
{
$hosts->del_test_server_ip();
}
}

class UpdateHosts
{
//*正式服务器与测试服务器的ip进行切换*//
/*思路
检查windows的hosts文件,如果存在测试的ip字符串,就替换为空。不存在就添加上去。
*/
//测试机ip头

var $win7 = "C:\Windows\System32\drivers\etc\hosts";
var $winxp = "C:\Windows\System32\drivers\etc\hosts";
var $test_server_ip = "192.16.15.21 www.xxx.com\r\n192.16.15.21 m.xxx.com\r\n192.16.15.21 xxx.com";
var $filename = "";
var $file_data = "";
function __construct(){

if(get_os() == 'Windows 7')
{
$this->filename = $this->win7;
}else {
echo '抱歉目前您使用的操作系统,现在还不能进行切换,请联系程序员为您开发。';
die();
}

//如果存在这个文件
if(is_file($this->filename))
{
$this->file_data = file_get_contents($this->filename);
//var_dump($this->file_data);
echo "*****************************************<br>";
}
else
{
echo "抱歉找不到该文件";
}
}

public function del_test_server_ip()
{
//去掉测试ip头
if (strstr($this->file_data,$this->test_server_ip) ) {
$file_content = str_replace("\r\n".$this->test_server_ip, "", $this->file_data);
$fp = fopen($this->filename ,"w");
flock($fp, LOCK_EX) ;
fwrite($fp,$file_content);
flock($fp, LOCK_UN);
fclose($fp);
echo "当前是正式服务器环境";
}else {
echo "当前是正式服务器环境";
}
}

public function add_test_server_ip()
{
//添加测试ip头
if (strstr($this->file_data,$this->test_server_ip)) {
echo "当前是测试服务器环境";
}else {
//添加到hosts文件末尾
$fp = fopen($this->filename ,"a");
flock($fp, LOCK_EX) ;
fwrite($fp,"\r\n".$this->test_server_ip);
flock($fp, LOCK_UN);
fclose($fp);
echo "当前是测试服务器环境";
}
}
}

/**
* 获取客户端操作系统信息
* @param null
* @return string
*/
function get_os()
{
$agent = $_SERVER['HTTP_USER_AGENT'];
$os = false;

if (preg_match('/win/i', $agent) &amp;&amp; strpos($agent, '95'))
{
$os = 'Windows 95';
}
else if (preg_match('/win 9x/i', $agent) &amp;&amp; strpos($agent, '4.90'))
{
$os = 'Windows ME';
}
else if (preg_match('/win/i', $agent) &amp;&amp; preg_match('/98/i', $agent))
{
$os = 'Windows 98';
}
else if (preg_match('/win/i', $agent) &amp;&amp; preg_match('/nt 6.0/i', $agent))
{
$os = 'Windows Vista';
}
else if (preg_match('/win/i', $agent) &amp;&amp; preg_match('/nt 6.1/i', $agent))
{
$os = 'Windows 7';
}
else if (preg_match('/win/i', $agent) &amp;&amp; preg_match('/nt 6.2/i', $agent))
{
$os = 'Windows 8';
}
else if (preg_match('/win/i', $agent) &amp;&amp; preg_match('/nt 5.1/i', $agent))
{
$os = 'Windows XP';
}
else if (preg_match('/win/i', $agent) &amp;&amp; preg_match('/nt 5/i', $agent))
{
$os = 'Windows 2000';
}
else if (preg_match('/win/i', $agent) &amp;&amp; preg_match('/nt/i', $agent))
{
$os = 'Windows NT';
}
else if (preg_match('/win/i', $agent) &amp;&amp; preg_match('/32/i', $agent))
{
$os = 'Windows 32';
}
else if (preg_match('/linux/i', $agent))
{
$os = 'Linux';
}
else if (preg_match('/unix/i', $agent))
{
$os = 'Unix';
}
else if (preg_match('/sun/i', $agent) &amp;&amp; preg_match('/os/i', $agent))
{
$os = 'SunOS';
}
else if (preg_match('/ibm/i', $agent) &amp;&amp; preg_match('/os/i', $agent))
{
$os = 'IBM OS/2';
}
else if (preg_match('/Mac/i', $agent) &amp;&amp; preg_match('/PC/i', $agent))
{
$os = 'Macintosh';
}
else if (preg_match('/PowerPC/i', $agent))
{
$os = 'PowerPC';
}
else if (preg_match('/AIX/i', $agent))
{
$os = 'AIX';
}
else if (preg_match('/HPUX/i', $agent))
{
$os = 'HPUX';
}
else if (preg_match('/NetBSD/i', $agent))
{
$os = 'NetBSD';
}
else if (preg_match('/BSD/i', $agent))
{
$os = 'BSD';
}
else if (preg_match('/OSF1/i', $agent))
{
$os = 'OSF1';
}
else if (preg_match('/IRIX/i', $agent))
{
$os = 'IRIX';
}
else if (preg_match('/FreeBSD/i', $agent))
{
$os = 'FreeBSD';
}
else if (preg_match('/teleport/i', $agent))
{
$os = 'teleport';
}
else if (preg_match('/flashget/i', $agent))
{
$os = 'flashget';
}
else if (preg_match('/webzip/i', $agent))
{
$os = 'webzip';
}
else if (preg_match('/offline/i', $agent))
{
$os = 'offline';
}
else
{
$os = '未知操作系统';
}
return $os;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>测试与正式环境切换</title>
</head>

<body>
<p>切换到 -> <a href="?act=test"> 测试环境</a></p>
<p>切换到 -> <a href="?act=official"> 正式环境</a></p>
</body>
</html>