TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
Session error
  • Hi,

    Everytime I run my code I get this error: A session had already been started - ignoring session_start()

    Do you know what can be the reason?

  • I'm getting something now ... nope. I can't read your mind.

    What Kohana version are you using? Can you provide some code samples?

  • Don't you have your crystall ball? (joke :P)

    version 3.x

    home view includes controllers (I use there UTF-8 coding header if it will help)

    login controller has 2 method - first one to display login's view, and the second one to run after user submits a form:

      public function action_index() {
         $this->template_content = new View('login');
      }
    
      public function run_model() {
         $results = new Model_Login;
         $count = $results->check(); // numbers of rows that match given login and password
         $row = $results->checkArray(); // if $count is not zero, $row is the array with fields from the matched row
         if ($count > 0) {
            Session::instance()->set('id', $row['ID']); // session with user id
            Session::instance()->set('zalogowany', $row['Login']); // session with user login
            $this->template->login = $row['Login']; // var to display login after logging in
            $this->template_content = new View('Done/login_done');
         } else {
            $this->template_content = new View('Errors/login_error');
         }
      }
    

    and login view, which is the form with login and password fields. In login's model I check if a row with login and password given in the form exists. If it does, in controller I create sessions, if not I display error (is it actually a good way to display errors?). I don't know if it will work because I haven't validated this code yet, I still get this A session had already been started - ignoring session_start() error. Is there something wrong in this code that causes this error?

  • any ideas?

  • Ivenius, can you show us the _whole_ backtrace? It sounds like you are trying to start two sessions in the same request and that won't work. The backtrace should show you what is happening.
  • SYSPATH/classes/kohana/session/native.php [ 27 ] » Kohana_Core::error_handler()
    SYSPATH/classes/kohana/session.php [ 177 ] » Kohana_Session_Native->_read()
    SYSPATH/classes/kohana/session.php [ 93 ] » Kohana_Session->read(arguments)
    SYSPATH/classes/kohana/session.php [ 35 ] » Kohana_Session->__construct(arguments)
    APPPATH/classes/model/guestbook.php [ 47 ] » Kohana_Session::instance(arguments)
    APPPATH/classes/controller/guestbook.php [ 13 ] » Model_Guestbook->GetUsersOnline()
    {PHP internal call} » Controller_Guestbook->action_index()
    SYSPATH/classes/kohana/request.php [ 882 ] » ReflectionMethod->invokeArgs()
    APPPATH/views/home.php [ 19 ] » Kohana_Request->execute(arguments)
    SYSPATH/classes/kohana/view.php [ 49 ] » include()
    SYSPATH/classes/kohana/view.php [ 303 ] » Kohana_View::capture(arguments)
    SYSPATH/classes/kohana/view.php [ 193 ] » Kohana_View->render(arguments)
    APPPATH/bootstrap.php [ 187 ] » Kohana_View->__toString()
    DOCROOT/index.php [ 103 ] » require()

    APPATH/classes/model/guestbook.php, lines 46 - 48:

    $query = DB::query(Database::SELECT, 'SELECT Login FROM Users WHERE ID = :online')
    ->param(':online', Session::instance()->get('zalogowany'))
    ->execute();

    APPATH/classes/controllers/guestbook.php, lines 12 - 13:

    $results = new Model_Guestbook;
    $this->template->UsersOnline = $results->GetUsersOnline();
  • Your PHP is probably set to automatically start the session. You should disable this in php.ini, or call session_destroy() before starting the session.

  • I have it disabled. I'm trying to validate my code, maybe it will help. How do I use mysqli_real_escape_string() function in 3.x version?
  • How do I use mysqli_real_escape_string() function in 3.x version?

    By using Database::quote.

  • How do I do this?

         $query = DB::query(Database::SELECT, 'SELECT * FROM Users WHERE Login = :login and Password = :password')
                ->param(':login', $_POST['login'])
                ->param(':password', $this->passwordHash($_POST['password']))
                ->quote($_POST['password'])
                ->execute();
    

    This gives an error Call to undefined method Database_Query::quote(). Where should I use Database::quote? The example in guide is $db->quote(NULL);, what's $db variable?

  • You dont need to use Database::quote() on paramenter you pass to Database_Query::param() of Database_Query::bind().

    Database_Query::param() and Database_Query::bind() store the value/reference you pass to them. Database_Query::execute() fills $db (if necessary) and passes it to Database_Query::compile(). Then Database_Query::compile() will use Database::quote() on the parameters.

  • Oh, thanks for help ;] One more little question...

    My controller extends template controller. It has public template variable that loades a form view.

      public $template = 'login';
      public function action_index() {
         $this->template->content = new View('login');
      }
      public function action_controll() {
         $results = new Model_Login;
         $row = $results->browse();
         if ($row == 'error') {
            $this->template->content = new View('Errors/login_error');
         } else {
            $this->template->login = $row; // var to display login after logging in
            $this->template->content = new View('Done/login_done');
         }
      }
    

    First, view form is loading in action_index() method. Next I type login and password, and the form redirect me to the above controller's action_controll() method. If the model's return value is error, it should to include error's view, otherwise it should to include a view file that shows everything was successful. However it always shows view file login. What should I change so that this form is not included after submitting it?

  • Nevermind, I solved it. How can I pass a get value through making new view file in controller? In controller I get login and I want to display it in view file. It should to look like this:

    $this->template->content = new View('Done/login_done?login='.$row);

    But it doesn't word. How do I do this?