Jump to content

[SOLVED] Loop through array and create variable session variable names


Recommended Posts

Hello guys & girls,

                      As part of my user authentication class I've written a method that sets session variables. I pass an array to the function by reference which contains elements populated with information from the users table in the DB (name, street, country etc)

 

At the moment the function looks like this:

 

/* Set the session variables for easy access to user data */
function _setSessionData(&$userData) { 
$_SESSION['loggedOn'] = true; // Logged on

		$_SESSION['userID'] = $userData['UserID'];
		$_SESSION['username'] = $userData['UserName'];
		$_SESSION['password'] = $userData['Password'];
		$_SESSION['firstName'] = $userData['FirstName'];
		$_SESSION['surname'] = $userData['Surname'];
		$_SESSION['email'] = $userData['Email'];
		$_SESSION['street'] = $userData['Street'];
		$_SESSION['city'] = $userData['City'];

		return true;
	}

 

What I'm wondering is if it is possible to loop through the $userData array, and dynamically name and populate the session variables rather than listing them all and assigning like I have?

 

Cheers,

Mark

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.