Having PHP session issue
Getting following warning messages on page load. I don't understand what is that?
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/heimann/public_html/admin/login.php:9) in /home/heimann/public_html/admin/login.php on line 47
Warning: Cannot modify header information - headers already sent by (output started at /home/heimann/public_html/admin/login.php:9) in /home/heimann/public_html/admin/login.php on line 52
The php code
//line 9 <?php
$admin = 'http://'. $_SERVER['HTTP_HOST'] . '/admin/';
$db = new MySQLi('localhost', 'heimann_admin', '0579ural', 'heimann_content') or die($db->error);
$db->set_charset('utf8');
$username=$db->escape_string($_POST['username']);
$password=$db->escape_string($_POST['password']);
$submit=$db->escape_string($_POST['submit']);
if($username && $password && $submit=='Giriş'){
$result=$db->query("SELECT id, username, password FROM users WHERE username='$username' AND password='$password'");
$count=$result->num_rows;
if($count>0)
{
while($row=$result->fetch_object()){
$dbusername=$row->username;
$dbpassword=$row->password;
$dbuserid=$row->id;
}
if($username==$dbusername && $password==$dbpassword){
//line 47 session_start();
$_SESSION['usernam开发者_StackOverflow社区e']=$dbusername;
$_SESSION['userid']=$dbuserid;
//line 52 header('location:'.$admin);
}
else message("Şifrə səhvdir");
}
else {message("Bazada belə login və şifrə kombinasiyası yoxdur");}
}
else {message("Giriş parametrlərindən biri daxil edilməyib");}
?>
<?
function message($text){
global $admin;
echo '<div class="message">'.$text. '</br><a href="'.$admin.'">Geri</a></div>';
}
?>
To avoid caching on the client, the session_start function sends some HTTP headers to the client. However, in any HTTP response, the headers come before the content. Your script has already sent some content when you call session_start(), so it can no longer send the headers.
You can call session_start() earlier in your file, so that it sends the headers before any content, or you can use output buffering to keep the content in a buffer.
you have some output before the session_start
, thats forbidden. If you cannot change the output before session_start you can use ob_start();
and ob_flush();
to cache the output.
If you have already outputted anything before your PHP code started, that is probably causing the issue, PHP has issues with certain functions being called after output has already started.
精彩评论