11.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. function generateHash($macAddress, $timestamp, $customString) {
  3. // 组合输入字符串
  4. $inputString = $macAddress . $timestamp . $customString;
  5. // 使用SHA256进行加密
  6. $hash = hash('sha256', $inputString);
  7. return $hash;
  8. }
  9. function get_all_headers() {
  10. $headers = [];
  11. foreach ($_SERVER as $key => $value) {
  12. if (substr($key, 0, 5) === 'HTTP_') {
  13. $header = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5)))));
  14. $headers[$header] = $value;
  15. }
  16. }
  17. return $headers;
  18. }
  19. $headers = get_all_headers();
  20. //有一个不存在就判断异常
  21. if(!isset($headers['X-Special-Header'])||!isset($headers['Timestamp'])||!isset($headers['Clinet-Mac'])){
  22. die("非法访问");
  23. }
  24. //foreach ($headers as $name => $value) {
  25. // echo "$name: $value<br>";
  26. //}
  27. //X-Special-Header token 字段 ='sha256' (Mac+时间戳+自定义字符串)
  28. //Timestamp 时间戳
  29. //Clinet-Mac mac地址
  30. //10秒内可以访问
  31. if((time()-$headers['Timestamp'])>10){
  32. die("无效访问");
  33. }
  34. if(generateHash($headers['Clinet-Mac'],$headers['Timestamp'],"minong123")!=$headers['X-Special-Header']){
  35. die("非法访问");
  36. }
  37. ?>