Page 1 of 2
SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 5:34 am
by someberry
Hi guys. Is it possible to add SERVER variables to the .htaccess file?
I am trying to do something like this, without success:
Code: Select all
SetEnv ROOT $_SERVER['DOCUMENT_ROOT']
to be able to do this:
Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 5:38 am
by Ollie Saunders
You need to format the configuration like this:
It only supports simple names like:
I think you then retrieve the variables from $_ENV. Failing that var_dump() all the superglobals to see where it is.
If you just want the DocumentRoot you can do $_SERVER['DOCUMENT_ROOT'] without adding anything to the Apache configuration. var_dump($_SERVER) to see more of what's inside.
Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 5:44 am
by someberry
Hi ole, everything is fine when setting simple values, i.e.
and then doing
However, I want to make the variables a little bit more clever in adding SERVER variables to generated SERVER variables...
Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 7:44 am
by Ollie Saunders
However, I want to make the variables a little bit more clever in adding SERVER variables to generated SERVER variables...
Please explain this differently.
Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 8:00 am
by Zoxive
ole wrote:However, I want to make the variables a little bit more clever in adding SERVER variables to generated SERVER variables...
Please explain this differently.
I think he means like his example which uses $_SERVER variables. (Uses $_SERVER variables to create different variables)
Which is not possible at all, in the .htaccess file. Because PHP has not been ran yet, so those variables are not available yet. Even if they were, Apache doesn't even know what do to with them, or how to get them.
Possible Solution: Why do you have to do this in the .htaccess file? Why not do it at the beginning of an include?
Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 8:10 am
by someberry
Zoxive wrote:Which is not possible at all, in the .htaccess file. Because PHP has not been ran yet, so those variables are not available yet. Even if they were, Apache doesn't even know what do to with them, or how to get them.
Yup. I was just wondering if there were .htaccess equivalents, since I have seen a few people use things like {HTTP_REFERER} to block spam comments with mod_rewrite.
Zoxive wrote:Possible Solution: Why do you have to do this in the .htaccess file? Why not do it at the beginning of an include?
I am, at the moment. This is what I am using:
Code: Select all
define('P', $_SERVER['DOCUMENT_ROOT'] . $_SERVER['REQUEST_URI']);
It works fine. However, I would like to add it to setenv it so I can use it throughout the site without having to redeclare the constant (At the moment I am declaring it in the index.php and the autoload.php).
Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 8:29 am
by Ollie Saunders
However, I would like to add it to setenv it so I can use it throughout the site without having to redeclare the constant (At the moment I am declaring it in the index.php and the autoload.php).
auto_prepend_file
or possibly mod_write all requests to a single script which then dispatches, like a front controller. That gives you a single point of entry.
Code: Select all
RewriteEngine on# won't rewrite listed extension, alternatively you can wrap the rewrite in a <Directory>RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 8:35 am
by someberry
auto_prepend_file would be perfect.. but not right here )-:
I am making an application as part of a university project. As such people will be installing it in different locations, so auto_prepend_file wouldn't be very good to use.
Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 8:45 am
by Ollie Saunders
You know you can set php configuration options in .htaccess
Code: Select all
php_value auto_prepend_file 'file'
Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 8:46 am
by someberry
ole wrote:You know you can set php configuration options in .htaccess
Code: Select all
php_value auto_prepend_file 'file'
I don't understand

Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 9:26 am
by Ollie Saunders
Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 9:36 am
by someberry
I don't understand how that would solve my problem.

Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 9:40 am
by Ollie Saunders
It means that you are able to alter the configuration of PHP before runtime without touching httpd.conf. I suspected you thought that wasn't possible and was the reason for you rejecting auto_prepend_file as a solution to your problem.
Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 9:45 am
by someberry
ole wrote:It means that you are able to alter the configuration of PHP before runtime without touching httpd.conf. I suspected you thought that wasn't possible and was the reason for you rejecting auto_prepend_file as a solution to your problem.
No - since the project will be available to download I don't want people to need to alter anything to do with config files. The whole point in my project is that it is simple for people new to programming to quickly install and use.
Re: SERVER variables in .htaccess file
Posted: Tue Jan 29, 2008 10:01 am
by Zoxive
someberry wrote:ole wrote:It means that you are able to alter the configuration of PHP before runtime without touching httpd.conf. I suspected you thought that wasn't possible and was the reason for you rejecting auto_prepend_file as a solution to your problem.
No - since the project will be available to download I don't want people to need to alter anything to do with config files. The whole point in my project is that it is simple for people new to programming to quickly install and use.
Exactly. Just like the OP, you wanted to accomplish setting some Server Variables in the .htaccess, which can not be done directly. But using the method ole has been talking about you can Call a file every time php is called.
So all you have to do is simply set the .htaccess to point to one of your files, to be called automatically.
.htaccess
Code: Select all
php_value auto_prepend_file '_autoload.php'
_autoload.php
Code: Select all
<?php
define('P', $_SERVER['DOCUMENT_ROOT'] . $_SERVER['REQUEST_URI']);
The only thing is, this is essentially the same as having an include at the top of every page.