2

I am very new to PHP. Coding my first website.

index.php

<?php
session_start();
?>   
<!DOCTYPE html>
<html lang="en">
   <form action="authenticate.php" method="post">
     //form inputs
   </form>
  </body>
</html>

The form calls authenticate.php:

<?php
session_start();

//Authenication work
//...
// line 43 here
if(!$authen){
  include_once("index.php");
}
else{
  header('Location: main.php');
  exit();
}
?>

Whenever I run the website and login, as soon as i click on the submit button in the form, instead of being redirected to main.php, I get redirected in the browser to authenticate.php, and I see this :

enter image description here

Text in image:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /var/www/html/projet/authenticate.php:1) in /var/www/html/projet/authenticate.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/projet/authenticate.php:1) in /var/www/html/projet/authenticate.php on line 51

My main.php code (the page I was supposed to be redirected to instead):

<?php session_start(); ?>
<html>
<head>
...

Things I have tried so far:

  1. Making sure session_start() is at the beginning of every page
  2. Trimming EVERY POSSIBLE white space before <?php and after ?>

I am getting desperate. Please note that the same website and files work locally on wampserver, but when i put them on a server this happened.

Am I missing something? Sorry I am completely new to PHP and I am learning.

Also questions:

  1. Do comments in PHP files count as empty spaces?
  2. Does Indentation in conditions in PHP files count as empty spaces?
HelpASisterOut
  • 3,085
  • 16
  • 45
  • 89
  • Did you check the file's encoding as well as the server's default encoding for PHP files? A BOM (byte order mark) can also cause that. – Funk Forty Niner Nov 03 '18 at 00:36
  • @FunkFortyNiner I am editing the files on notepad++ (made sure encoding is `UTF8` and then pushed then using Filezilla. Then i would just Right click Edit The files from the server directly using Notepad. What can i do to investigate further? – HelpASisterOut Nov 03 '18 at 00:38
  • There are 2 types of UTF-8 encoding types. One with BOM and one without. If you didn't specify without the byte order mark, then there's the problem. It should be without BOM. – Funk Forty Niner Nov 03 '18 at 00:39
  • If not what I said above, then there may be a hidden unicode somewhere; that too can cause headers to be sent. Use a hex editor and you'll see it. – Funk Forty Niner Nov 03 '18 at 00:41
  • @FunkFortyNiner I noticed when I opened files using Notepad++ that some of them were encoded in UTF-8 BOM, I changed it to UTF-8 , and then I pushed. After that I continued to edit using Notepad. – HelpASisterOut Nov 03 '18 at 00:42
  • There's the problem. You need to save them all again without BOM. Edit: this is covered in [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php?rq=1). – Funk Forty Niner Nov 03 '18 at 00:42
  • 1
    @FunkFortyNiner Great!! Post it as an answer so I can accept it. – HelpASisterOut Nov 03 '18 at 00:46
  • As requested, *cheers* – Funk Forty Niner Nov 03 '18 at 00:53

1 Answers1

1

There are 2 types of UTF-8 encoding types. One with BOM (byte order mark) and one without. If you didn't specify without the byte order mark, then there's the problem. It should be without BOM.

You need to resave all of your files without it.

References:

A byte order mark is an invisible set of characters that can also account for output before header.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141