| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| NamePros Member Join Date: Apr 2007
Posts: 28
![]() | 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. |
| | |
| | #2 (permalink) |
| NamePros Member Join Date: Nov 2009
Posts: 142
![]() ![]() | 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 |
| | |
| | #3 (permalink) |
![]() Join Date: Jul 2005 Location: Coffs H, Australia
Posts: 3,456
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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 Rhett.
__________________ Free Forums / GoDaddy Coupon Codes (NEW DOMAIN!) / Free Arcade Script / <?='Your computer is '.(1?fine:broken).'.'?> |
| | |
| | #5 (permalink) |
| NamePros Regular Join Date: Mar 2005
Posts: 921
![]() ![]() ![]() ![]() ![]() | 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 |
| | |
| | #6 (permalink) |
| NamePros Regular Join Date: Nov 2007 Location: UK
Posts: 403
![]() ![]() ![]() | 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 |
| | |
| | #7 (permalink) |
| Account Suspended Join Date: Dec 2008 Location: Boston, Ma
Posts: 650
![]() ![]() ![]() ![]() ![]() ![]() | 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> ????: NamePros.com http://www.namepros.com/programming/701051-global-variables-in-php.html Code:
<Files .htaccess>
order allow,deny
deny from all
</Files> usernames.protected.txt emails.protected.txt and so on... I hope this helps you in some way. |
| | |
| | #8 (permalink) |
| Account Suspended Join Date: Dec 2008 Location: Boston, Ma
Posts: 650
![]() ![]() ![]() ![]() ![]() ![]() | 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. |
| | |
![]() |
| 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 |