Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. trying to write a cookie

trying to write a cookie

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
phphtmldatabasequestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MacRaider4
    wrote on last edited by
    #1

    Subject is pretty self explanatory. This is what I have for code and then I'll try to explain it.

    <?php
    echo "Login page";
    ?>
    <form action="rememberMe.php" method="post">
    <div style="width: 30em;">
    <label for="name">Please Enter Your Name:</label>
    <input type="text" name="name" id="name" value="" />
    <br />
    <label for="location">In what location do you work<br />(Portland/Brewer)?</label>
    <input type="text" name="location" id="location value="" />
    <div style="clear: both;">
    <input type="submit" name="sendInfo" value="sendInfo" />
    </div>
    </div>
    </form>

    and then

    <?php

    $name = $_POST["name"];
    $location = $_POST["location"];
    $cookie = $name. "," . $location;
    //echo $cookie;

    setcookie( "thomasEmployee", $cookie, time() + 60 * 60 * 24 * 365), "/", ".linuxserver", false, true);

    if (isset($_COOKIE['thomasEmployee'])) {
    var_dump($_COOKIE);
    }
    else {
    header( 'Location: index.html' ) ;
    }

    ?>

    This is the page where I'm trying to "write" the cookie. The name I'm trying to use for the cookie is thomasEmployee, using the variable I created ($cookie) to hold the value, setting it for a year (though I'm sure it'll get deleted before then), and the domain (part I'm not sure about) is linuxserver. This is for a intranet site, the url for the indexpage is typed linuxserver/index.html so I'm guessing the domain is linuxserver? What I have right now doesn't seem to be writing anything as when I go to look for it, it's not there and when I try to get to my test page, it brings me right back to the login page.

    <?php
    if (isset($_COOKIE["thomasEmployee"])) {
    // var_dump($_COOKIE);
    }
    else {
    header( 'Location: login.php' ) ;
    }

    ?>
    <html>
    <head>

    That's the top of the page where I currently have it checking for the cookie. I'm sure it's something stupid, so thanks in advance!!!

    P T 2 Replies Last reply
    0
    • M MacRaider4

      Subject is pretty self explanatory. This is what I have for code and then I'll try to explain it.

      <?php
      echo "Login page";
      ?>
      <form action="rememberMe.php" method="post">
      <div style="width: 30em;">
      <label for="name">Please Enter Your Name:</label>
      <input type="text" name="name" id="name" value="" />
      <br />
      <label for="location">In what location do you work<br />(Portland/Brewer)?</label>
      <input type="text" name="location" id="location value="" />
      <div style="clear: both;">
      <input type="submit" name="sendInfo" value="sendInfo" />
      </div>
      </div>
      </form>

      and then

      <?php

      $name = $_POST["name"];
      $location = $_POST["location"];
      $cookie = $name. "," . $location;
      //echo $cookie;

      setcookie( "thomasEmployee", $cookie, time() + 60 * 60 * 24 * 365), "/", ".linuxserver", false, true);

      if (isset($_COOKIE['thomasEmployee'])) {
      var_dump($_COOKIE);
      }
      else {
      header( 'Location: index.html' ) ;
      }

      ?>

      This is the page where I'm trying to "write" the cookie. The name I'm trying to use for the cookie is thomasEmployee, using the variable I created ($cookie) to hold the value, setting it for a year (though I'm sure it'll get deleted before then), and the domain (part I'm not sure about) is linuxserver. This is for a intranet site, the url for the indexpage is typed linuxserver/index.html so I'm guessing the domain is linuxserver? What I have right now doesn't seem to be writing anything as when I go to look for it, it's not there and when I try to get to my test page, it brings me right back to the login page.

      <?php
      if (isset($_COOKIE["thomasEmployee"])) {
      // var_dump($_COOKIE);
      }
      else {
      header( 'Location: login.php' ) ;
      }

      ?>
      <html>
      <head>

      That's the top of the page where I currently have it checking for the cookie. I'm sure it's something stupid, so thanks in advance!!!

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      First guess is that you've run into the old "too late to send headers" problem. Quoting from the PHP manual: setcookie() defines a cookie to be sent along with the rest of the HTTP headers. 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. Note the last few words. A single space or newline can break your code. HTH Peter

      Software rusts. Simon Stephenson, ca 1994.

      M 1 Reply Last reply
      0
      • P Peter_in_2780

        First guess is that you've run into the old "too late to send headers" problem. Quoting from the PHP manual: setcookie() defines a cookie to be sent along with the rest of the HTTP headers. 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. Note the last few words. A single space or newline can break your code. HTH Peter

        Software rusts. Simon Stephenson, ca 1994.

        M Offline
        M Offline
        MacRaider4
        wrote on last edited by
        #3

        ok I have changed the code to this:

        <?php
        $name = $_POST["name"];
        $location = $_POST["location"];
        $cookie = $name. "," . $location;
        setcookie("thomasEmployee",$cookie,time() + 60 * 60 * 24 * 365,"/","linuxserver",false,true);
        header('Location: http://linuxserver/index.html');
        ?>

        I have also tried:

        setcookie("thomasEmployee",$cookie,time() + 31536000,"/","linuxserver",false,true);

        and

        setcookie("thomasEmployee",$cookie,time()+31536000,"/","linuxserver",false,true);

        but only this seems to work:

        setcookie("thomasEmployee",$cookie,time()+31536000);

        granted this is a intranet site so not a huge deal, but it would be nice to know why the rest isn't working. Also this will be available to all pages from this site correct? I'm pretty sure the answer is yes, but I'd like to be sure.

        P A L 3 Replies Last reply
        0
        • M MacRaider4

          ok I have changed the code to this:

          <?php
          $name = $_POST["name"];
          $location = $_POST["location"];
          $cookie = $name. "," . $location;
          setcookie("thomasEmployee",$cookie,time() + 60 * 60 * 24 * 365,"/","linuxserver",false,true);
          header('Location: http://linuxserver/index.html');
          ?>

          I have also tried:

          setcookie("thomasEmployee",$cookie,time() + 31536000,"/","linuxserver",false,true);

          and

          setcookie("thomasEmployee",$cookie,time()+31536000,"/","linuxserver",false,true);

          but only this seems to work:

          setcookie("thomasEmployee",$cookie,time()+31536000);

          granted this is a intranet site so not a huge deal, but it would be nice to know why the rest isn't working. Also this will be available to all pages from this site correct? I'm pretty sure the answer is yes, but I'd like to be sure.

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          MacRaider4 wrote:

          it would be nice to know why the rest isn't working.

          All I can suggest is adding the remaining optional parameters one at a time and see what happens with, say, Firebug, and a peek into the server logs.

          MacRaider4 wrote:

          Also this will be available to all pages from this site correct? I'm pretty sure the answer is yes, but I'd like to be sure.

          I'm pretty sure too. Suck it and see, in with the testing I suggested above. Cheers, Peter

          Software rusts. Simon Stephenson, ca 1994.

          1 Reply Last reply
          0
          • M MacRaider4

            Subject is pretty self explanatory. This is what I have for code and then I'll try to explain it.

            <?php
            echo "Login page";
            ?>
            <form action="rememberMe.php" method="post">
            <div style="width: 30em;">
            <label for="name">Please Enter Your Name:</label>
            <input type="text" name="name" id="name" value="" />
            <br />
            <label for="location">In what location do you work<br />(Portland/Brewer)?</label>
            <input type="text" name="location" id="location value="" />
            <div style="clear: both;">
            <input type="submit" name="sendInfo" value="sendInfo" />
            </div>
            </div>
            </form>

            and then

            <?php

            $name = $_POST["name"];
            $location = $_POST["location"];
            $cookie = $name. "," . $location;
            //echo $cookie;

            setcookie( "thomasEmployee", $cookie, time() + 60 * 60 * 24 * 365), "/", ".linuxserver", false, true);

            if (isset($_COOKIE['thomasEmployee'])) {
            var_dump($_COOKIE);
            }
            else {
            header( 'Location: index.html' ) ;
            }

            ?>

            This is the page where I'm trying to "write" the cookie. The name I'm trying to use for the cookie is thomasEmployee, using the variable I created ($cookie) to hold the value, setting it for a year (though I'm sure it'll get deleted before then), and the domain (part I'm not sure about) is linuxserver. This is for a intranet site, the url for the indexpage is typed linuxserver/index.html so I'm guessing the domain is linuxserver? What I have right now doesn't seem to be writing anything as when I go to look for it, it's not there and when I try to get to my test page, it brings me right back to the login page.

            <?php
            if (isset($_COOKIE["thomasEmployee"])) {
            // var_dump($_COOKIE);
            }
            else {
            header( 'Location: login.php' ) ;
            }

            ?>
            <html>
            <head>

            That's the top of the page where I currently have it checking for the cookie. I'm sure it's something stupid, so thanks in advance!!!

            T Offline
            T Offline
            Tony Wright UK
            wrote on last edited by
            #5

            We use the following to store the users ID when they logon... setcookie("UID", $_REQUEST['cuser'], time()+2592000, "/"); ...this allows us to remember a users ID, BUT it is only available after the page is reloaded. So after the above line you could add... header("Location: $http_url"); You could then check to see if it has been set. ...or you could push the value into the Session as well... $_SESSION['UID'] = $_REQUEST['cuser']; Hope this helps.

            1 Reply Last reply
            0
            • M MacRaider4

              ok I have changed the code to this:

              <?php
              $name = $_POST["name"];
              $location = $_POST["location"];
              $cookie = $name. "," . $location;
              setcookie("thomasEmployee",$cookie,time() + 60 * 60 * 24 * 365,"/","linuxserver",false,true);
              header('Location: http://linuxserver/index.html');
              ?>

              I have also tried:

              setcookie("thomasEmployee",$cookie,time() + 31536000,"/","linuxserver",false,true);

              and

              setcookie("thomasEmployee",$cookie,time()+31536000,"/","linuxserver",false,true);

              but only this seems to work:

              setcookie("thomasEmployee",$cookie,time()+31536000);

              granted this is a intranet site so not a huge deal, but it would be nice to know why the rest isn't working. Also this will be available to all pages from this site correct? I'm pretty sure the answer is yes, but I'd like to be sure.

              A Offline
              A Offline
              Al42
              wrote on last edited by
              #6

              Have you tried to change your code to this?

              1 Reply Last reply
              0
              • M MacRaider4

                ok I have changed the code to this:

                <?php
                $name = $_POST["name"];
                $location = $_POST["location"];
                $cookie = $name. "," . $location;
                setcookie("thomasEmployee",$cookie,time() + 60 * 60 * 24 * 365,"/","linuxserver",false,true);
                header('Location: http://linuxserver/index.html');
                ?>

                I have also tried:

                setcookie("thomasEmployee",$cookie,time() + 31536000,"/","linuxserver",false,true);

                and

                setcookie("thomasEmployee",$cookie,time()+31536000,"/","linuxserver",false,true);

                but only this seems to work:

                setcookie("thomasEmployee",$cookie,time()+31536000);

                granted this is a intranet site so not a huge deal, but it would be nice to know why the rest isn't working. Also this will be available to all pages from this site correct? I'm pretty sure the answer is yes, but I'd like to be sure.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                three suggestions: 1. read the documentation (it may be there), and use Google (others may have encountered the same problem). 2. the function has a return value; always check the return value when there is one, it is there for a reason. 3. the domain parameter seems to expect a domain name (or a partial one, starting with a period), not a host name. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                M 1 Reply Last reply
                0
                • L Luc Pattyn

                  three suggestions: 1. read the documentation (it may be there), and use Google (others may have encountered the same problem). 2. the function has a return value; always check the return value when there is one, it is there for a reason. 3. the domain parameter seems to expect a domain name (or a partial one, starting with a period), not a host name. :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  M Offline
                  M Offline
                  MacRaider4
                  wrote on last edited by
                  #8

                  Not quite a month since I've worked on this (this is a side project). Anyways, I think I'll have some time this week to get back to PHP and the web. I'll go through what you guys wrote and if I have any further questions or get it "working" I'll let you know. Thanks again!!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups