-3

I've hosted my website on 000webhost. It was working fine locally with no errors at all. Once I've hosted there, this error has arised on the contact page:

Warning: session_start(): Cannot start session when headers already sent in ..

my code is as follow:

<!DOCTYPE html>
<html lang="en">
<?php
    session_start();
    require_once('config.php'); //the required page has no session_start() inside it
    if (empty($_SESSION['user'])) {
        if (!empty($_COOKIE['email']) && !empty($_COOKIE['name']) && !empty($_COOKIE['role']) && !empty($_COOKIE['id'])) { //remember me was selected at last log in

             if ($_COOKIE['role'] != 'customer')
                  redirect_page('../admin_home.php?status=not_auth');

             $_SESSION['user']['email'] = $_COOKIE['email'];
             $_SESSION['user']['name'] = $_COOKIE['name'];
             $_SESSION['user']['role'] = $_COOKIE['role'];
             $_SESSION['user']['id'] = $_COOKIE['id'];
             $welcome = true;
        } else {
             $welcome = false;
        }
    } else {
        if ($_SESSION['user']['role'] == 'admin') {
             redirect_page('../admin_home.php?status=not_auth');
        } else {
             $welcome = true;
        }
    }
?>
<head>
   <title>Sure Wheels</title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   <!-- some styling and links -->

Would anyone please tell me whats wrong with my code?

TIA

  • Try moving `session_start();` before ` ` – Pippo Apr 16 '23 at 21:40
  • 2
    outputting HTML in your file causes the server to start sending the response body of the HTTP request to the browser. Once the body is started, it can no longer send any more response headers, since in the structure of a HTTP response the headers must come first (therefore it sends the headers before starting that body output). But starting the session involves setting the session cookie header in the response, so it can't do that if they've already been sent. Hence the error. Make sure session_start() is executed before any content in your page (either static or PHP-generated). – ADyson Apr 16 '23 at 21:45
  • 2
    P.S. This is a duplicate of many previous questions...you need to google the error message. e.g. [session\_start errors because 'headers already sent' by the same session\_start action?](https://stackoverflow.com/questions/28366881/session-start-errors-because-headers-already-sent-by-the-same-session-start-ac) – ADyson Apr 16 '23 at 21:45

1 Answers1

0

You need to move the session_start(); to the top.
session_start() must be called before anything is sent to the client (such as HTML).

Therefore the code would look like this:

<?php
    session_start();
?>
<!DOCTYPE html>
<html lang="en">
<?php
    require_once('config.php'); //the required page has no session_start() inside it
    if (empty($_SESSION['user'])) {
        if (!empty($_COOKIE['email']) && !empty($_COOKIE['name']) && !empty($_COOKIE['role']) && !empty($_COOKIE['id'])) { //remember me was selected at last log in

             if ($_COOKIE['role'] != 'customer')
                  redirect_page('../admin_home.php?status=not_auth');

             $_SESSION['user']['email'] = $_COOKIE['email'];
             $_SESSION['user']['name'] = $_COOKIE['name'];
             $_SESSION['user']['role'] = $_COOKIE['role'];
             $_SESSION['user']['id'] = $_COOKIE['id'];
             $welcome = true;
        } else {
             $welcome = false;
        }
    } else {
        if ($_SESSION['user']['role'] == 'admin') {
             redirect_page('../admin_home.php?status=not_auth');
        } else {
             $welcome = true;
        }
    }
?>
<head>
   <title>Sure Wheels</title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   <!-- some styling and links -->