Session does not work between functions

Asked

Viewed 48 times

0

public function index(){
    session_start();
    $_SESSION['user_id'] = 2;
    // Na minha página é impressa o valor "2" 
    echo $_SESSION['user_id']."<br/>";
    $this->view->render("dashboard/dashboard-2");
}

public function teste(){
    session_start();
    echo $_SESSION['user_id']."<br/>";
   //Notice: Undefined index: user_id
}

//My HTML

       <a href="<?php echo URL ?>dashboard/teste" class="btn btn-primary">TESTE</a>

Before rendering the page, I save a id in Session, in my case is $_SESSION['user_id'] = 2. After the page is rendered, there is a download button, in case a href redirecting to the method teste(), but when opening this method, my session does not recognize the user_id and an error is generated: Notice: Undefined index: user_id.

There’s something strange too. Locally this code works, but on the server (when hosted) it does not work.

  • 1

    In your PHP.INI display_errors is set to ON?

1 answer

0

<?php session_start();

Try using session_start on the first line of your php file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.