get private user id from a session

0

I want to get an id from a user that is recorded in the session, I'm trying to serialize the object when it's created:

$_SESSION['usuario'] = serialize($objUsuario);

and to recover tried to use:

echo unserialize($_SESSION['usuario'])->getIduser();

return error:

  

Fatal error: main (): The script tried to execute a method or access to property of an incomplete object. Please ensure that the class definition "pojoUsuario" of the object you are trying to operate on is loaded before unserialize () gets called or provide an autoloader to load the class definition in

If I give a var_dump($_SESSION['usuario']); die();

returns:

add-data.php:4:string 'O:11:"pojoUsuario":8:{s:19:"�pojoUsuario�iduser";s:1:"1";s:21:"�pojoUsuario�username";s:8:"nanous";s:18:"�pojoUsuario�senha";s:32:"1f32aa4c9a1d2readssdsdsdsddds6a04";s:18:"�pojoUsuario�email";s:20:"[email protected]";s:25:"�pojoUsuario�frasesecreta";s:14:"dogs";s:21:"�pojoUsuario�resposta";s:4:"S#hu";s:17:"�pojoUsuario�flag";s:1:"1";s:22:"�pojoUsuario�datasenha";s:10:"2018-07-24";}' (length=398)

I need to get the data stored in the session, such as username and iduser and I'm not getting it.

    
asked by anonymous 02.10.2018 / 17:32

1 answer

3

In the file you are giving unserialize($_SESSION['usuario']) you must have the definition of class pojoUsuario as well.

One tip is to include the file that defines this class, for example:

require_once 'PojoUsuario.php'; // inclui definição da classe

session_start();

echo unserialize($_SESSION['usuario'])->getIduser();

More information and tips on PHP documentation here

    
02.10.2018 / 19:01