0
  • session_start() method is in Session.php class constructor

  • Session.php class is required in every page header (require_once('Session.php'))

That means that Session class will be included again when I redirect to another url and every time new session will be started. This will cause errors.

But this piece of code works properly, why?

class Session {
    private $signed_in = false;
    public $user_id;
    private $is_admin;
    public $message;
    public $count;

    function __construct() {

        session_start();
        $this->visitor_count();
        $this->check_the_login();
        $this->check_message();
    }

} //End of Session Class

$session = new Session();

Why I should start new session on every initialization of Session class

devpro
  • 16,184
  • 3
  • 27
  • 38
Denis2310
  • 939
  • 1
  • 15
  • 41
  • 1
    [`session_start`](http://php.net/manual/en/function.session-start.php) does not always start a new session, it also resumes an existing one (first sentence of the doc) – Karsten Koop Feb 01 '19 at 11:51
  • 1
    You cannot call `session_start()` twice in the same script. Two URL requests are two different script executions. – Álvaro González Feb 01 '19 at 11:53
  • Okay then it's not bad idea to have session_start() on every page? – Denis2310 Feb 01 '19 at 11:53
  • 3
    It is _necessary_ to have it on every page, otherwise it won’t work correctly to begin with. – 04FS Feb 01 '19 at 11:56
  • u can also create a file like autoload and use session in this file, something like CI framework. are u using any framework here? – devpro Feb 01 '19 at 11:58
  • PHP is interpreted, every time that a user request a page, its a new "start/end" process. You need remember php thats has a session, at least, in one of pages processed. – Recigio Poffo Feb 01 '19 at 11:59

2 Answers2

2

Your problem is call session_start in the middle of the code:

http://php.net/manual/en/function.session-start.php

Note:

To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

Try put it at start of file, before class declaration, or in a another include.

0

I would decouple your session class from the session_start command (sounds silly but bear with) to avoid the posiblity of the calls to it becoming confused and/or forgotton and/or called at the wrong time (after headers).

If you are not using a framework, which I can assume youre not as youre dealing with sessions directly, then I would probably opt for putting the session_start call in a custom autoloader file which you load at the start of each request.

Something like this (assumes your'e using composer's autolaoder)

custom_autoload.php

<?php
    session_start();

    $loader = require __DIR__ . '/relative/path/to/vendor/autoload.php';

This way you know its called before any output to the browser. Then your session class can be responsible for handling the contents of $_SESSION which should be its single job.

See this question and answers for good guidance on using composers autoloader if your'e not familiar with it.

DevDonkey
  • 4,835
  • 2
  • 27
  • 41