session_start(): Cannot send session cache limiter - headers already sent even with session_start() at the top [closed]


session_start(): Cannot send session cache limiter - headers already sent even with session_start() at the top [closed]



I'm trying to set up an admin login page but I keep getting this error. It works on a local server but not on an online server. I know this is a duplicate but all the other answer suggest putting session_start() at the top which I'm already doing. Here's my code



index.php






if ( !isset($_SESSION['user']) ){
header('Location: login.php');
} else {
?>
//html content
<?php
}



login.php (just html with a form tag)


<form method="POST" action="loginVerify.php" autocomplete="off">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="submit">
</form>



login verify


<?php
session_start();

$username = $_POST['username'];
$password = $_POST['password'];
$adminUsername = 'myUsername';
$adminPassword = 'myPassword';

if($username==$adminUsername && $password == $adminPassword) {
$_SESSION['user'] = $username;
header('Location: index.php');
} else {
header('Location: login.php');
}



Any ideas why this is happening? Also, is this a good structure for a login system? Am I doing this right?



Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





We have to assume that the problem is in your login verify file, and not in your index.php file, which would make more sense?
– KIKO Software
Jul 2 at 9:45



login verify


index.php





where exactly does the error occur?
– Tobias F.
Jul 2 at 9:45





which version of php are you using in local and on the server? Also putting 'ob_start();' at the top of both php files should solve your issue
– Jacopo Sciampi
Jul 2 at 9:57





First of all, show proper code. (No, your index.php does most certainly not begin with if ( directly.) Plus, the error message already tells you were exactly that first output happened. If despite checking the duplicates even this didn’t help you figure it out on your own so far, then at least quote the full error message.
– CBroe
Jul 2 at 10:50


if (





“It works on a local server but not on an online server.” - then the person who wrote this likely didn’t know what they were doing to begin with (otherwise they would not have relied on output buffering being enabled for their script to work) … which might say a lot already about whether you should use this script in any productive setting in the first place.
– CBroe
Jul 2 at 10:53





1 Answer
1



Add after any header('Location: xxx');.


header('Location: xxx');



command: die();


die();

Popular posts from this blog

How to make file upload 'Required' in Contact Form 7?

Rothschild family

amazon EC2 - How to make wp-config.php to writable?