浏览器请求的'/'的时候,假如服务器重定向到'/test',这个时候请求'/test'的headers里面会有referer吗?为什么?怎样可以让浏览器带上?
var express = require('express') , http = require('http') , path = require('path'); var app = express(); app.configure(function(){ app.set('port', process.env.PORT || 8888); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(path.join(__dirname, 'public'))); }); app.configure('development', function(){ app.use(express.errorHandler()); }); app.get('/', function(req, res, next) { res.redirect('/test'); }); app.get('/test', function(req, res, next) { res.send(req.headers); }); http.createServer(app).listen(app.get('port'), function(){ console.log("Express server listening on port " + app.get('port')); });
返回:
{ host: "localhost:8888", connection: "keep-alive", user-agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4", accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", accept-encoding: "gzip,deflate,sdch", accept-language: "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4", accept-charset: "UTF-8,*;q=0.5", cookie: "nd_secret=44f180cce9c8e88023d056a4b6a06a116d81dbf0de5c95349100946a3d6fbc294826df4b1bc4b5ba30927fb7002bd427; connect.sid=s%3A0ajlt%2FaQVFfX%2Fn%2F7Bi%2Ffa%2B9z.3z9KvkiqVTozWD%2BMIm8XOOwzoINHTATA0pH7sYOeM7o" }
ringa_lee2017-04-17 11:00:53
和浏览器版本有关系,标准也并没有对此做出说明。
所以最好不要依赖referer,这也是为什么很多网站都会将源地址当作参数传过去。
参见这里
PHPz2017-04-17 11:00:53
正确的做法是重定向时用/?from=/test
这样的地址。浏览器的行为没有办法确定,况且还不一定是浏览器在发起HTTP请求,如果是搜索引擎spider或命令行的脚本呢?