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?
8
  • 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. Commented Nov 3, 2018 at 0: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? Commented Nov 3, 2018 at 0: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. Commented Nov 3, 2018 at 0: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. Commented Nov 3, 2018 at 0:41
  • 1
    @FunkFortyNiner Great!! Post it as an answer so I can accept it. Commented Nov 3, 2018 at 0:46

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.