Medi DEMIRDELEN
Medi DEMIRDELEN

Reputation: 45

My session variables are not carried over through the different pages

I am making a user login interface on the MVC architecture. The view has a login/password form thtat will send the form results to a file in the controller part. The controller will call the sql query in the model to check if the this login/passowrd are present, and if they are, they redirect to home page with a session variable having the login.

However, while the redirect is working, the session variable doesn't seem to be carried over through the home page when it's redirected there.

This is my controller/login.php file

include '../model/query.php'; //the model file with the sql queries

$query = new query();

if ( isset($_GET['login']) && isset($_GET['password']) )
{
    $login = $_GET['login'];
    $password = $_GET['password'];

    $user = $query->checkUser($login, $password);
    //this will return an array containing the result of the query that checks the lines with that login and password
    if($user)
    {
        $_SESSION['login'] = $user['login'];
        header('Location: ../view/home.php'); //this also works correctly
    }
}

And this is my view/home.php file

include 'page.php'; 
//page.php is a class that features the elements common to all pages

$page = new page();
$page->body = "test" . print_r($_SESSION); 
//I've added the print_r($_SESSION) to check if the session variable's value
echo $page->showpage();

However, after the computing on login.php and returning to home.php, the page shows an error message

Notice: Undefined variable: _SESSION in C:[...]\home.php on line 15"

(line 15 is where I do the print_r)

And, the page displays "test1" (aka corresponding to "test" . print_r($_SESSION))

Upvotes: 0

Views: 51

Answers (1)

mahfuz
mahfuz

Reputation: 3223

Add session_start();

  • at the beginning of file controller/login.php, and,
  • at the beginning of file view/home.php

Upvotes: 0

Related Questions