简体   繁体   English

在.htaccess中获取PHP会话变量

[英]Get PHP session vars in .htaccess

Is it possible to read the data in the php $_SESSION array in the .htaccess file in Apache? 是否可以读取Apache中.htaccess文件中的php $ _SESSION数组中的数据? So say I have the following: 所以说我有以下内容:

$_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. 我已经有了一个可行的解决方法,但如果可行的话,它会更好。

Answer to the main question: 回答主要问题:

No, at least not as you imagine. 不,至少不像你想象的那样。 The file is called .ht access because they are "distributed configuration files" and of course configuration && access handling must happen first - therefor you can't get "PHP session variable" because it simply comes after the processing of the .htaccess 该文件被称为.ht access因为它们是“分布式配置文件” ,当然配置&& 访问处理必须首先发生 - 因此,您无法获得“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.php as 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.png to bla.php but 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 - eg this one. 在使用MPV / MVC模型的框架中,通常存在用于“路由”的脚本 - 例如这个。
Optionally you could make a "php-router" from this script adding some other PHP-relying redirects while using if/elseif/else or case to choose what will happen - where the redirect will go. 您可以选择使用此脚本创建一个“php-router”,添加一些其他依赖PHP的重定向,同时使用if/elseif/elsecase来选择将要发生的事情 - 重定向将在哪里。

OR 要么

You are probably using a session anyway - so why just don't generate the url straight in PHP like: 您可能正在使用会话 - 所以为什么不直接在PHP中生成URL,如:
$url = "example.com/bla.png?foo=".$_SESSION['foo']; + regexp in .htaccess or even: + .htaccess表达式甚至:
$url = "example.com/folder/".$_SESSION['foo']."/file.png";
Anyway I guess you are redirecting because you can't (for some reason) do that. 无论如何,我猜你是重定向因为你不能(出于某种原因)这样做。 :-) :-)

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_COOKIE server-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_COOKIE server-variable for some condition. 正如您所看到的,诀窍是为某些条件创建条件检查HTTP_COOKIE服务器变量。 It basicly says: 它基本上说:

Is there a COOKIE called "foo" which contains only "-, _, lower or upper case letters or numbers"? 是否有名为“foo”的COOKIE仅包含“ - ,_,小写或大写字母或数字”?
If so - redirect example.com/bla.png to example.com/folder/CURRENT_FOO_VALUE/file.png 如果是这样 - 将example.com/bla.png重定向到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) [flags]307重定向 (R = 307),不介意字母(NC),不适用其他规则(L)

Good point is that HTTP_COOKIE server-variable is structured in this way: 好的一点是, HTTP_COOKIE服务器变量的结构是这样的:

name=value; name2=value2

2) NOT recommended - other workarounds: 2)不推荐 - 其他解决方法:

a) read session to HTTP_SESSION environment variable using mod_session a)使用mod_session读取HTTP_SESSION环境变量的会话

See the apache manual for mod_session for more info how to read session into the HTTP_SESSION env. 有关如何将会话读入HTTP_SESSION 环境的更多信息,请参阅mod_sessionapache手册 variable. 变量。 The approach then would be the same as for the "COOKIE-workaround". 那么方法将与“COOKIE-workaround”相同。

b) store needed info in a text file and read it with the RewriteMap directive as @Chris suggested b)将所需信息存储在文本文件中,并使用RewriteMap指令将其读取为@Chris建议

But again you will somehow have to detect which 'foo' is it so maybe SSL_SESSION_ID or some other $_GET/$_POST parameters? 但是你又会以某种方式检测出哪个'foo',所以可能是SSL_SESSION_ID或其他一些$_GET/$_POST参数?

I'm not aware that its possible. 我不知道它可能。

But I can think of a few workarounds involving rewriting to a PHP script. 但我可以想到一些涉及重写PHP脚本的变通方法。

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 (ie reading the data from wherever it is stored, de-serializing etc.) 您可以在.htaccess中使用类似%{HTTP_COOKIE}的内容来读取PHPSESSID,但为了能够访问会话中的实际数据,PHP正在做很多额外的工作,因此您必须以某种方式重新实现(即从任何存储位置读取数据,反序列化等)

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。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM