Session Errors

When I run my website (not up yet), I go from my index.html page to ContactUS.PHP page. In my ContactUS.php page I have two required_once statements. The first one is “…/navbar/navbar.php” and the other is ‘backform.php’. And then the program tosses me an error. The error states "Warning session_start(): Session cannot be started after headers have already been sent in D:\XAMPP\hdocs\sitename. I do know that when my ‘backform.php’ starts up, the very first line in it is session_start(); Is this due to the new navbar? My navbar problem can be found in CSS.
Thanks

Assuming the nav bar is HTML, that will be the problem.
A session start must come before anything has been output to the browser. So there must be no HTML (or any other kind of output) before session_start(), it must come before <DOCTYPE html> which is generally the first output to an HTML page.

1 Like

The full error message should state where output is occurring at that is preventing the session_start from working, though php had some 7.x versions that didn’t include this in the error message.

The code for any page should be laid out in this general order -

  1. initialization - session_start should be in this section
  2. post method form processing
  3. get method business logic - get/produce data needed to display the page
  4. html document - your navbar.php code should be required in this section
2 Likes

I changed the file type from html to php by adding in some php coding, didn’t work. The message is on my backpage. Here are the sceenshots of all 3 pages.

I tried to post, but since I’m a new user I can’t :(. Any other way of showing?
Thanks Dan

I think you misunderstood my answer.
It does not matter what file type you use, there must not be any output to the browser before the session start.
Your nav is HTML regardless of whether it comes from an HTML or PHP file.
The response from @mabismad is more detailed, and explains hoe you properly structure your PHP scripts.
Session start should come first, or early in the script, and must come before you get to any HTML or other output.

2 Likes