VaulX Posted July 6, 2010 Share Posted July 6, 2010 I've read different methods of storing variables, but I have one question. How do you store a variable in memory to be used on any web page the user views? Do you use the "define" method for constants? Quote Link to comment https://forums.phpfreaks.com/topic/206837-site-wide-variables/ Share on other sites More sharing options...
premiso Posted July 6, 2010 Share Posted July 6, 2010 You would use sessions. But this will only work for the current website (ie domain.com) and any pages that have the session_start at the top. But that is what you are probably wanting to look into. Quote Link to comment https://forums.phpfreaks.com/topic/206837-site-wide-variables/#findComment-1081698 Share on other sites More sharing options...
VaulX Posted July 6, 2010 Author Share Posted July 6, 2010 Ahh... Ok, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/206837-site-wide-variables/#findComment-1081723 Share on other sites More sharing options...
VaulX Posted July 7, 2010 Author Share Posted July 7, 2010 One more question regarding sessions... Once a session variable is set, can it be changed during the same session? Quote Link to comment https://forums.phpfreaks.com/topic/206837-site-wide-variables/#findComment-1082248 Share on other sites More sharing options...
premiso Posted July 7, 2010 Share Posted July 7, 2010 Yes, it can. Quote Link to comment https://forums.phpfreaks.com/topic/206837-site-wide-variables/#findComment-1082250 Share on other sites More sharing options...
VaulX Posted July 7, 2010 Author Share Posted July 7, 2010 Ok... So what's the difference between the "define" function and sessions? Or maybe I should ask what's the difference between assigning a variable ($a) and defining a constant with "define"? Quote Link to comment https://forums.phpfreaks.com/topic/206837-site-wide-variables/#findComment-1082700 Share on other sites More sharing options...
Andy-H Posted July 7, 2010 Share Posted July 7, 2010 I think you are misunderstanding the meaning of constant. It's not constant throughout your whole application, it's constant during the runtime of the script in which it is define'd. A constant is a set value, and if you try to change the value of a constant that has already been set you will get an error at runtime. They are commonly used to store values that never or rarely change. e.g. define('_VAT_', 0.175); $cost = 10; $cost_with_vat = $cost + ($cost * _VAT_); echo 'Cost excluding VAT: £' . number_format($cost, 2) . '<br >'; echo 'Cost including VAT: £' . number_format($cost_with_vat, 2); The value of VAT changes rarely, but you can still change it, as it has been changed recently: define('_VAT_', 0.2); $cost = 10; $cost_with_vat = $cost + ($cost * _VAT_); echo 'Cost excluding VAT: £' . number_format($cost, 2) . '<br >'; echo 'Cost including VAT: £' . number_format($cost_with_vat, 2); --------------------- A session is a value which is stored on the server, and a session id stored in a cookie on the client's machine is sent to the server in each HTTP request; to identify weather they have a session stored on the server (and the location of the file that it's stored in, if they do)and this array of values (specific to each client), can be retrieved through out the sessions lifetime, so long as the code has session_start before any header's, including output, are sent. page1.php session_start(); $_SESSION['username'] = 'Andy-H'; page2.php session_start(); echo $_SESSION['username']; //String - 'Andy-H' Quote Link to comment https://forums.phpfreaks.com/topic/206837-site-wide-variables/#findComment-1082708 Share on other sites More sharing options...
VaulX Posted July 8, 2010 Author Share Posted July 8, 2010 Thanks, Andy... I managed to get my code working using sessions. What I've created is a language selector, either English or Vietnamese. Depending on which language the user selects it stores a session variable for it and then calls a PHP file where I have defined all my constants. I originally just assigned variables ( $user = "Vaulx"; ), but that didn't seem to work with another PHP script I downloaded to use as a form mailer. I had to "define" all of my variables/constants. This part I haven't figured, but I'm getting there. The following is the small piece of code I created to do three things: 1 - Checks if user selected the language button. 2 - Determines which language to use. 3 - Sets default language to English if first time visit. <?php // Check if user selected a language button and assign session variable. if ($_GET[lan] == 'vn') { $_SESSION['lan'] = 'vn'; } elseif ($_GET[lan] == 'en') { $_SESSION['lan'] = 'en'; } elseif (!isset($_SESSION['lan'])) { $_SESSION['lan'] = 'en'; } // Include the appropriate language file depending upon session variable set in previous step. if ($_SESSION['lan'] == 'vn') { include ('/.../language/vietnamese.php'); } elseif ($_SESSION['lan'] == 'en') { include ('/.../language/english.php'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/206837-site-wide-variables/#findComment-1082841 Share on other sites More sharing options...
VaulX Posted July 8, 2010 Author Share Posted July 8, 2010 By the way, I don't have session_start() in this code and it still works. I think it's because this script is being called from another script where session_start() already exists. Quote Link to comment https://forums.phpfreaks.com/topic/206837-site-wide-variables/#findComment-1082842 Share on other sites More sharing options...
Andy-H Posted July 8, 2010 Share Posted July 8, 2010 By the way, I don't have session_start() in this code and it still works. I think it's because this script is being called from another script where session_start() already exists. Indeed it is Also, super-global arrays ($_POST, $_GET, $_SESSION, etc.) should have their keys quoted as strings, ie $_GET[lan]; // look at the syntax highlighting, lan is treated as a constant. $_GET['lan']; // correctly quoted and treated as a string. Quote Link to comment https://forums.phpfreaks.com/topic/206837-site-wide-variables/#findComment-1083118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.