Skip to content
Advertisement

How to create a $_SESSION from an array index?

I am doing a login system and I want to show a welcome message when the user is validated. I explain.

This is the database:

enter image description here

The user when is logging in uses the username but I want in the welcome message to show it’s firstname.

What I have is a function that selects all the information of the username that is logging in.

LoginManager.php

JavaScript

Then in LoginController.php I obtain the $_POST information, I call the function to show database information and also I validate the user with LDAP.

LoginController.php

JavaScript

When I do a var_dumb($dds) I obtaing that:

enter image description here

Also in ProfessorController.php I have to pass the $_SESSION so I can obtain the firstname in professor.html

JavaScript

So how can I create a $_SESSION with the firstname in order to show it in my views?

Advertisement

Answer

Your session data contains the username. You need to create a session with the firstname. The function obtenirDades() returns multiple rows because you’re using the fetchAll() method. Since it is a login function, use the fetch() method, which returns a single key/value array. Then to store in a session you would do the following

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement