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:
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
public function obtenirDades($usuari)
    {
        //Obtenim les dades de la base de dades
        $sql = " SELECT * FROM erp_user WHERE username = '$usuari'";
        $cons = (BdD::$connection)->prepare($sql);
        $ok = $cons->execute();
        $cons->setFetchMode(PDO::FETCH_ASSOC);
        $resultat = $cons->fetchAll();
        /*var_dump($resultat);
        die();*/
        return $resultat;
    }
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
<?php
if (!isset($_SESSION))
{
    session_start();
}
class LoginController extends Controller
{
    public function process($params)
    {
        //EN EL LOGIN VALIDEM L'USUARI I CONTRASENYA
        if (isset($_POST["login"])) {
            /*HERE WE INSTANTIATE LOGINMANAGER SO WE CAN USE IT'S FUNCTIONS*/
            $manager = new LoginManager();
            /*WE OBTAIN THE POST DATA*/
            $usuari = $_POST["usuari"];
            $password = $_POST["password"];
            //HERE WE USE THE FUNCTION TO OBTAIN INFORMATION FROM THE DATABASE
            $dds = $manager->obtenirDades($usuari); `WE`
            @$this->data["erp_users"] = $dds[0]["firstname"];
            if($manager->valida($usuari, $password)){
                //validació de ldap i redirecció si es professor o alumne
                if ($manager->valida($usuari, $password) && $dds[0]["type"] == "PROFESSORS") {
                    $_SESSION["username"] = $usuari;
                    var_dump($dds);
                    die();
                    header("Location: professor");
                } elseif ($manager->valida($usuari, $password) && $dds[0]["type"] == "ALUMNES") {
                    $_SESSION["username"] = $usuari;
                    header("Location: alumne");
                } else {
                    header("Location: login");
                }
            }else{
                echo "<div class='error'>USUARI i/o CONTRASENYA INCORRECTES! </div>";
            }
        }
        //Destrium la session
        if (isset($_POST['logout'])) {
            session_destroy();
            unset($_SESSION['username']);
            header('location:login');
        }
        $this->getLogin();
    }
    //Funció que ens retorna la vista de login
    public function getLogin()
    {
        $this->twig = "login.html";
    }
}
When I do a var_dumb($dds) I obtaing that:
Also in ProfessorController.php I have to pass the $_SESSION so I can obtain the firstname in professor.html
<?php
if (!isset($_SESSION)) {
    session_start();
}
class ProfessorController extends Controller{
    public function process($params)
    {
        /*var_dump($params);
        die();*/
        if(empty($params[0])){
            /*echo $_SESSION["username"];*/
            $this->getProfessor();
        }elseif(isset($params[0]) && $params[0] == "crearNivell"){
            $this->twig = "crearNivell.html";
        }
    }
    public function getProfessor(){
        $this->twig = "professor.html";
    }
}
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
$_SESSION["firstname"] = $dds['firstname'];

