just wanna ask..if session cant be use, i try setcookies() to get the variable pass through the pages, however i get this error message:

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\VertrigoServ\www\logincheck.php:2) in C:\Program Files\VertrigoServ\www\logincheck.php on line 3

i put setcookie($username, time()+3600); right after the mysql_connect(), and below it juz a normal function to differentiate between user and admin...
juz curious what is the mistake here and how can i declare the code? Thanks so much for helping..

Dani AI

Generated

Short, practical diagnosis: the warning means PHP sent output before you tried to set headers/cookies. was right to check for whitespace before <?php — invisible output (leading spaces, UTF‑8 BOM) or any echo/HTML will trigger this. Use headers_sent() to locate the file/line or enable output buffering as a temporary workaround. (php.net)

Three concrete problems in 's snippet and how to fix them:

  • The cookie call runs before $username is assigned — the name/value will be wrong. Assign variables first.
  • The setcookie() parameter order matters: name, value, expiry, path, domain, secure, httponly. Set the cookie with a literal name (for example "username") so $_COOKIE['username'] will contain the value on the next request.
  • Cookies set by setcookie() are only available to PHP on the next HTTP request (the browser must receive the Set-Cookie header and send it back). To use the value immediately either set $_COOKIE['username'] = $username; in the same script or redirect the user and read the cookie on the landing page. Example pattern:
<?php
// after validating credentials and setting $username
setcookie('username', $username, time() + 3600, '/');
$_COOKIE['username'] = $username; // optional for same-request use
header('Location: claim_try.php');
exit;
?>

See the PHP manual for the full signature and notes about next-request behavior. (php.net)

Quick debugging and security checklist: run headers_sent($file,$line) to find premature output; save files as UTF-8 without BOM and remove the closing ?> in pure-PHP files to avoid trailing whitespace; inspect cookies with the browser DevTools (Application/Storage) or print_r($_COOKIE) on the next page; set path='/' so the cookie is site-wide; use Secure and HttpOnly flags for sensitive cookies and avoid storing passwords—prefer server sessions or signed tokens per OWASP guidance. (php.net)

Recommended Answers

All 4 Replies

Do you have any html tag or any echo statement ? Can you post your code ?

hey..is you..hihi...
well, i setcookies() right after the user had logged in and the variables pass to the page shown below, i put it at the top right after <?php.....

<?php
setcookie($username, time()+3600);
mysql_connect("localhost", "root", "1234") or die(mysql_error());
mysql_select_db("userlist") or die(mysql_error());
$username=$_POST['username'];
$password=$_POST['password'];
$query = "SELECT * FROM user where username='$username' && password='$password'"; 
$result = mysql_query($query) or die(mysql_error());
if ($username=="admin" && $password=="admin1234")
{
	echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />";
	echo "<center><h1>Administrator logged in!!!</h1></center>";
	echo "<center><h1>Please wait to be directed to the administrator page.....</h1></center>";
	echo "<meta http-equiv=\"refresh\" content=\"5;url=trytolink.php\">";
}
else if(mysql_num_rows($result) > 0)
	{
	echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />";
	echo "<center><h1>You had sucessfully logged in as ".$username ."</h1><br/></center>";
	echo "<center><h1>Please <a href='registration.php'>Click here</a> to update your details or 
	<a href='claim_try.php'>Click here</a> to the claim page.</h1></center>";
}

else
{
	echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />";
	echo "<center><h1>Sorry! Your username and password is incorrect!!! Please try again!!!</h1></center>";
	echo "<meta http-equiv=\"refresh\" content=\"5;url=index3.php\">";
}
?>
<html>
<head>
<title>::. ISO CLAIM PAGE</title>
<link rel="stylesheet" type="text/css"
href="my.css">
</head>
<body>
</body>
</html>

There must be a whitespace before <?php . Check it out ! There shouldn't be any whitespace, any echo statements or any html tags before setcookie! :)

oh..thanks, i not even notice about that also. So the cookies had been set with $username and timeout 1 hour.....Then from this page, i click to another page, using the same syntax for set cookies () declared at the top before any header or echo....and again i try to retrieve my username so that it can show on the page, however i found that it is blank.

Here is the code i use to show the $username out:

......
<form method="post" action="claim_try2.php">
		<div><label>Name:<?php echo $_COOKIE["username"];?></label><br></div>
			<div><label for="type">Department:</label>
				<select name="department">
					<optgroup label="---SELECT---">
										<option>KKK</option>														<option>KEE</option>										<option>KPM</option>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.