I am making 2 cookies, but when they are set, it shows me this error

"Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\forum\index.php:4) in C:\xampp\htdocs\forum\index.php on line 81"

But I dont have any headers??

Here is the code

$expire=time()+60*60*24*30;
setcookie('username',$u,$expire);
setcookie('password',$p,$expire);

I made a login script, with a remember me feature

Here is the entire script

<?
session_start();
include("global.php");
echo "<link href='style.css' rel='stylesheet' type='text/css' />";
?>
<script type="text/javascript">
<!--
function logconfirm(){
var f = confirm("Are You Sure You Want To Logout?");
if(f){
return true;
}
else{
return false;
}
}

//-->
</script>
<title>Main Forums Page</title>
<div id="page">
<?
if($_SESSION['username']){
echo "<div id='user_info'>";
echo "<h6>Welcome ".$_SESSION['username']."!";
$result = mysql_query("SELECT `admin` FROM `users` WHERE `username` = '".$_SESSION['username']."'");
while($row = mysql_fetch_array($result)){
if($row['admin'] == 1){
echo "<br><a href='new_cat.php'>New Catagory</a>";
}
}
// set timeout period in seconds
$inactive = 900;

// check to see if $_SESSION['timeout'] is set
if(isset($_SESSION['timeout']) ) {
   $session_life = time() - $_SESSION['timeout'];
   if($session_life > $inactive)
        {
      // go to login page when idle
      session_unset();
      session_destroy();
   }
}

echo "<br>\n<a href='search.php'>Search Users</a><br>\n<a href='users.php'>Total Users</a><br>\n<a href='inbox.php'>Inbox</a><br>\n<a href='userchange.php'>Edit User Info</a>\n";
echo "<br>\n<a href='logout.php' onClick='return logconfirm()'>logout</a></h6>";
echo "</div>";
echo "<div id='page2'>\n";
echo "<h2>Categories</h2>\n";
echo "<hr size='1' width='75%'>\n";
$result0 = mysql_query("SELECT * FROM forum_cats ORDER BY date");
while($row = mysql_fetch_array($result0)){
echo "<a href='forums.php?id=".$row['id']."'>".$row['cat_name']."</a><br>Date Added: ".$row['date']."<hr size='1' width='50%'><br>\n";
}
echo "<br>\n";
echo "</div>";
}else{
echo "Welcome Guest! Please login or register to start viewing the categories, topics, and to start posting!<br>\n
<a href='register.php'>Register Here</a>";
$c1 = $_COOKIE["username"];
$c2 = $_COOKIE["password"];
?>
<br><br>
<table border=0 align='center'>
<form action='./index.php' method='post'>
<tr><td colspan="3" align="center" bgcolor="#333333"><font color="#ffffff">Login Form</font></td></tr>
<tr><td>Username:</td><td colspan='2'><input type=text name=user maxlength=20 value='<?php echo $c1; ?>'></td></tr>
<tr><td>Password:</td><td colspan='2'><input type=password name=pass maxlength=20 value='<?php echo $c2; ?>'></td></tr>
<tr><td align='left'>Remember </td><td align='center'><input type='checkbox' value='1' name='remember'></td><td align='right'><input type="submit" value="Login" name="submit2"/></td></tr>
</form>
</table>
<?
$rem = $_POST['remember'];
$sub = $_POST['submit2'];
$u = $_POST['user'];
$p = $_POST['pass'];
if($sub){
if($rem == 1){
$expire=time()+60*60*24*30;
setcookie('username',$u,$expire);
setcookie('password',$p,$expire);
}
$sql = mysql_query("SELECT count(id) FROM users WHERE username='$u' AND password='$p'");
$result = mysql_result($sql, 0);
if($result!=1){
print "<br>Invalid Login Information";
}else{
$result1 = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($result1)){
mysql_query("UPDATE users SET admin = '1' WHERE id = '1'") or die(mysql_error());
}
$_SESSION['username'] = $u;
$_SESSION['timeout'] = time();
mysql_query("UPDATE `users` SET `online` = 'Online' WHERE `username` = '".$_SESSION['username']."'") or die(mysql_error());
echo "<br>You are now logged in ".$_SESSION['username']."!";
}
}
}
?>

Recommended Answers

All 5 Replies

You can't have anything echoed to the page before the setcookie call, not even a newline.

How hard is it to read the manual? It has the answer to pretty much every question.

http://us3.php.net/setcookie (notice how the description has your answer, it would of taken a minute to find the solution)

How hard is it to read the manual? It has the answer to pretty much every question.

I find that a rather boorish response. The obvious answer is that it is not very difficult to read the manual, but the question should really be "how hard is it to understand the PHP manual if you are not very familiar with the language?"
I would say that the answer is "quite difficult and daunting".

Although the PHP manual is a great resource for those who are familiar with the language, the indepth wording of some phrases can be offputting to new users. Example: "Note that the value portion of the cookie will automatically be urlencoded when you send the cookie, and when it is received, it is automatically decoded and assigned to a variable by the same name as the cookie name. If you don't want this, you can use setrawcookie() instead if you are using PHP 5"

I feel it would have been much more pleasant, yet equally direct to say "This is explained in the manual at http://us3.php.net/setcookie. It is generally best to use the manual first, as it will often contain your answer and boost your understanding of PHP."

I find that a rather boorish response. The obvious answer is that it is not very difficult to read the manual, but the question should really be "how hard is it to understand the PHP manual if you are not very familiar with the language?"
I would say that the answer is "quite difficult and daunting".

Although the PHP manual is a great resource for those who are familiar with the language, the indepth wording of some phrases can be offputting to new users. Example: "Note that the value portion of the cookie will automatically be urlencoded when you send the cookie, and when it is received, it is automatically decoded and assigned to a variable by the same name as the cookie name. If you don't want this, you can use setrawcookie() instead if you are using PHP 5"

I feel it would have been much more pleasant, yet equally direct to say "This is explained in the manual at http://us3.php.net/setcookie. It is generally best to use the manual first, as it will often contain your answer and boost your understanding of PHP."

Everything is written plain English on that site. Nothing about it is hard to understand. Guess where I learned php? The php.net manual. If something is not clear research it. Don't know what urlencoded means, then search for it and it gives a nice understandable explaination. It just takes a little more time.

Its pointless to make a thread where the answer is just a minute away. Instead you spend more time waiting for a response, which is really just a waste of his/her time.

Here is how easy it is to find the correct answer:
1. Open a browser and go to http://www.google.com
2. Type in 'setcookie' and click search.
3. Click the first result which is usually the php.net manual.
4. Start reading and look there it is. "Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace."
If something isn't understood, then search for it using the steps above.

If I don't tell them to look into the php.net manual they will think its ok to post multiple pointless threads over problems that can be easily solved themselves. My hope is that they realize this and take the time to try and solve the problem.

If I don't tell them to look into the php.net manual they will think its ok to post multiple pointless threads over problems that can be easily solved themselves. My hope is that they realize this and take the time to try and solve the problem.

I understand the method behind what you said. I agree that in most cases it is better for someone to research the answer themselves through the manual. I just think that phrasing it as "How hard is it to read the manual?" might be interpreted as a hurtful comment, especially as they would not know the full context behind it. The original poster on this thread is relatively new to the forum and perhaps a more carefully worded response would have made the point equally well.

The manual is an excellent tool to experienced users of PHP who use it for understanding of how something works, but when you are new to it, long and complicated phrases can be offputting. I agree that sometimes you can find the answer much more quickly in the manual, and perhaps assertively explaining that to the creator of this thread would have also been helpful.

Ultimately this forum is here for people to post their question about PHP. I agree it's not the most elegant method for some questions, but it's the choice of the individual which resource they use.
We would probably use the manual or search online for the answer in other locations before posting here. Maybe this user tried that and just missed the answer. Maybe they didn't. The choice is theirs.

commented: Very well made point I agree with you ;) +1

Simon and you are both right, I agree the manual is the best place to find your answer and saves us allot of, well, typing. Nevertheless this is supposed to be a friendly community. Just some constructive criticism, don't take it personally.

@DealthRune, the manual, as Kkeith29 stated, has the answer to your question. If you ever need help with an ilfunctioning script your welcome to ask us for help and we will be happy to help, but first use google to find out if there is another issue like yours and most importantly check the manual.

If at the end of this 5 minute check there is nothing or you can't understand what the solution is then we will be happy to help.

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.