views:

1351

answers:

4

Is it possible to read the data in the php $_SESSION array in the .htaccess file in Apache? So say I have the following:

$_SESSION['foo'] = 'bar';

could I then in .htaccess do something like:

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.

+3  A: 

I'm not aware that its possible.

But I can think of a few workarounds involving rewriting to a PHP script.

benlumley
Yes, rewrite to a PHP script, let PHP output the PNG.
OIS
That is what I do right now.
Pim Jager
A: 

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.

Look at the following list and see if any of them helps:
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

Nazgulled
A: 

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.)

Tom Haigh
+1  A: 

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.

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.

Chris