0

I have recently published my website on Hostgator and I have a problem with the sessions. I have a session start on multiple pages: config.php, login.php, logout.php, index.php.

I have tried everything out there in with checking the whitespace, adding to the .httpaccess file, adding @ob_start()...and nothing seems to work. In the error.log I have the following error:

PHP Warning: session_start(): Cannot start session when headers already sent in ../config.php on line 3

Can you please help me?

alexxa
  • 39
  • 1
  • 9

2 Answers2

0

Session start should be placed at the top of the script. And don't repeat. To avoid errors, Just put a '@' before session_start command. Example

@session_start();
0

You should put your session_start() at very beginning of scripts

config.php

<?php
session_start();

// do your other code here

in your subviews like index you should put the config.php in the very beginning of line or make a separate php file like session.php and include it on very beginning of scripts.

The cause of error is that session is trying to start but the page is already sent to the browser or rendered.

or maybe you have a whitespace in opening tag?

Jerson
  • 1,700
  • 2
  • 10
  • 14
  • what do you mean by ,,in your subviews like index you should put the config.php in the very beginning of line ?" How does making the session in another file help? – alexxa Jun 14 '21 at 06:45
  • I mean you should create one file for that the include it of very beginning line of scripts – Jerson Jun 14 '21 at 06:59
  • unfortunately, the error still persists but it says ''PHP Warning: session_start(): Cannot start session when headers already sent in ..session.php on line 1" instead of saying that the error is in config.php – alexxa Jun 14 '21 at 07:09
  • can you post the whole code here? – Jerson Jun 14 '21 at 07:12