apache 在 .htaccess 中获取 PHP 会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/342509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get PHP session vars in .htaccess
提问by Pim Jager
Is it possible to read the data in the php $_SESSION array in the .htaccess file in Apache? So say I have the following:
是否可以在 Apache 的 .htaccess 文件中读取 php $_SESSION 数组中的数据?所以说我有以下几点:
$_SESSION['foo'] = 'bar';
could I then in .htaccess do something like:
然后我可以在 .htaccess 中执行以下操作:
RewriteRule bla.png folder/{the php session var foo}/file.png
Is that possible?
那可能吗?
I already have a working workaround but if this is possible it would be way nicer.
我已经有了一个可行的解决方法,但如果可能的话,它会更好。
采纳答案by benlumley
I'm not aware that its possible.
我不知道这是可能的。
But I can think of a few workarounds involving rewriting to a PHP script.
但是我可以想到一些涉及重写 PHP 脚本的解决方法。
回答by jave.web
Answer to the main question:
回答主要问题:
No,at least not as you imagine. The file is called .htaccessbecause they are "distributed configuration files"and of course configuration && accesshandling must happen first - therefor you can't get "PHP session variable" because it simply comes after the processing of the .htaccess
不,至少不像你想象的那样。之所以调用该文件是.htaccess因为它们是“分布式配置文件”,当然必须首先进行配置 &&访问处理 - 因此您无法获得“PHP 会话变量”,因为它只是在处理.htaccess
I will show you some workarounds below, but before - you should pay attention to this:
我将在下面向您展示一些解决方法,但在此之前 - 您应该注意这一点:
Is this really necessary? - suggested solutions:
这真的有必要吗?- 建议的解决方案:
If you just want to redirect filepath /by logged-in user - .htaccess is NOT the way- use just bla.phpas address and then in php file do the redirect - something like this:
如果您只想通过登录用户重定向文件路径/- .htaccess 不是方式- 仅用bla.php作地址,然后在 php 文件中进行重定向 - 如下所示:
<?php
session_start();
header("Location: folder/".$_SESSION['foo']."/file.png");
exit();
?>
However if you can't change the source URL, you will first have to redirect bla.pngto bla.phpbut I guess you know how to do that :-)
但是,如果您无法更改源 URL,则首先必须重定向bla.png到,bla.php但我想您知道该怎么做 :-)
In frameworks using MPV/MVC model there are often scripts for "routing" for a reason - e.g. this one.
Optionally you could make a "php-router" from this script adding some other PHP-relying redirects while using if/elseif/elseor caseto choose what will happen - where the redirect will go.
在使用 MPV/MVC 模型的框架中,通常会有出于某种原因“路由”的脚本 - 例如这个。
或者,您可以从这个脚本中创建一个“php-router”,在使用时添加一些其他依赖 PHP 的重定向,if/elseif/else或者case选择将发生的事情 - 重定向的去向。
OR
或者
You are probably using a session anyway - so why just don't generate the url straight in PHP like: $url = "example.com/bla.png?foo=".$_SESSION['foo'];+ regexp in .htaccessor even:
$url = "example.com/folder/".$_SESSION['foo']."/file.png";
Anyway I guess you are redirecting because you can't (for some reason) do that. :-)
无论如何,您可能正在使用会话 - 那么为什么不直接在 PHP 中生成 url,例如:$url = "example.com/bla.png?foo=".$_SESSION['foo'];+ regexp in.htaccess甚至:
$url = "example.com/folder/".$_SESSION['foo']."/file.png";
无论如何我猜您正在重定向,因为您不能(出于某种原因)这样做。:-)
Workarounds
解决方法
If you are still persuaded that you have to do this in .htaccess file here are some workarounds
如果您仍然被说服必须在 .htaccess 文件中执行此操作,这里有一些解决方法
1) Use cookies
1) 使用cookies
In modern days cookies are often available, so if you "trust" your client's cookies, you could make a redirect rule based on a HTTP_COOKIEserver-variable - assuming you've stored cookie called "foo" before:
在现代,cookie 通常可用,因此如果您“信任”客户端的 cookie,您可以根据HTTP_COOKIE服务器变量制定重定向规则- 假设您之前存储了名为“ foo”的cookie :
RewriteCond %{HTTP_COOKIE} ^(.*)foo=([-_a-zA-Z0-9]+)(.*)$ [NC]
RewriteRule ^bla.png$ /folder/%2/file.png [R=307,NC,L]
#possibly use 302 if you like dinosaurs
As you can see the trick is to create condition checking HTTP_COOKIEserver-variable for some condition. It basicly says:
如您所见,诀窍是HTTP_COOKIE为某些条件创建条件检查服务器变量。它基本上说:
Is there a COOKIE called "foo" which contains only "-, _, lower or upper case letters or numbers"?
If so - redirect example.com/bla.pngto example.com/folder/CURRENT_FOO_VALUE/file.png
[flags]: 307 redirect(R=307), don't mind letter-case (NC), don't apply other rules (L)
是否有一个名为“foo”的 COOKIE 只包含“-、_、小写或大写字母或数字”?
如果是这样 - 将example.com/bla.png重定向到example.com/folder/CURRENT_FOO_VALUE/file.png
[flags]: 307 重定向(R=307),不介意大小写 (NC),不适用其他规则 (L)
Good point is that HTTP_COOKIEserver-variable is structured in this way:
好的一点是HTTP_COOKIE服务器变量的结构是这样的:
name=value; name2=value2
2) NOT recommended - other workarounds:
2)不推荐 - 其他解决方法:
a) read session to HTTP_SESSIONenvironment variable using mod_session
a)HTTP_SESSION使用 mod_session读取会话到环境变量
See the apache manual for mod_sessionfor more info how to read session into the HTTP_SESSIONenv. variable. The approach then would be the same as for the "COOKIE-workaround".
有关如何将 session 读入env 的更多信息,请参阅mod_session的apache 手册HTTP_SESSION。多变的。该方法将与“COOKIE-workaround”相同。
b) store needed info in a text file and read it with the RewriteMapdirective as @Chris suggested
b) 将需要的信息存储在一个文本文件中,并RewriteMap按照@Chris 的建议使用指令读取它
But again you will somehow have to detect which 'foo' is it so maybe SSL_SESSION_IDor some other $_GET/$_POSTparameters?
但同样,您将不得不以某种方式检测它是哪个“foo”,所以也许SSL_SESSION_ID还是其他一些$_GET/$_POST参数?
回答by Tom Haigh
I don't think this is something that you could do easily. You could read the PHPSESSID using something like %{HTTP_COOKIE} in your .htaccess, but in order to get access to the actual data in the session PHP is doing a lot of extra work, so you would have to somehow re-implement that (i.e. reading the data from wherever it is stored, de-serializing etc.)
我不认为这是你可以轻松做到的事情。您可以在 .htaccess 中使用 %{HTTP_COOKIE} 之类的内容读取 PHPSESSID,但是为了访问会话中的实际数据,PHP 做了很多额外的工作,因此您必须以某种方式重新实现(即从存储数据的任何地方读取数据,反序列化等)
回答by Chris
I'm not sure if this will apply to your particular problem or not but RewriteMap is a very useful and often over-looked directive for mod_rewrite.
我不确定这是否适用于您的特定问题,但 RewriteMap 是一个非常有用且经常被忽视的 mod_rewrite 指令。
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritemap
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritemap
If you can pre-compute your session variables or store them to a text file (maybe when they get set) the map entries can be easily retrieved based on any of the available request details.
如果您可以预先计算会话变量或将它们存储到文本文件(可能在设置时),则可以根据任何可用的请求详细信息轻松检索地图条目。
Otherwise, you could put together a simple external mapper (probably, a PHP script as that'd be easiest) that uses the sessionid to determine the value of the session variable and returns the proper URL for the rewrite rule to use.
否则,您可以组合一个简单的外部映射器(可能是最简单的 PHP 脚本),它使用 sessionid 来确定会话变量的值并返回正确的 URL 以供重写规则使用。
回答by rfgamaral
You can't do that the way you want.
你不能那样做。
If you are really using the $_SESSION variable maybe there's an Apache environmental variable that you can use that will have the same value as the $_SESSION one.
如果你真的在使用 $_SESSION 变量,也许你可以使用一个 Apache 环境变量,它与 $_SESSION 具有相同的值。
Look at the following list and see if any of them helps:
http://www.zytrax.com/tech/web/env_var.htm
查看以下列表,看看它们是否有帮助:http:
//www.zytrax.com/tech/web/env_var.htm
You'll have to use it like this:
你必须像这样使用它:
RewriteRule bla.png folder/%{VAR_NAME}/file.png
RewriteRule bla.png 文件夹/%{VAR_NAME}/file.png

