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.

8
  • Are you running the index.php script from the command line or from an http request? Commented May 11, 2015 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. Commented May 11, 2015 at 17:02
  • are you running mod_env? stackoverflow.com/a/2008355/773522 Commented May 11, 2015 at 17:02
  • @MarcB, that's exactly why I asked... Commented May 11, 2015 at 17:03
  • 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. Commented May 11, 2015 at 17:19

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.