user3672700
user3672700

Reputation: 162

Why is my array of arrays not being called when the $_SESSION is changed to TRUE?

I'm new to PHP and I can't understand why is my code not working as intended. I have two functions on a class controller, which one of them is supposed to be called when the user is logged out and one of them when the user is logged in. But what happens is the function is never called and I have no idea why. Only the function when the user is logged out is being called.

//index.php

    <!DOCTYPE html>

    <?php

        require_once 'Control/Controller.php';
        session_start();
        $_SESSION['logged'] = FALSE;

    ?>

    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <div id="mid">
                <div id="left">
                    <?php

                    if (User::isLogged())
                    {
                        $menu = Controller::initMenuUser();
                    }
                    else
                    {
                        $menu = Controller::initMenuGuest();
                    }

                    if (!empty($menu))
                    {
                        foreach ($menu as $item) 
                        {
                            echo '<li><a href="' . $item['url'] . '">' . $item['text'] . '</a>';
                        }
                    }
                </div>
            </div>
        </body>
    </html>

What should be happening is that on the div with id="left", there should be a different set of links and names depending either the user is logged in or not.

//Controller.php

     ...
     public static function initMenuGuest() 
        {
            $menu = array(
                array(
                    'url' => 'index.php?page=Login',
                    'text' => 'Login'
                ),
                array(
                    'url' => 'index.php?page=Register',
                    'text' => 'Register'
                )
            );
            return $menu;
        }

        public static function initMenuUser() 
        {
            $menu = array(
                array(
                    'url' => 'index.php?page=Profile',
                    'text' => 'Profile'
                ),
                array(
                    'url' => 'index.php?page=Cart',
                    'text' => 'Cart'
                ),
                array(
                    'url' => 'index.php?logout=true',
                    'text' => 'Logout'
                ),
                array(
                    'url' => 'index.php?page=Administration',
                    'text' => 'Administration'
                )
            );
            return($menu);
        }

Here is the method of the User class responsible to changing the $_SESSION['logged'] to TRUE. I'm calling this function on the Controller.php. if (isset($_POST['login'])) then call the function. $_POST['login'] is a form on the index.php

    //User.php
     public static function login()
        {
            $login = $_POST['loginl'];
            $password = $_POST['passwordl'];
            $u = new User($login);
            $res = $u ->check();
            $udb = $res->fetch_object();
            if (password_verify($password, $udb->password))
            {
                $_SESSION['user'] = $udb->login;
                $_SESSION['id'] = $udb->id;
                $_SESSION['logged'] = TRUE;
                $msg = 'Welcome '.$_SESSION['user'].' '.$_SESSION['logged'];
            }
            else
            {
                $msg = "Wrong Login information.";
            }      
            $res->free();
            return $msg;
        }

    public static function isLogged()
    {
        if ($_SESSION['logged'] == TRUE)
        {
            $res = TRUE;
        }
        else 
        {
            $res = FALSE;
        }
        return $res;
    }  

Upvotes: 0

Views: 52

Answers (1)

John Bupit
John Bupit

Reputation: 10618

You are setting $_SESSION['logged'] = FALSE in the third line of index.php. Regardless of the login status of a User, this line sets the value of logged to false, thereby failing the User::isLoggedIn() condition that follows.

Remove that line.

Upvotes: 0

Related Questions