Friday 21 September 2018

php - "Warning: Cannot modify header information - headers already sent by [error]"










Here's my code:



$con = mysql_connect("localhost",$dbuser,$dbpass);

mysql_select_db($dbname, $con);
function crSalt($max = 15) {
$characterList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$i = 0;
$salt = "";
while ($i < $max) {
$salt .= $characterList{mt_rand(0, (strlen($characterList) - 1))};
$i++;
}
return $salt;

}
if ($_POST['username'] && $_POST['password']){
$username=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$hash = hash("ripemd160", $password);
$x = mysql_fetch_array(mysql_query("SELECT * FROM `accounts` WHERE `Username`='$username'"));
if ($hash == $x['Password']){
$salt = crSalt();
setcookie("xtx_SALT", $salt, time()+3600*74 , "/");
setcookie("xtx_ID", $x['id'], time()+3600*74 , "/");

setcookie("xtx_CRED", hash("ripemd160", $username), time()+3600*74, "/");
mysql_query("UPDATE `accounts` SET `Salt`='$salt' WHERE `Username`='$username'");
}
}
?>

Username:
Password:





I've been struggling with these errors for a while:




Warning: Cannot modify header information - headers already sent by
(output started at
/home/xtrosx10/public_html/Secure/Login/index.php:5) in
/home/xtrosx10/public_html/Secure/Login/index.php on line 27




Warning: Cannot modify header information - headers already sent by
(output started at
/home/xtrosx10/public_html/Secure/Login/index.php:5) in
/home/xtrosx10/public_html/Secure/Login/index.php on line 28



Warning: Cannot modify header information - headers already sent by
(output started at
/home/xtrosx10/public_html/Secure/Login/index.php:5) in
/home/xtrosx10/public_html/Secure/Login/index.php on line 29





I don't see why I am actually getting this because there is not one line of code here that suggests a header should be sent.


Answer



setcookie will try to modify headers, you'll need to make sure there are no echos or erroring/noticing/warning parts of code before that point. Try looking in your error logs.



setcookie: http://php.net/manual/en/function.setcookie.php


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...