| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- function generateHash($macAddress, $timestamp, $customString) {
- // 组合输入字符串
- $inputString = $macAddress . $timestamp . $customString;
- // 使用SHA256进行加密
- $hash = hash('sha256', $inputString);
- return $hash;
- }
- function get_all_headers() {
- $headers = [];
- foreach ($_SERVER as $key => $value) {
- if (substr($key, 0, 5) === 'HTTP_') {
- $header = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5)))));
- $headers[$header] = $value;
- }
- }
- return $headers;
- }
- $headers = get_all_headers();
- //有一个不存在就判断异常
- if(!isset($headers['X-Special-Header'])||!isset($headers['Timestamp'])||!isset($headers['Clinet-Mac'])){
- die("非法访问");
- }
- //foreach ($headers as $name => $value) {
- // echo "$name: $value<br>";
- //}
- //X-Special-Header token 字段 ='sha256' (Mac+时间戳+自定义字符串)
- //Timestamp 时间戳
- //Clinet-Mac mac地址
- //10秒内可以访问
- if((time()-$headers['Timestamp'])>10){
- die("无效访问");
- }
- if(generateHash($headers['Clinet-Mac'],$headers['Timestamp'],"minong123")!=$headers['X-Special-Header']){
- die("非法访问");
- }
- ?>
|