NameSilo

Global Variables in PHP

Spacemail by SpaceshipSpacemail by Spaceship
Watch

xarzu

Established Member
Impact
0
Is it possible to have true global variables in PHP? The tests I have done so far seem to suggest that you can have a global variable that is global only to the php file that declares it and not to your whole website. Once you go to another php file, the variable seems to loose what you assign it.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
you can assign $_SESSION variables which is probably the best approach for what you require
 
0
•••
xarzu,

You can have a global variable across files, that is, if you are including those files.

If you're using those variables within a function, you'll need to declare them as global within the function also.
Code:
global variableA,variableB,variableC
That might be the issue you were having, not sure.

Rhett.
 
0
•••
i'm not sure i understand you correctly but there are a couple of site-wide globals available in php for example $_SESSION or $_COOKIE

in order to use $_SESSION you must do session_start(); at the top of each php file
 
0
•••
You can assign a variable in a file (for example global.php) and include that file where ever you need access to those vars.

If you don't want to do that, then you have to use either $_SESSION or $_COOKIE.

I would use the first method to assign the variable in the global.php file and include it where ever needed.

But yeah, obviously what you're saying isn't possible to declare a $var and have it available globally from any other file that's what (SESSIONS/COOKIES) are for :)

Regards,
 
0
•••
As suggested, sessions and cookies are the only solution for persistant variables (which of course can be lost etc if the browser is closed etc)

Otherwise why not store them in a database or file cache on the server? What sort of things are you trying to make global?
 
0
•••
I would go more for sessions, they are more secure. If it is sensitive material you can even store the data in a text file and name the file something like data.protested.php and then in .htaccess do this to prevent access to that file. Then when you need the data, just open the file and get it. But most likely sessions are the optimal practice.

This allows you to specify multiple files to protect:

Code:
<FilesMatch "^(cache|protected)\.txt$">
	Order deny,allow
	deny from all
	allow from localhost 127.0.0.1 50.22.145.130 50.22.145.131 50.22.145.190 50.22.145.191
</FilesMatch>

This allows you to protect one sort of file:

Code:
<Files .htaccess>
     order allow,deny
     deny from all
</Files>

You can always have multiple files like this:

usernames.protected.txt
emails.protected.txt
and so on...


I hope this helps you in some way.
 
0
•••
If it is something in your configuration, you can always pack everything into a neat array and use Global $var; in the functions it is needed in too.

Also you can get into objects. I love them, but in moderation.
 
0
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back