0

I want a variable to be superglobal, but as am using procedural style I don't think I can make one of my own, so basically the question is that am using a query to retrieve all security control of my website from security table, am checking whether maintenance mode is on/off, if it's on am redirecting it to website under maintenance page, so on each page I need to check the status of variable $maintenance_status, for doing this, i need to call that query on each page, or else am getting an error that undefined variable, moreover if am making a function and including that function file in other pages, it is showing me that $db_connect(which is my db connection variable) is undefined, am including my pages in this sequence

include_once('connection.php');
include_once('functions.php');
 /*other scripts goes here*/

Any idea how to pull this status on each page? I thought to make a new file for common queries but is ait a clean solution? moreover I guess am not understanding includes, if I included connection.php before functions.php than why my functions.php is showing undefined variable $db_connect?

Random Guy
  • 2,878
  • 5
  • 20
  • 32

2 Answers2

1

You can use a constant for that by using define(). Defines can be set once per script execution and can not be changed during one script execution. They are superglobal - also across files which are being included.

See http://php.net/define or just

define('MY_CONSTANT', 'whatever');
define('MY_OTHER_CONSTANT', false);

function foo() {
    if (MY_OTHER_CONSTANT !== true) {
        echo MY_CONSTANT;
    }
}

foo();
Jan.
  • 1,925
  • 15
  • 17
  • well I would define constant, but if I define it on a functions page, I am getting that my file is not connected with my db, I've specified the includes, and if I define it in a page I cant include it to other, the only thing I can do here is to define it it my connections file – Random Guy Sep 06 '12 at 16:04
  • Thanks for your answer, but I needed to make mmy db_connect as a global variable, now I am simply calling function on each page, I can only mark 1 answer as imp, though I'll upvote your's :) – Random Guy Sep 06 '12 at 16:13
  • Actually you need a static singleton class for your db. :) – Jan. Sep 06 '12 at 16:18
0

Without more code context on where within your files you are getting the errors, it will be hard to provide any advice. For example, is your reference to $db_connect done from inside a function? If it is, than it will not work unless you have a global $db_connect declaration within that function (to use the $db_connect in the global scope rather than the undefined $db_connect in the function's scope).

While I don't prefer using such global declarations within functions for a number of reasons (I would rather use dependency injection, or get the DB connection via a static singleton function call), that is probably a lesson for another time.

You might be best served anyway to make your query in some sort of init script (like after your connection.php inlcude) and define a constant regarding whether maintenance mode if on or off. Something like this

// assuming you have already made DB query and have a value of true/false on a variable called $is_maint_mode
define('MAINT_MODE', $is_maint_mode);

This would give you a constant MAINT_MODE that is globally available to your code.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • bingo, I defined $db_connection as global and it works, and btw you recommend that I should not use global but here, my connection variable is global, i guess that's pretty ok? anyways I'll be changing my db_connection to a constant soon – Random Guy Sep 06 '12 at 16:08
  • actually as I said I cant use constant because if I use constant than I can call a query either on connection or my function page as these are the only two pages I am including anyways I made a function, called a query in that funtion, made my database connection variable global, and am returning the vale and comparing it with a string using `if(maintenance_status() == off) {//then do something}` thanks any way, both answers were helpful, but using the word global solves my function error.. – Random Guy Sep 06 '12 at 16:12
  • @V413HAV DB connection handles can't be stored in a constant. You can however store thing like your maintenance mode on/off values in constants. I was merely stating that I generally don't like using `global $variable` type declarations in functions. I would typically use dependency injection (passing variable to function as a parameter), or use a DB connection singleton and just call a static method like `db::connect()` from within the function to get a DB connection handle within the function. But since you said you are mainly doing procedural that last solution is probably not for you. – Mike Brant Sep 06 '12 at 16:14