Php 8.1 header not working

This was working in php7 the service provider upgraded to php8, the code stopped working,

setlocale(LC_MONETARY, 'en_IN');
include "../fdms/config.php";
$staffname = $_SESSION['staffname'];
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="css/comp.css" type="text/css" />
<link rel="stylesheet"  type="text/css" media= screen and (max-width:800px) href="../css/mobile.css">
<header class="header">
<li style='text-align: right; padding:5px;'>Logged In User:<?php echo $staffname?> </li>"
<form action="" method="POST">
        <button style="width:auto; background:brown"   name="option" value="HOME" >Notice </button>
        <button style="width:auto; background:orange;" name="option" value="COMP">Complaints</button>
        <button style="width:auto; background:green;"  name="option" value="ATTE">Attendence</button>
        <button style="width:auto; background:blue; "  name="option" value="LOGOUT">Logout</button>
<div style='text-align: right; padding:5px; color:whitesmoke; 'id="date_time"></div>
<script type="text/javascript" src="javascript.js"></script>
<script type="text/javascript">window.onload = date_time('date_time');</script>
$userlevel = $_SESSION['user_level'];
    if(!isset($userlevel)) {
$option=$_POST['option']=' ';

if($option==='HOME')   {header("Location:notice.php");}
if($option==='COMP')   {header("Location:complaint.php");}
if($option==='ATTE')   {header("Location:attendence.php");}
if($option==='LOGOUT') {header("Location:logout.php");}

Php has made a number of bad decisions in the past that has resulted in wasted time for newbie coders making code that is non-portable between differently configured systems. This problem is due to one such bad decision, having output_buffering set in the php.ini. If you are unable to change this setting on your web hosting, you will need to rewrite your code so that there is no output being sent to the browser prior to any header() statement.

As was already suggested in one of your threads, you should organize your code as follows, to allow any header() in your post method form processing code to work -

completely at sea, I dont even know where to look, a few pointers would be appreciated.

Well, there are documents that talk about what has changed from 7.0 to 8.1, but, it is easier just to add in error checking and reviewing your server logs. This is handled in various ways. You never upgrade your PHP versions on a live site. You always use your test server. Often a test server is a simple WAMP or XAMP system installed on your local pc.
Now with that said, on your test server, add these lines to the beginning of the page that you are working on. Or, add them to ALL pages using an include function…

ini_set("display_errors", 1);

This tells the page to log errors. Then, locate your log files. On each server, the log area is different. In my WAMP test server, just on my high-end laptop, it is located at: C:\wamp64\logs …
On my live server, I have to log into the control panel and go to the tools area and select VIEW-LOGS.
This is different on all servers, but, it easy to locate in your control panel.

To debug the page, erase all of your previous logs. You might be surprised to see the old logs as they can accumulate quickly while testing a website. Erase them or archive them if you want to read old ones. I have found that old ones do not matter on a test server, but, are important to review often to see what your users have been doing to cause errors. Anyway, once erased, go to your website page that is failing. If it is in your header area, go to any page. Then look at your logs and you will find where it is failing. At that point, you can read up on your individual errors. Post them here in a new post with the title of each problem so we can help individual problems. Without knowing the exact errors, it is hard to help you fix your upgrade problems.

Sorry for such a long post, but, wanted to explain how to start debugging your site errors.
To read about this upgrade from the source, here is a link and look at #2, incompatible functions.
Migrating from 7.4 to 8.0

thanks for your replay, appreciated

I got this error, which I rectified, using isset, no no errors showing,

[Fri Feb 10 15:20:38.421288 2023] [proxy_fcgi:error] [pid 45495:tid 139670331361024] [remote] AH01071: Got error ‘PHP message: PHP Warning: Undefined array key “staffname” in /home/fdmsin/domains/ on line 9PHP message: PHP Deprecated: ucfirst(): Passing null to parameter #1 ($string) of type string is deprecated in /home/fdmsin/domains/ on line 10PHP message: PHP Warning: Undefined array key “user_level” in /home/fdmsin/domains/ on line 11’

however still not working

The problem causing the latest errors is - either the session variables are not being set at all or the session start isn’t working, either on the page where you are setting the session variables or on the page where you are trying to use the session variables.

You should always validate inputs before using them. The session variables are inputs to the code on this page. Also, you should only store the user id (autoincrement primary index) in a session variable to indicate who the logged in user is, and query on each page request to get any other user data, such as the username or permissions.

If the page you are showing us in this thread requires a logged in user, you should test if the session variable holding the user id isset() near the top of the code. If it isn’t set, you should either setup an error message or redirect somewhere else and not allow any of the form processing or main output on the page to be executed.

If the page you are showing us in this thread does not require a logged in user, you should have conditional logic at each point where you are using any of the user data to output whatever content you want for a non-logged in user.

Sponsor our Newsletter | Privacy Policy | Terms of Service