Hello, HashCode!

I want to add a variable to the cookie, but it gives an error. The php code I used for this:

 <?php session_start(); $val = "Хай"; setcookie("ref", $val); ?> 

Mistake:

Warning: Cannot modify header information - headers already sent by (output started at Z:\home\ automatic.ru \www\index.php:9) in Z:\home\ automatic.ru \www\header.php on line 9

All code:

 <html> <head> <?php session_start(); $elseref = $_GET["ref"]; if ($elseref != '') { $val = "Хай"; setcookie("ref", $val); } require("config.php"); if (isset($_SESSION["username"]) && isset($_SESSION["password"])) { $username = $_SESSION["username"]; $password = $_SESSION["password"]; $sql = mysql_query("SELECT * FROM t_user WHERE username = '$username' and pass = '$password'") or die(mysql_error()); $row = mysql_fetch_array($sql); $_SESSION["status"] = $row["status"]; } ?> </head> <body> <center><img src="img/logo.png" height="100"> </b></h1></center> <hr /> </body> </html> 
  • where you have already sent the response body, after which you cannot add cookies. I understand that this is not all code, where something is included - Ale_x
  • on line 9, and you have 7 lines! the problem is not here - johniek_comp
  • Yes, no, I will add all the code. - Angus123
  • The problem is not here because I replaced the code a little, but both of them do not work and give the same error. - Angus123

2 answers 2

 <?php session_start(); $elseref = $_GET["ref"]; if ($elseref != '') { $val = "Хай"; setcookie("ref", $val); } require("config.php"); if (isset($_SESSION["username"]) && isset($_SESSION["password"])) { $username = $_SESSION["username"]; $password = $_SESSION["password"]; $sql = mysql_query("SELECT * FROM t_user WHERE username = '$username' and pass = '$password'") or die(mysql_error()); $row = mysql_fetch_array($sql); $_SESSION["status"] = $row["status"]; } ?> <html> <head> </head> <body> <center><img src="img/logo.png" height="100"> </b></h1></center> <hr /> </body> </html> 

and by the way, your code is vulnerable, I’ll go in and don’t know the password :) and read how the isset function works, you don’t use it correctly

  • And what's wrong there? PS error remained. - Angus123
  • The problem is solved, comrade :) - Angus123

You cannot use any echo , print , etc. before setcookie and header() . You get that

 echo "<html> <head>"; 

And then setcookie . Try setcookie shove up to <html> .

  • Did not help. - Angus123
  • Are you doing well with the sessions? Maybe they borohlyat? The second option: you can work with cookies on JS. If you ask, I can send the function. - atnartur