Re: [PHP] Session Variables ~ Best Practices
thanks Jay. -- - Michael Mason Arras People www.arraspeople.co.uk - Jay Blanchard [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] [snip] I am also wondering if I need to declare all my variables one after the other or can I simply declare variables that I will be using immediately upon submission. [/snip] Since PHP is not strongly typed (like C or C++) you need not declare any variables. There are some caveats (see http://www.php.net/error_reporting ). Is it good practice? Not really, especially if you have a shop where more than one developer may be touching the code. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Session Variables ~ Best Practices
I'm right in the middle of developing some pages that will require session cookies. Now I have a few questions and hope I don't bore you too much... I presume I am right in assuming that I can declare variables anywhere I like providing I have started a session on the page but I cannot actually use those variables until a post or some similar action has been performed by the user. I am also wondering if I need to declare all my variables one after the other or can I simply declare variables that I will be using immediately upon submission. -- - Michael Mason Arras People www.arraspeople.co.uk - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Session Variables ~ Best Practices
[snip] I am also wondering if I need to declare all my variables one after the other or can I simply declare variables that I will be using immediately upon submission. [/snip] Since PHP is not strongly typed (like C or C++) you need not declare any variables. There are some caveats (see http://www.php.net/error_reporting ). Is it good practice? Not really, especially if you have a shop where more than one developer may be touching the code. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Session Variables ~ Best Practices
I presume I am right in assuming that I can declare variables anywhere I like providing I have started a session on the page but I cannot actually use those variables until a post or some similar action has been performed by the user. No, you can use them right away - they are stored server-side, so you don't need to wait for a request/response loop before reading them: session_start(); $_SESSION['foo'] = 'bar'; echo $_SESSION['foo']; // 'foo' is available immediately No need to perform any special declarations. It's cookies that need to wait for a subsequent request to be available via getcookie(). Careful with your session variable naming - you may want to implement a primitive namespace, by using named arrays in $_SESSION, such as $_SESSION['auth']['username'] or $_SESSION['prefs']['boxers_or_briefs']. -mike. - michal migurski- contact info and pgp key: sf/cahttp://mike.teczno.com/contact.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Session Variables ~ Best Practices
On Tue, 13 Jul 2004 17:52:44 +0100, Harlequin [EMAIL PROTECTED] wrote: I'm right in the middle of developing some pages that will require session cookies. Now I have a few questions and hope I don't bore you too much... I presume I am right in assuming that I can declare variables anywhere I like providing I have started a session on the page but I cannot actually use those variables until a post or some similar action has been performed by the user. I am also wondering if I need to declare all my variables one after the other or can I simply declare variables that I will be using immediately upon submission. I'm not quite sure what your question is, but here's some info. You have to call session_start() before using anything in the session. This must be called before there is any output to the browser. After the call to session_start() you can get/set/unset any session value you want at any time in the script (all of this is stored on the server). The best way to do all of this is through the $_SESSION superglobal. Use it like a normal assicative array: $_SESSION['var'] = 'value'; or: $_SESSION['array'] = array(1, 2, 3, 4); os: $_SESSION['object'] = new Object(); Everything you put in there will be available from any function without having to use the global keyword or the $GLOBALS superglobal. Note that you can't store recources in the session (at least, they can't be used as resources on subsequent requests.) This includes things such as open file desriptors and database connections or statement handles. -- DB_DataObject_FormBuilder - The database at your fingertips http://pear.php.net/package/DB_DataObject_FormBuilder paperCrane --Justin Patrin-- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php