-
Cookie problem
Hello,
I am new to PHP (few week) and I cannot write cookie corectly without having message error.
Here is the error :
Warning: Cannot add header information - headers already sent by (output started at c:\program files\easyphp\www\nowlogin.php:18) in c:\program files\easyphp\www\nowlogin.php on line 58
Here is the code of my .php:
PHP Code:
<html>
<head>
<title>[ P ] atriot</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK href="patriot.css" rel="stylesheet" type="text/Css">
</head>
<body>
<?php
require_once("header.php");
AfficherEntete();
?>
<?php
// db variables
// inserts variables accordingly
$host = "myip";
$user = "allo";
$pass = "bye";
$db = "nothere";
$table = "boo";
//Connection a la base de donnee
$link = mysql_connect ($host, $user, $pass)
or die("Impossible de se connecter : " . mysql_error());
//Selection de la base utilisee
mysql_select_db($db);
//Prend les parametres
$varId=$_POST['User_id'];
//Prend le numéro de de l'équipe
$sql = 'SELECT usagers_nom FROM usagers WHERE usagers_id = ' .$varId;
//Execute l'ordre sql
$result = mysql_query($sql)
or die ("NO USER FOUND");
$numRow = mysql_num_rows($result);
if ($numRow > 0)
{
//Prend la valeur de retour
$Nom = mysql_result($result,0);
echo($Nom. '(#' .$varId.') is logged');
//Enregistre le cookies!!!!!
setcookie ("patriot_user_id" , $varId , time()+600);
}
else
{
echo('Loggin failed');
}
mysql_free_result($result);
// closing the db link
mysql_close ($link);
?>
</body>
</html>
-
I want to add the cookie only after verifying if the ID is correct in my database, I've read that it need to be before the HTML tag but I do not understand where to put it. Anyone can help me to modify my code, I've loss 2 days to try to put it work and it doesn't, I really need you!! :)
Daok
-
-
Hey Daok,
The setcookie() function MUST be called before any output is sent to the browser.
Mean no HTML tags, echo statements, or blank spaces can come before you call the function.
-
Try doing something like this:
Code:
<?php
// db variables
// inserts variables accordingly
$host = "myip";
$user = "allo";
$pass = "bye";
$db = "nothere";
$table = "boo";
//Connection a la base de donnee
$link = mysql_connect ($host, $user, $pass)
or die("Impossible de se connecter : " . mysql_error());
//Selection de la base utilisee
mysql_select_db($db);
//Prend les parametres
$varId=$_POST['User_id'];
//Prend le numéro de de l'équipe
$sql = 'SELECT usagers_nom FROM usagers WHERE usagers_id = ' .$varId;
//Execute l'ordre sql
$result = mysql_query($sql)
or die ("NO USER FOUND");
$numRow = mysql_num_rows($result);
if ($numRow > 0)
{
//Prend la valeur de retour
$Nom = mysql_result($result,0);
//Enregistre le cookies!!!!!
setcookie ("patriot_user_id" , $varId , time()+600);
?>
<html>
<head>
<title>[ P ] atriot</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK href="patriot.css" rel="stylesheet" type="text/Css">
</head>
<body>
<?php
require_once("header.php");
AfficherEntete();
echo($Nom. '(#' .$varId.') is logged');
}
else
{
echo('Loggin failed');
}
mysql_free_result($result);
// closing the db link
mysql_close ($link);
?>
</body>
</html>
-
BIG BIG BIG THANKS to you man !
Thx Hobo!!!!