NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Global Variables in PHP

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 02-07-2011, 03:36 PM THREAD STARTER               #1 (permalink)
NamePros Member
 
xarzu's Avatar
Join Date: Apr 2007
Posts: 28
xarzu is an unknown quantity at this point
 



Global Variables in PHP


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.
__________________
There is nothing stronger than human will. Nothing can stop it.
xarzu is offline   Reply With Quote
Old 02-07-2011, 04:46 PM   #2 (permalink)
NamePros Member
Join Date: Nov 2009
Posts: 142
smghost001 will become famous soon enoughsmghost001 will become famous soon enough
 



you can assign $_SESSION variables which is probably the best approach for what you require
__________________
Scientific Calculator - SciCalculator.com

Dns For Sale: Fuelcooler/s(.dot)com - RuralRiding(.dot)com - AttackTank(.dot)com - Foredoomed(.dot)com - SavingCalories(.dot)com - FairDealFinder(.dot)com
smghost001 is offline   Reply With Quote
Old 02-07-2011, 07:16 PM   #3 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease
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.
BillyConnite is offline   Reply With Quote
Old 02-15-2011, 04:15 AM   #4 (permalink)
New Member
Join Date: Feb 2011
Posts: 8
tvstreamscript is an unknown quantity at this point
 



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
tvstreamscript is offline   Reply With Quote
Old 03-07-2011, 06:07 AM   #5 (permalink)
NamePros Regular
 
Zubair1's Avatar
Join Date: Mar 2005
Posts: 921
Zubair1 is a glorious beacon of lightZubair1 is a glorious beacon of lightZubair1 is a glorious beacon of lightZubair1 is a glorious beacon of lightZubair1 is a glorious beacon of light
 


AIDS/HIV
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,
__________________
Live Support : Zubair11 [at] hotmail.com
Free SEO Directory! || Free Online TV || Tech Blog
Web Design & Web Development Services || Reliable Web Hosting
Zubair1 is offline   Reply With Quote
Old 03-09-2011, 08:54 AM   #6 (permalink)
NamePros Regular
 
sourcez's Avatar
Join Date: Nov 2007
Location: UK
Posts: 403
sourcez is a jewel in the roughsourcez is a jewel in the roughsourcez is a jewel in the rough
 



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?
__________________
3cc Internet
sourcez is offline   Reply With Quote
Old 03-22-2011, 08:41 AM   #7 (permalink)
Account Suspended
Join Date: Dec 2008
Location: Boston, Ma
Posts: 650
CrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to all
 



Marrow Donor Program Animal Rescue Autism Autism
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:
????: NamePros.com http://www.namepros.com/programming/701051-global-variables-in-php.html

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.
CrackFeed.Com is offline   Reply With Quote
Old 03-24-2011, 08:42 AM   #8 (permalink)
Account Suspended
Join Date: Dec 2008
Location: Boston, Ma
Posts: 650
CrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to all
 



Marrow Donor Program Animal Rescue Autism Autism
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.
CrackFeed.Com is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining your World: All About Constants Wildhoney CODE 4 10-25-2007 02:16 PM
How To Secure Your PHP Website selim06 Programming 9 08-20-2006 02:57 AM
Looking for PHP Guru to teach me PHP :) - MSN messenger lessons dkr For Sale / Advertising Board 1 06-28-2006 09:02 PM

 
All times are GMT -7. The time now is 02:44 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger