erickreno Posted October 29, 2021 Share Posted October 29, 2021 I am having an issue I cannot figure out. Been looking at it for 3 days now and I don't see anything wrong, maybe you can help. When I run the script, the form comes up and works well, when I submit it I get the following PHP error message: Quote Warning: session_start(): Cannot start session when headers already sent in /home/smchigho/public_html/eweek/admin/index.php on line 23 Warning: Cannot modify header information - headers already sent by (output started at /home/smchigho/public_html/eweek/admin/index.php:1) in /home/smchigho/public_html/eweek/admin/index.php on line 32 header() comes after the session_start() but it's still telling me there is an issue. The include files all work and have no spaces or echo commands, they are used in another part of the site and work fine. Here is the code: <?php include_once('../includes/connectvars.php');?> <?php include_once('../functions/globalFunctions.php');?> <?php $webID = ''; $password = ''; $sendForm = ''; // * Form Processing * if(isset($_POST['username'], $_POST['password'])){ $webID = $_POST['username']; $password = $_POST['password']; // * Validate login * if(validateLoginAdmin($webID, $password, $conn) == false){ $sendForm = false; }else{$sendForm = true;} // * Good to go? Start Session * if($sendForm == true){ //include_once('startSessionAdmin.php'); session_start(); $query = "SELECT * FROM adminlogin WHERE adminuser = '$webID' limit 1"; $result = mysqli_query($conn, $query); $row = $result->fetch_assoc(); $_SESSION['firstName'] = $row['firstName']; $_SESSION['lastName'] = $row['lastName']; $_SESSION['login'] = 1; $_SESSION['admin'] = 2; $_SESSION['username'] = $webID; header('Location:admin_landing.php'); exit(); }} ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Login</title> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css"> <link rel="stylesheet" href="../css/login.css"> </head> <body> <div class="login"> <h1>Enrichment Week Admin Login</h1> <form action="index.php" method="post"> <label for="username"> <i class="fas fa-user"></i> </label> <input type="text" name="username" placeholder="Username" id="username" required> <label for="password"> <i class="fas fa-lock"></i> </label> <input type="password" name="password" placeholder="Password" id="password" required> <input type="submit" value="Login"> </form> </div> </body> </html> What am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/314146-issues-with-start_session-before-header/ Share on other sites More sharing options...
ginerjm Posted October 29, 2021 Share Posted October 29, 2021 You need to make sure that you have no blank lines/spaces being sent out before you do these things. Make sure that your php tag is the very first byte of your script. Quote Link to comment https://forums.phpfreaks.com/topic/314146-issues-with-start_session-before-header/#findComment-1591578 Share on other sites More sharing options...
erickreno Posted October 29, 2021 Author Share Posted October 29, 2021 First thing I thought about, even recreated the file manually to make sure I wasn't missing any errant characters. The fact that the header() is after the session_start() is the part that is strange. Also checked all include files to make sure no errant characters. I use the same script for student's login with no issues. Quote Link to comment https://forums.phpfreaks.com/topic/314146-issues-with-start_session-before-header/#findComment-1591579 Share on other sites More sharing options...
ginerjm Posted October 29, 2021 Share Posted October 29, 2021 What is that blank line between your MULTIPLE php tags? Don't use a close php tag unless you have to. Why isn't the session start up there at the top? Quote Link to comment https://forums.phpfreaks.com/topic/314146-issues-with-start_session-before-header/#findComment-1591580 Share on other sites More sharing options...
ginerjm Posted October 29, 2021 Share Posted October 29, 2021 This more like it should look: <?php session_start(); include_once '../includes/connectvars.php'; include_once '../functions/globalFunctions.php'; $webID = ''; $password = ''; $sendForm = ''; // * Form Processing * if(isset($_POST['username'], $_POST['password'])) { $webID = $_POST['username']; $password = $_POST['password']; // * Validate login * if(validateLoginAdmin($webID, $password, $conn) == false) { $sendForm = false; } else{$sendForm = true;} // * Good to go? Start Session * if($sendForm == true) { //include_once('startSessionAdmin.php'); $query = "SELECT * FROM adminlogin WHERE adminuser = '$webID' limit 1"; $result = mysqli_query($conn, $query); $row = $result->fetch_assoc(); $_SESSION['firstName'] = $row['firstName']; $_SESSION['lastName'] = $row['lastName']; $_SESSION['login'] = 1; $_SESSION['admin'] = 2; $_SESSION['username'] = $webID; header('Location:admin_landing.php'); exit(); } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Login</title> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css"> <link rel="stylesheet" href="../css/login.css"> </head> <body> <div class="login"> <h1>Enrichment Week Admin Login</h1> <form action="index.php" method="post"> <label for="username"> <i class="fas fa-user"></i> </label> <input type="text" name="username" placeholder="Username" id="username" required> <label for="password"> <i class="fas fa-lock"></i> </label> <input type="password" name="password" placeholder="Password" id="password" required> <input type="submit" value="Login"> </form> </div> </body> </html> PS Include/requires do not need parens. Quote Link to comment https://forums.phpfreaks.com/topic/314146-issues-with-start_session-before-header/#findComment-1591581 Share on other sites More sharing options...
ginerjm Posted October 29, 2021 Share Posted October 29, 2021 To clarify the error message points you at the header line but that is not the cause. It is probably that blank line up at the top. When in php mode nothing goes to the client by accident. But when NOT in php mode any blank chars or lines do go to the client and that's the message you are getting. Actually line 1 seems to be your problem. Quote Link to comment https://forums.phpfreaks.com/topic/314146-issues-with-start_session-before-header/#findComment-1591582 Share on other sites More sharing options...
erickreno Posted October 29, 2021 Author Share Posted October 29, 2021 thanks ginerjm, I thought it might be a strange issue, but went through all the white space on the page again and found a single space that was hiding behind a tab. Quote Link to comment https://forums.phpfreaks.com/topic/314146-issues-with-start_session-before-header/#findComment-1591585 Share on other sites More sharing options...
ginerjm Posted October 29, 2021 Share Posted October 29, 2021 (edited) Take a long look at how I coded up your page. Only one php start tag and one end. Pretty simple. You don't have to use a pair of tags for a single line of php code. But - all you had to do was move your first php start tag to the very first line in column 1 and that would have cured you if you got rid of the end tags too. HTH Edited October 29, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314146-issues-with-start_session-before-header/#findComment-1591586 Share on other sites More sharing options...
gizmola Posted October 31, 2021 Share Posted October 31, 2021 I would urge you to adopt most if not all the coding standards in PSR-12 from the PHP Framework Interop group, which has published many useful PHP standards. In particular as mentioned, look at 2.2 which tells you to omit a php end tag. Do this whenever and wherever you can. In conclusion, your php files should never end with a closing tag, and would only need a closing tag if there is an intermixing of php and html blocks. Even if you do, as ginerjm helpfully demonstrated, you won't ever have a closing tag at the end of the file, regardless of whether the file ended with an html block or a php block. Quote Link to comment https://forums.phpfreaks.com/topic/314146-issues-with-start_session-before-header/#findComment-1591610 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.