0

I am getting an error while trying to setup my login system. I've tried multiple "solutions" but none of them work such as putting the start_session on the top of all the scripts.

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at test4\index.php:311) in test4\classes\Login.php on line 28


Line 311:

    <?php // line 311
    if (version_compare(PHP_VERSION, '5.3.7', '<')) {
        exit("Please upgrade the PHP version to 5.3.7 or higher.");
    } else if (version_compare(PHP_VERSION, '5.5.0', '<')) {
        require_once("libraries/password_compatibility_library.php");
    }

    require_once("config/db.php");
    require_once("classes/Login.php");

    $login = new Login();

Line 28 in classes/Login.php:

<?php
class Login {
    private $db_connection = null;
    public $errors = array();
    public $messages = array();
    public function __construct() {

        session_start(); // line 28

Thank you in advance!

J. Doe
  • 503
  • 1
  • 6
  • 19

2 Answers2

3

The session_start() function should be on top of your file like this:

<?php
session_start(); // Should be on top when using $_SESSION

class Login {
    private $db_connection = null;
    public $errors = array();
    public $messages = array();
    public function __construct() {

        // Rest of your scripts

This error caused because the headers where sent already so it is to late to load the session_handler() either, that's why you have to put it on top of your file to load it with the headers.

If you use a framework that includes the pages, you only need to use the session_start() once in the main file (e.g. index.php), where you include the other files

I hope this will help you.

node_modules
  • 4,790
  • 6
  • 21
  • 37
  • Thank you for your input C0dekid, but this doesn't seem to work for me: `Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at test4\index.php:311) in test4\classes\Login.php on line 2` – J. Doe Apr 28 '16 at 11:13
  • Do you use the session elsewhere in your files? – node_modules Apr 28 '16 at 11:15
  • I just checked and the one in classes/Login.php is the only session_start – J. Doe Apr 28 '16 at 11:17
  • Are you using a framework? If yes, remove the `session_start()` in all your files and put it in the main file (e.g. index.php). :) – node_modules Apr 28 '16 at 11:18
  • 1
    That worked, thanks for the help C0dekid! – J. Doe Apr 28 '16 at 11:24
3

session_start() function call just top.

<?php
session_start();