Skip to content
Advertisement

Get PHP $_SESSION data in .htaccess for LiteSpeed cache

I need to send data from $_SESSION from PHP file to .htaccess for setup Vary variable for setting LiteSpeed cache.

PHP script example:

<?php $lang = $_SESSION['lang'];?>

.htaccess example

...
<IfModule LiteSpeed>
CacheLookup public on
CacheLookup private on
RewriteRule .* - [E=cache-control:max-age=604800]
RewriteRule .* - [E=cache-control:vary=**!!!HERE I NEED TO HAVE $lang value!!!**]
</IfModule>
...

I already tried these ways:

  1. Via cookies. I set PHP variable in the cookies and get it from there in .htaccess via "RewriteCond %{HTTP_COOKIE} (.*) [NC]". But I will have also private data which I can’t set in the cookies. So this approach is not very good.
  2. Via ENV variables (putenv("PROVARY=".$_SESSION['lang']);). But it does not work. Because they die at the end of the request.
  3. HTTP_SESSION. I found this name of a module but didn’t find info about how it works or even how to use it.

Is it possible to get $_SESSION variables from .htaccess directly? If yes, show a working example of how it works.

Advertisement

Answer

It’s never a good idea to cache pages, based only on session vars – especially, if the content only varies based on the language stored inside the session. Even LiteSpeed say, to don’t do so

However, you could store the language additonally inside a cookie and vary on the cookie value.

For further projects I would recommend to store the language inside the uri (example.com/en/..., example.com/de/...)

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement