0

I Have the following situation where I need add several values from $usuario to $_SESSION. Here's one way to do this however is cumbersome as $user has too many fields.

$_SESSION['logado_ecse']    = true; 
$_SESSION['user']           = $usuario['login'];
$_SESSION['usuario_id']     = $usuario['id'];
$_SESSION['habilitada']     = $usuario['habilitada'];
$_SESSION['bloqueada']      = $usuario['desbloqueada'];
$_SESSION['administrador']  = $usuario['administrador'];
                ....
$_SESSION['auditor']        = $usuario['auditor'];

I was looking for a more elegant solution where in just one line I could do the same as for example something like this, but this solution doesn't work as expected.

$_SESSION = array_merge($usuario, $_SESSION);

The variable $usuario is as follows

echo gettype($usuario);
array

var_dump($usuario);
array (size=34)
  'id' => string '1' (length=1)
  0 => string '1' (length=1)
  'login' => string 'admin' (length=5)
  1 => string 'admin' (length=5)
  'auditor' => string '0' (length=1)
  2 => string '0' (length=1)

              ...
  'telefone2' => string '11' (length=2)
  14 => string '11' (length=2)

I like @KV's solution, I think I'm gonna implement it as a function. So far, my idea is that $_SESSION is a container to "the user", so a solution such as

 $_SESSION['USER']=$usuario

will fragment the user's info in two

 $_SESSION['some_name']
 $_SESSION['USER']['some_name']

, not to mention that I'll have to reformat other parts of the code.

1
  • 3
    why not just $_SESSION['USER']=$usuario; Commented Dec 9, 2015 at 19:27

4 Answers 4

3

You can push $usuario array onto $_SESSION array like this:

$_SESSION['user_details'] = $usuario;

And after that you can retrieve user details like this:

echo $_SESSION['user_details']['login'];
echo $_SESSION['user_details']['id'];
echo $_SESSION['user_details']['habilitada'];
...
echo $_SESSION['user_details']['auditor'];
Sign up to request clarification or add additional context in comments.

Comments

1
foreach($usuario as $k => $user) if(is_string($k)) $_SESSION[$k] = $user;

2 Comments

It's usually a good idea to add a description of what the code does instead of just providing section of code.
@XO Well, the code does what the OP is asking. One line of code to set the session vars.
0
<?php
$_SESSION['user'] => Array(
'userid'=> '123',
'username'=> 'some_joe',
'role' => 'customer',
'website' => 'http://www.example.com'
);

// reading values from array
$userid = $_SESSION['user']['userid'];
$username = $_SESSION['user']['username'];
// etc. etc.

// adding values to array
$_SESSION['user']['something'] = "Foo";
$_SESSION['user']['somethingelse'] = $bar;

// Adding via function
Set_Session('user', array('var'=> 'value'));

// determine if session has started
Function HasSessionStarted() {

    $result = false; // default to false

    // Check if session has started
    IF ((session_status() == PHP_SESSION_NONE) || (session_id() == '')) { 
        $result = true; 
    }

    return $result;

}

// Set a session value
Function Set_Session($name, $value) {
    /* @params value: can be a string or an array */
    $new_session = HasSessionStarted(); // Check Session Status

    IF ((isset($name)) && (isset($value))) {
        IF ($new_session) { session_start(); }
        $_SESSION[$name] = $value;
        IF ($new_session) { session_write_close(); }
    }

}

Function Unset_Session($name) {

    $new_session = HasSessionStarted(); // Check Session Status

    IF (isset($_SESSION[$name])) {
        IF ($new_session) { session_start(); }
        unset($_SESSION[$name]);
        IF ($new_session) { session_write_close(); }
    }

}
?>

Comments

0

just assign $usuario to session variable:

$_SESSION['user'] = $usuario;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.