Session does not work between functions

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 on Session , in my case it is $_SESSION['user_id'] = 2 . After the page is rendered, there is a download button, in this case a href that redirects to the teste() method, however when opening this method, my session does not recognize user_id and an error is generated: Notice: Undefined index: user_id .

There is a strange thing as well. Locally this code works, but on the server (when being hosted) it does not work.

    
asked by anonymous 19.09.2016 / 14:58

1 answer

0

<?php session_start();

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

    
19.09.2016 / 16:34