SERVER variables in .htaccess file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

SERVER variables in .htaccess file

Post 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:

Code: Select all

echo $_SERVER['ROOT']
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: SERVER variables in .htaccess file

Post by Ollie Saunders »

You need to format the configuration like this:

Code: Select all

SetEnv name value
It only supports simple names like:

Code: Select all

SetEnv foo bar
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.
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: SERVER variables in .htaccess file

Post by someberry »

Hi ole, everything is fine when setting simple values, i.e.

Code: Select all

SetEnv FOO "bar"
and then doing

Code: Select all

echo $_SERVER['FOO'];
However, I want to make the variables a little bit more clever in adding SERVER variables to generated SERVER variables...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: SERVER variables in .htaccess file

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: SERVER variables in .htaccess file

Post 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?
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: SERVER variables in .htaccess file

Post 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).
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: SERVER variables in .htaccess file

Post 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
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: SERVER variables in .htaccess file

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: SERVER variables in .htaccess file

Post by Ollie Saunders »

You know you can set php configuration options in .htaccess

Code: Select all

php_value auto_prepend_file 'file'
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: SERVER variables in .htaccess file

Post 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 :?:
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: SERVER variables in .htaccess file

Post by Ollie Saunders »

someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: SERVER variables in .htaccess file

Post by someberry »

I don't understand how that would solve my problem. :?:
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: SERVER variables in .htaccess file

Post 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.
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: SERVER variables in .htaccess file

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: SERVER variables in .htaccess file

Post 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.
Post Reply