Jump to content

Session_start() Questions


wommbatt
 Share

Recommended Posts

annnd im back again for another round of, "I've missed something."i love this game lolbeen searchin about for a while and im not understanding one of the session functions1. the session_start() i get and it works just fine.2. is it necessary to have isset functions for the variables and how do the variables work, thats my main issue currently3. i get the $_SESSION['var']="value" line but how do i set variables from form data? it seems to me that i have to set the value for it in the script and then call back to it later for echoes or what have u which isnt what im looking to do, can i even set the form data in this fashion?i tried replacing $_POST['value'] w/ $_SESSION['value'] but that didnt yield any results.

Link to comment
Share on other sites

session_start() fills up the $_SESSION array with the session data from earlier, or just starts a new session if one is not already started i.e. it doesn't fill up $_SESSION with anything, but it makes it so that when the script ends, the next time $_SESSION will contain its latest data.You add values to $_SESSION the same way you'd add values to any array. Now, ask yourself, how do you add or replace values to arrays normally (see W3Schools' section on arrays if you can't answer that)? Here's a hint:

$myArray = array('firstKey'=>'firstValue', 'secondKey'=>'secondValue');$myArray['thirdKey'] = 'thirdValue';$myArray['firstKey'] = $_POST['value'];

Link to comment
Share on other sites

$_SESSION is just an array that persists between scripts. Form data is either stored in the $_GET or $_POST arrays (depending on the method used), and the values from said array can then be assigned to the session array, e.g. $_SESSION['test'] = $_POST['test_field'];. The $_GET and $_POST arrays only exist for the page the form submits to, but the $_SESSION array is available for any script after session_start() is called.

Link to comment
Share on other sites

I use isset() regularly with $_SESSION elements. Usually all I want to know is if a user's computer is associated with a $_SESSION element that was set in an earlier script. I don't generally care what the value is, as long as it is set. So this:if (isset($_SESSION['user']) ) { . . .usually tells me all I need to know. The function returns true if the user has passed through my login system. The function returns false if he hasn't, or if the session has expired.

Edited by Deirdre's Dad
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...