PHP obtains browser request headers attributes and attribute values

Recently, there is a demand. If request with parameters, my website will give it a specific page, otherwise the normal page will be displayed.

The parameters passed from the homepage will be displayed in the Request Headers in the browser developer tool, so first get the attributes inside to determine whether there is a designated page.

<?php
	function judge(){
		// 服务器端跨域设置
			header('Access-Control-Allow-Origin:*'); 
			// 
			if (!function_exists('getallheaders')) 
			{ 
				function getallheaders() 
				{ 
					   $headers = []; 
				   foreach ($_SERVER as $name => $value) 
				   { 
					   if (substr($name, 0, 5) == 'HTTP_') 
					   { 
						   $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
					   } 
				   } 
				   return $headers; 
				} 
			} 
			$head = [];
			foreach (getallheaders() as $name => $value) {
				//echo "$name: $value\n"."<br>";
				$head[] = strtolower($name); //将所有数据存在数组并转为小写 防止大小而判断出错
				
			}
			#  print_r($head);
			if (in_array("accept",$head)){//Accept
			  //true 特定页面
			  echo file_get_contents("index1.html");
			}
			else{
			 // false 正常页面
			  echo file_get_contents("index2.html");
			}
	}
	judge();
?>

Follow the specific page of the above code page, because each page of Accept will be accessed normally, and you can modify it to your specific parameters according to your needs.

 

Guess you like

Origin blog.csdn.net/tang242424/article/details/115256134
Recommended