PHP $_SERVER['HTTP_REFERER'] 无效

本文深入探讨PHP中的超级全局变量$GLOBALS和$_SERVER的作用与应用场景,特别是$_SERVER['HTTP_REFERER']的可靠性及其在不同情况下的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学到PHP的超级全局变量时,遇到了PHP $_SERVER['HTTP_REFERER'],不料在phpstorm中运行的到如下结果:

<?php
$x=5;
$y=10;

function addtion(){
    $GLOBALS["z"]=$GLOBALS["x"]+$GLOBALS["y"];/** $GLOBALS 是PHP的一个超级全局变量组,在一个PHP脚本的全部作用域中都可以访问。
                                                * $GLOBALS 是一个包含了全部变量的全局组合数组。变量的名字就是数组的键。*/
}

addtion();
echo $z."<br>";
var_dump ($z);
echo "<br>";

/**
 * $_SERVER 是一个包含了诸如头信息(header)、路径(path)、以及脚本位置(script locations)等等信息的数组。这个数组中的项目由 Web 服务器创建。
 * 不能保证每个服务器都提供全部项目;服务器可能会忽略一些,或者提供一些没有在这里列举出来的项目。
 * 以下实例中展示了如何使用$_SERVER中的元素:
 */
echo $_SERVER["PHP_SELF"]."<br>";
echo $_SERVER["SERVER_NAME"]."<br>";
echo $_SERVER["HTTP_HOST"]."<br>";
echo $_SERVER["HTTP_REFERER"]."<br>";
echo $_SERVER["HTTP_USER_AGENT"]."<br>";
echo $_SERVER["SCRIPT_NAME"]."<br>";

我们知道,它得到的是:引导用户代理到当前页的前一页的地址(如果存在)。由 user agent 设置决定。并不是所有的用户代理都会设置该项,有的还提供了修改 HTTP_REFERER 的功能。简言之,该值并不可信。),也就是说,用户代理(浏览器)可能更改了它,也可能是当前页没有前一页(这个意思就是说当前页是点击前一页(来源页面)中的链接得来的,我觉得最可能是这个,毕竟我是用phpstorm运行滴,后面我证实了确实是这样,解决方法在最下面的博客链接里)

查阅得知:它无效还可能是以下几种情况。这几种情况源自https://www.cnblogs.com/caicaizi/p/6053890.html

PHP $_SERVER['HTTP_REFERER'] 无效

需要注意的是,$_SERVER['HTTP_REFERER'] 完全来源于浏览器。并不是所有的用户代理(浏览器)都会设置这个变量,而且有的还可以手工修改 HTTP_REFERER。因此,$_SERVER['HTTP_REFERER'] 不总是真实正确的。

通常下面的一些方式,$_SERVER['HTTP_REFERER'] 会无效:

  1. 直接输入网址访问该网页。
  2. Javascript 打开的网址。
  3. Javascript 重定向(window.location)网址。
  4. 使用 meta refresh 重定向的网址。
  5. 使用 PHP header 重定向的网址。
  6. flash 中的链接。
  7. 浏览器未加设置或被用户修改。

所以一般来说,只有通过 <a></a> 超链接以及 POST 或 GET 表单访问的页面,$_SERVER['HTTP_REFERER'] 才有效。

由于 $_SERVER['HTTP_REFERER'] 对 POST 表单访问也是有效的,因此在表单数据处理页面一定程度上可以通过校验 $_SERVER['HTTP_REFERER'] 来防止表单数据的恶意提交。但该方法并不能保证表单数据的绝对正确,即对表单数据的真实性检测并不能完全依赖于 $_SERVER['HTTP_REFERER'] 。

 

注意!!!

这个问题我可能已经解决了,详见我的这篇博客后面突然就想明白了:https://blog.csdn.net/doubleguy/article/details/90166275

#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream backend { server 127.0.0.1:8848; } server { listen 8848; server_name localhost; #前端打的dist资源存放目录 root G:\JEECG\JeecgBoot\jeecgboot-vue3\dist; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; index index.html index.htm; # 用于配合 browserHistory使用 try_files $uri $uri/ /index.html; } location /jeecgboot/ { #后台接口地址(我们部署去掉了/jeecg-boot项目名,如果你有请加上) proxy_pass http://127.0.0.1:8848/; proxy_redirect off; #真实IP获取 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; set $my_proxy_add_x_forwarded_for $proxy_add_x_forwarded_for; if ($proxy_add_x_forwarded_for ~* "127.0.0.1"){ set $my_proxy_add_x_forwarded_for $remote_addr; } proxy_set_header X-Forwarded-For $my_proxy_add_x_forwarded_for; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } 帮我看看哪里有问题nginx起不起来了
最新发布
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小的香辛料

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值