set_cookie() uses the headers sent by an HTTP request. This is sent when the page first starts outputting data.

Therefore, you cant have any echo's, prints etc before this function.

Also make sure for spaces at the start of pages.

for example if the following was how your page started it would have a problem, note the space before <?php

' <?php'

    wangsc66 wrote:

    I have to output something before setting the cookie.

    Why? I haven't found a situation yet where I have to output anything before headers, it is just a matter of designing the code properly.

    The code you posted is what happens after the cookie, that doesn't matter the least. It is what happens before that you need to change.

      Piranha wrote:

      Why? I haven't found a situation yet where I have to output anything before headers, it is just a matter of designing the code properly.

      The code you posted is what happens after the cookie, that doesn't matter the least. It is what happens before that you need to change.

      hmmm...

      i change it to something like below, but still not working. 🙁

      <html>
      <head><title>REVOLUTION II - Sign In</title>
      <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
        <LINK HREF="../css/default.css" REL="stylesheet" TYPE="text/css">
        <style type="text/css">
        </style>
      </head><body>
      <?php
      if(isset($_POST['login']))
      {
      include 'config.php';
      include 'opendb.php';
      $username = $_POST['username'];
      $password = $_POST['password'];
      $usercheck = $_POST['username'];
      $_POST['username'] = stripslashes($_POST['username']);
      $hour = time() + 3600;
      setcookie(ID_my_site, $_POST['username'], $hour);
      ...
      ?>
        <html>
        <head><title>REVOLUTION II - Sign In</title>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
          <LINK HREF="../css/default.css" REL="stylesheet" TYPE="text/css">
          <style type="text/css">
          </style>
        </head><body> 

        Lots of output sent before setcookie()

          Anything that will be visible when you look at the source code and is before the set_cookie is output.

          If you are really determined to output before the function call;

          http://uk3.php.net/set_cookie

          http://uk3.php.net/set_cookie wrote:

          Note: As of PHP 4, you can use output buffering to send output prior to the call of this function, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive on in your php.ini or server configuration files.

          However I wouldn't recommend it. I've never actually tried it before to be honest so I hope I'm right in what I'm saying. What this does is it executes the whole page and prepares it before anything is sent.

            laserlight wrote:
            <html>
            <head><title>REVOLUTION II - Sign In</title>
            <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
              <LINK HREF="../css/default.css" REL="stylesheet" TYPE="text/css">
              <style type="text/css">
              </style>
            </head><body> 

            Lots of output sent before setcookie()

            ok, i got the idea.

            I removed the output before setcookie(), but another question is where to add those
            removed code?

              Anywhere after you call set_cookie

              <?php
              if(isset($_POST['login']))
              {
              include 'config.php';
              include 'opendb.php';
              $username = $_POST['username'];
              $password = $_POST['password'];
              $usercheck = $_POST['username'];
              $_POST['username'] = stripslashes($_POST['username']);
              $hour = time() + 3600;
              setcookie(ID_my_site, $_POST['username'], $hour);
              ?>
              
              <html>
              <head><title>REVOLUTION II - Sign In</title>
              <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
                <LINK HREF="../css/default.css" REL="stylesheet" TYPE="text/css">
                <style type="text/css">
                </style>
              </head><body>
              <?php
              
              // ..............
              
              ?>

                You need to pick a good spot after the setcookie() call, when you know that all processing is done and all that is left is to display the page.

                  dougal85 wrote:

                  Anywhere after you call set_cookie

                  <?php
                  if(isset($_POST['login']))
                  {
                  include 'config.php';
                  include 'opendb.php';
                  $username = $_POST['username'];
                  $password = $_POST['password'];
                  $usercheck = $_POST['username'];
                  $_POST['username'] = stripslashes($_POST['username']);
                  $hour = time() + 3600;
                  setcookie(ID_my_site, $_POST['username'], $hour);
                  ?>
                  
                  <html>
                  <head><title>REVOLUTION II - Sign In</title>
                  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
                    <LINK HREF="../css/default.css" REL="stylesheet" TYPE="text/css">
                    <style type="text/css">
                    </style>
                  </head><body>
                  <?php
                  
                  // ..............
                  
                  ?>

                  i got it! \o/

                    err... i need help again~~~ how to echo the value of the cookie in another php file? i tried like below, but not working... 🙁

                    <html>
                    <head><title></title>
                    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
                      <LINK HREF="css/default.css" REL="stylesheet" TYPE="text/css">
                      <style type="text/css">
                      </style>
                    </head><body>
                    <?php
                    echo "<br><br>";
                    echo "<TABLE WIDTH='752' BORDER='0' ALIGN='center' CELLPADDING='0' CELLSPACING='0' HEIGHT='142' BGCOLOR='WHITE'><tr>";
                    echo "	  <TD width='752' HEIGHT='142'><a href='../adminuser.php' target='_self'><img src='img/logo.jpg' width='750' height='140' class='border'></a></TD>";
                    echo "	</TR>";
                    echo "</TABLE><br><br>";
                    echo "<table width='264'  border='0' align=center cellpadding='3' cellspacing='0' class=mainouter>";
                    echo "<tr><td width='283' align='center' class='outer' style='padding-top: 20px; padding-bottom: 20px'>";
                    echo "<table width='275' border='0' align='center' cellpadding='5'>";
                    echo "<tr><td>";
                    
                    include 'library/config.php';
                    include 'library/opendb.php';
                    
                    echo $_COOKIE["ID_my_site"];
                    
                    include 'library/closedb.php';
                    ?>
                    </body></html>
                      
                      setcookie(ID_my_site, $_POST['username'], $hour);
                      

                      Don't think your setting it correctly. try;

                      
                      setcookie("ID_my_site", $_POST['username'], $hour);
                      
                        dougal85 wrote:
                        
                        setcookie(ID_my_site, $_POST['username'], $hour);
                        

                        Don't think your setting it correctly. try;

                        
                        setcookie("ID_my_site", $_POST['username'], $hour);
                        

                        I think it's not the problem. I added echo $_COOKIE['ID_my_site']; after the setcookie function and it works doesn't matter if there's a quotation mark.

                        But it's not working when i added it to another php file. why~~?

                          wangsc66 wrote:

                          doesn't matter if there's a quotation mark.

                          Actually, it does; but if you've got error reporting turned down you'll be missing all those notices from PHP telling you it's a Bad Thing.
                          If you're doing this on your computer, set error_reporting in php.ini to E_ALL. If you're using someone else's (or this is live, but who would be building a site there?) then use the [man]error_reporting[/man] function to do the same thing.

                          As for setcookie not working, note that because you're not specifying a path then the page where you're setting it and the page where you're trying to view it will need to be in the same directory (more accurately, their URLs have to be identical up to the end of the path). Are they?

                            Also not that using [man]setcookie/man sends the cookie to the user's browser.

                            This does not add the data to the $COOKIE array; this array is only populated with data sent from the browser. So, you should only expect to see data successfully added to the $COOKIE as early as the next link/page the user visits (at which point the browser will send the cookie information it just stored from the last page).

                            EDIT: In addition to Weedpacket's explanation of the path parameter, you must also be sure that the page you're expecting to see the cookie appear on is within the same domain AND subdomain as the page that set the cookie. For example, a cookie set by http://yoursite.com/page.php will not, by default, be accessible by a script located at http://www.yoursite.com/page2.php since the two are on different subdomains.

                              ok guys, i got it work finally. Thank you very much for the help! 🙂

                                Write a Reply...