1

I'm trying to print my environment variable defined in .htaccess from PHP script in shell.

My tree is:
/.htaccess (in root folder)
/test/index.php (in "test" folder)

I set my variable in .htaccess with SetEnv:

SetEnv HTTP_TEST "testing"

My PHP script "/test/index.php" is:

#!/usr/bin/php
<?php
echo $_ENV['HTTP_TEST']; // print empty
echo $_SERVER['HTTP_TEST']; // print empty
echo getenv('HTTP_TEST'); // print empty

But if I access my PHP script from my browser there is no problems (without #!/usr/bin/php of course...)

Thank you for any help.

Vadzym
  • 148
  • 10
  • Are you running the index.php script from the command line or from an http request? – Adon May 11 '15 at 17:01
  • 1
    not possible from the command line. since apache isn't involved at all there, nothing you do in .conf/.htaccess will have any effect. it's like wondering why you're getting rained on while standing outside, because you don't get rained on while you're inside your house. – Marc B May 11 '15 at 17:02
  • are you running mod_env? http://stackoverflow.com/a/2008355/773522 – fbas May 11 '15 at 17:02
  • @MarcB, that's exactly why I asked... – Adon May 11 '15 at 17:03
  • Yes. I'm running my PHP script from shell: # php index.php – Vadzym May 11 '15 at 17:04
  • basically, no. you'd have to write/find something that will parse through your apache setup and recreate everything done in the startup phase, which means you might as well just run this script via an http request, since you'd almost have replicated most of apache at that point. – Marc B May 11 '15 at 17:05
  • It's a cron script. I can't run it in browser. – Vadzym May 11 '15 at 17:18
  • 2
    You can run a cron script and simulate 'running from the browser'. Basically you can use the `wget` command to call an http request. This would solve your problem. – Adon May 11 '15 at 17:19

1 Answers1

2

I built a small PHP script that parse the .htaccess file to find the SetEnv

#!/usr/bin/php
<?php

// Read all lines from file
$htaccess = file('/.htaccess');

foreach ($htaccess as $line) {
    // Trim left/right spaces, replace all repeated spaces by 1 space
    $line = preg_replace('/[ \t]+/', ' ', trim($line));

    // It's our variable defined with SetEnv
    if (substr($line, 0, 7) === 'SetEnv ') {
        $tmp = explode(' ', substr($line, 7), 2);
        $ENV[$tmp[0]] = str_replace('"', '', $tmp[1]);
    }
}

print_r($ENV);
Vadzym
  • 148
  • 10