Jump to content

Usual variables vs Global


j.smith1981

Recommended Posts

Hi there,

 

I am a highly concerned about ever using global variables (as many guides I have seen on security issues with PHP say you should avoid them, kind of understand why though to be fair), but I was wanting to really understand custom based session handlers in PHP and this first one here: http://uk.php.net/manual/en/function.session-set-save-handler.php on the example: 'Example #1 session_set_save_handler() example' uses them.

 

Now would it be better to take the global variable in function: ''open()' and then return the variable $sess_save_path then pass it up to the next function that may need it perhaps?

 

I just don't want to use globals if I can avoid it, been working out a system that work use for some old solution we use and it uses globals and it really bugs the living life out of me, why people are so lazy they can't avoid them at all costs grrr.

 

Any advice generally speaking is of great appreciation,

Jeremy.

Link to comment
Share on other sites

Assuming that you actually need to use a custom session save handler (most people don't), you would use a class so that the variables that are specific to those functions that must be shared between the functions can do so with out needing to jump through any hoops.

Link to comment
Share on other sites

The reason why 'global' is used there is because the specific open, close, etc. functions associated with the session_save_handler function expect a certain number of incoming parameters for each of those functions (e.g., open() anticipates only two parameters, etc.).

 

Seems dumb that those functions don't allow optional arguments for the session path and whatnot.  I'm curious as to whether or not these functions would be considered to be user defined or not, since the user is the one writing their definitions.  If they are user defined, then they could have variable length argument lists, which would allow the use of func_get_args, and eliminate the 'global' problem.

Link to comment
Share on other sites

But would not you then set save path and all that stuff outside of the function, as it's only really a value and then pass that to the other functions when needed?

 

Just trying to get to the bottom of why this precisely needs a global var or is that out of this question, I mean does it demand a global variable? (doubtful in my head since most discourage this use of a global variable), I kind of get why though which is why I like to ask.

 

Hmm interesting though, I mean I know most don't need them but I am just interested to see what I can do with them, then maybe use them when I need to etc you know? Just to say I can do them then blog about how to make them work etc.

Link to comment
Share on other sites

A) The example code you found is a bad one to pick to try and decide if you should use the global keyword (you should not). For the specific example code, now you have got to remember to never use the a variable named $sess_save_path in all the code you write for all the programmers working on your project, now and forever in the future, when new programmers come on board. The global keyword simply creates more work and problems.

 

B) The parameters that those specific functions receive come from php when it calls those functions as part of the session handling. The definition of the parameters for each function is determined by the underlying C code. The specific save_path parameter being discussed has (likely) been processed by php to resolve things like the path (the system tmp folder for the current operating system, a php/user defined folder, or which folder in the optional random multiple-folder set.) The save_path parameter is something that php provides and that you need to use for any statement that accesses a session data file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.