0

I am having a bit of trouble setting and using environment variables in PHP.

I have a php file as follows

http://localhost/site/include.php

<?php
    define("ENV_LOCATION", "http://localhost/site/resources/addition");
?>

Which I am including in the following file

http://localhost/site/home.php

<html>

<?php
    include("include.php");
?>

<body>
    <?php

        // this bit works
        echo ENV_LOCATION;

        // this bit works
        include(ENV_LOCATION . "/test.php");

    ?>
</body>

</html>

but then it seems to lose the environment variable when I try to use it in the last file I am including

http://localhost/site/resources/addition/test.php

<?php
    // this fails
    echo ENV_LOCATION;
?>

Why is this file unable to see the environment variable? I am getting the following error

Notice: Use of undefined constant ENV_LOCATION - assumed 'ENV_LOCATION' in /var/www/html/site/resources/addition/test.php on line 3
ENV_LOCATION
TheLovelySausage
  • 3,838
  • 15
  • 56
  • 106
  • define('ENV_LOCATION', "http://localhost/site/resources/addition"); can you use this – Md Hasibur Rahaman Nov 08 '16 at 09:29
  • 2
    `define` doesn't set environment variables, it defines a constant, and constants don't get persisted across requests, they need to be defined every time. Are you sure you're not looking to be using sessions? – Jonnix Nov 08 '16 at 09:31
  • @JonStirling What do you mean requests? He's *including* files, not requesting. – Rax Weber Nov 08 '16 at 09:35
  • @RaxWeber OP is `include`ing a URL. – Jonnix Nov 08 '16 at 09:38
  • So, the answer appears to be, stop using includes on URLs and use their proper file system paths. – Jonnix Nov 08 '16 at 09:40
  • This looks like an XY problem: you want to do something, you figured out `define()` is the solution and you're asking about `define()`. You'll only get useful answers if you explain your original goal. – Álvaro González Nov 08 '16 at 09:43
  • @JonStirling Yes, he's including a URL. Well, then that makes it a request itself. :) – Rax Weber Nov 08 '16 at 09:43

2 Answers2

2

define doesn't define environmental variable. It defines constant and the constant variable should be available in current scope to use. for example where it says failed the file where constant defined not included.

Btw, for environment variable php has putenv function. Reference http://php.net/manual/en/function.putenv.php

  • I've tried to use the putenv as you suggested and there are no errors put when I execute echo getenv("ENV_ELEMENTS") from the test.php file it doesn't echo anything – TheLovelySausage Nov 08 '16 at 10:16
  • I mentioned putenv function for your reference if you really want to set environment variable instead of constant. putenv will set environment variable only for current request. Ensure in your current request putenv loaded. Paste your whole code where you are calling envrioment/constant – Muhammed Imran Hussain Nov 08 '16 at 10:20
  • Is it not possible to set the environment variable globally? Without using session variables – TheLovelySausage Nov 08 '16 at 10:22
  • Globally means? if you paste your code where env variable declared and where called that will be great for me to troubleshoot – Muhammed Imran Hussain Nov 08 '16 at 10:26
  • The same way an environment variable works in console development languages. Like set an environment variable on the operating system and it is accessible to any module of an application – TheLovelySausage Nov 08 '16 at 10:28
  • The same way a session variable works but I don't have sessions at the moment – TheLovelySausage Nov 08 '16 at 10:30
  • Yes you can but in different way. Hope this post will be helpful for you http://stackoverflow.com/questions/5052558/declaring-global-variable-with-php-ini Let me know if you couldn't figure it out – Muhammed Imran Hussain Nov 08 '16 at 10:32
1

What you're doing is:

include "http://localhost/site/resources/addition/test.php";

This includes the file over HTTP. That means a new HTTP request to your server will be made (your server making a request to itself), and that new request won't have any context from the other request. It's like any other independent HTTP request coming into your web server, nothing is shared between them.

It's

  1. madness to include files over HTTP and
  2. hopefully obvious why it doesn't work as you expect.

You need to include the file locally, not from a URL:

include "resources/addition/test.php";

Probably you want to define your constant as a file path, not a URL:

define("ENV_LOCATION", ___DIR___);
deceze
  • 510,633
  • 85
  • 743
  • 889