8

I need to add a variable to the $_SERVER array in a php script. Is there a possibility to do this via the php.ini file? I want to add the variable for all scripts on the webserver, so it's quite inconvenient to add it in each script.

Thanks, TSS

TSS
  • 139
  • 1
  • 1
  • 6

3 Answers3

9

You can set it in your .htaccess or in the apache configuration

<VirtualHost *:80>
    ...
    SetEnv PARAM "VALUE"
    ...
</VirtualHost>

It will be added to the variable $_SERVER['PARAM'].

freddy
  • 159
  • 1
  • 4
  • 23
cochet
  • 91
  • 1
  • 3
3

If you're using Apache, try the SetEnv module.

(you can see more here: Declaring global variable with php.ini)

Community
  • 1
  • 1
jlb
  • 19,090
  • 8
  • 34
  • 65
2

I can't think of a way to add things to $_SERVER via php.ini (which doesn't mean there's no way to do it).

However, you could add things to $_ENV, server-wide, using SetEnv in httpd.conf (assuming apache, here). There are likely methods for doing this with other web servers, but I'm not sure what they are.

timdev
  • 61,857
  • 6
  • 82
  • 92
  • 1
    The problem is, I really need to set it in the $_SERVER array – TSS Jul 13 '11 at 08:49
  • One of the good/bad things about php is that superglobals (like $_SERVER) are writable. Meaning, you can add new keys to the superglobal by just calling `$_SERVER['your_key'] = 'hello world';` – ILikeTacos May 04 '16 at 21:02
  • @AlanChavez - you're right, but OP was specifically asking about setting a variable globally across the server. – timdev May 04 '16 at 22:39