cookies wont work

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

okay i have these cookies that i am trying to set but when i run the script i get an error

Quote:
Warning: Cannot modify header information - headers already sent by (output started at /home/manicman/public_html/test/functions.php:2) in /home/manicman/public_html/test/login.php on line 23

Warning: Cannot modify header information - headers already sent by (output started at /home/manicman/public_html/test/functions.php:2) in /home/manicman/public_html/test/login.php on line 24


heres my script
PHP Code: [ Select ]
$user = ($_POST[user]);
 
$pass = ($_POST[pass]);
 
$checkuser = mysql_query("SELECT * FROM members WHERE username = '$user'");
 
if(mysql_num_rows($checkuser) <1) {
 
     echo "You have entered an invalid password/username.<br>
 
       <form action='login.php' method='post'>
 
<input name='user' type='text'>
 
<input name='pass' type='password'>
 
<input name='Submit' value='Login' type='button'>
 
</form>";
 
} else {
 
     echo "You have been successfully logged in.<br><a href='console.php'>Click here to go to your console</a>";
 
     //Sets a cookie to keep login info
 
    if(!$username) {
 
   $user = $usercook;
 
   $pass = $passcook;
 
}
 
setcookie("usercook", "$user",time()+3600,"/");  //line 23
 
setcookie("passcook", "$pass",time()+3600,"/"); //line 24
  1. $user = ($_POST[user]);
  2.  
  3. $pass = ($_POST[pass]);
  4.  
  5. $checkuser = mysql_query("SELECT * FROM members WHERE username = '$user'");
  6.  
  7. if(mysql_num_rows($checkuser) <1) {
  8.  
  9.      echo "You have entered an invalid password/username.<br>
  10.  
  11.        <form action='login.php' method='post'>
  12.  
  13. <input name='user' type='text'>
  14.  
  15. <input name='pass' type='password'>
  16.  
  17. <input name='Submit' value='Login' type='button'>
  18.  
  19. </form>";
  20.  
  21. } else {
  22.  
  23.      echo "You have been successfully logged in.<br><a href='console.php'>Click here to go to your console</a>";
  24.  
  25.      //Sets a cookie to keep login info
  26.  
  27.     if(!$username) {
  28.  
  29.    $user = $usercook;
  30.  
  31.    $pass = $passcook;
  32.  
  33. }
  34.  
  35. setcookie("usercook", "$user",time()+3600,"/");  //line 23
  36.  
  37. setcookie("passcook", "$pass",time()+3600,"/"); //line 24
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Cookies are sent in the header of the page. The headers are (normally) generated automagically by the server and sent <i>before the content</i>. As far as the server is concerned, the moment you use some html or an echo statement - that is the queue to sent the header. You must have set the cookies before you write any html page data.

You are going to have to juggle with the code and variables to get the cookies at the top of the page.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

PHP Code: [ Select ]
$user = ($_POST[user]);
 
$pass = ($_POST[pass]);
 
$checkuser = mysql_query("SELECT * FROM members WHERE username = '$user'");
 
if(mysql_num_rows($checkuser) <1) {
 
     $output = "You have entered an invalid password/username.<br>
 
        <form action='login.php' method='post'>
 
<input name='user' type='text'>
 
<input name='pass' type='password'>
 
<input name='Submit' value='Login' type='button'>
 
</form>";
 
} else {
 
     $output = "You have been successfully logged in.<br><a href='console.php'>Click here to go to your console</a>";
 
     //Sets a cookie to keep login info
 
     if(!$username) {
 
    $user = $usercook;
 
    $pass = $passcook;
 
   }
 
}
 
setcookie("usercook", "$user",time()+3600,"/");  //line 23
 
setcookie("passcook", "$pass",time()+3600,"/"); //line 24
 
echo $output
 
 
  1. $user = ($_POST[user]);
  2.  
  3. $pass = ($_POST[pass]);
  4.  
  5. $checkuser = mysql_query("SELECT * FROM members WHERE username = '$user'");
  6.  
  7. if(mysql_num_rows($checkuser) <1) {
  8.  
  9.      $output = "You have entered an invalid password/username.<br>
  10.  
  11.         <form action='login.php' method='post'>
  12.  
  13. <input name='user' type='text'>
  14.  
  15. <input name='pass' type='password'>
  16.  
  17. <input name='Submit' value='Login' type='button'>
  18.  
  19. </form>";
  20.  
  21. } else {
  22.  
  23.      $output = "You have been successfully logged in.<br><a href='console.php'>Click here to go to your console</a>";
  24.  
  25.      //Sets a cookie to keep login info
  26.  
  27.      if(!$username) {
  28.  
  29.     $user = $usercook;
  30.  
  31.     $pass = $passcook;
  32.  
  33.    }
  34.  
  35. }
  36.  
  37. setcookie("usercook", "$user",time()+3600,"/");  //line 23
  38.  
  39. setcookie("passcook", "$pass",time()+3600,"/"); //line 24
  40.  
  41. echo $output
  42.  
  43.  


just assign the string to be echoed to a variable and then once you have done the header manipulation echo the variable to output the string.

look above.

Another way to do it is output buffering

at the top of the page just put
PHP Code: [ Select ]
ob_start()
and at the bottom of the page
PHP Code: [ Select ]
ob_end_flush()


hope this works. I usually go with the first option I gave you as output buffering doesn't display anything until the page is completely finished loading.
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

>< didnt work :(
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

SpooF wrote:
>< didnt work :(


Check that your functions page is not outputting anything ie: echoing anything before you try to set the cookies

The error is being generated because the file 'functions.php' is sending content on line 2

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 61 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.