Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: learning teh PHP, cookie help

  1. #1
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default learning teh PHP, cookie help

    alright, im learning the basics of php, and im trying out cookies

    so far, i have this:
    Code:
    <?php
      setcookie("fname", name, 60);
     ?>
    
    
    <html>
    <body>
    <center>
    <title>loltest</title>
    
    
    <form action="phpcookieform.php" method="post">
    Name: <input type="text" name="name" />
    Age: <input type="text" name="age" />
    <input type="submit" />
    
    </form>
    
    <?php
      if (isset($_COOKIE["fname"]))
        echo "Hello" . $_COOKIE["fname"] . "!<br />";
      else
      echo "Hello Guest!"; ?>
    </center>
    </body>
    </html>
    the problem is it either doesnt save the cookie, or it doesnt load it correctly

    it should be self explanitory, a form tells you to put in your name, and it saves it to a cookie. then it should echo your name, when ever you go to the page( i believe ). i also read a tut somewhere saying that saving the cookie should be above all HTML tabs, it seems to work because i got an error when i didnt. im a noob at this so it should be very obvious .

    and if your wondering, the web page is here

    thanks
    Last edited by Awkwardsaw; 07-13-2009 at 09:03 AM.
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

  2. #2
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    php Code:
    setcookie("fname", name, 60);
    Shouldn't it be
    Code:
      setcookie("fname", $_POST['name'], 60);

  3. #3
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    thats not what the tut's said (2 of them)

    i'll try it out

    e: nope
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

  4. #4
    Join Date
    Dec 2008
    Posts
    209
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Meh clean up the code please?

    PHP Code:
    // begin: phpcookieform.php

    <?php
      setcookie
    ('fname'name60);
    ?>

    // end: phpcookieform.php

    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
      "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
      <head>
        <title>loltest</title>

        <style type="text/css">
          body { 
            margin: 0 auto;
          }

          label {
            display: block;
          }
        </style>
      </head>

      <body>
        <form action="phpcookieform.php" method="post">
          <fieldset>
            <legend>phpcookieform</legend>

            <label for="name">
              <span>Name: </span>
              <input id="name" name="name" type="text" />
            </label>

            <label for="age">
              <span>Age: </span>
              <input id="age" name="age" type="text" />
            </label>

            <input type="submit" />
          </fieldset>
        </form>

        <?php
          
    if (isset($_COOKIE['fname'])) {
            echo 
    '<p>Hello'.$_COOKIE['fname'].'!</p>';
          } else {
            echo 
    'Hello guest!';
          }
        
    ?>
      </body>
    </html>
    Also, I believe that the problem is that you specified no type attribute for name/age.

    Note, my code is xhtml 1.1, and thus a xml declaration is at the top of the file - turn short tags off!
    ^ Also, xhtml is not for presentation, so I fixed that up with css ;]
    Last edited by Craig`; 07-14-2009 at 02:42 AM.

  5. #5
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

  6. #6
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    1 hour = 3600.

    It is in seconds Shut .

  7. #7
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

  8. #8
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    no, it was only suposed to be for 1 min

    and math, xhtml is something i still have to learn. and maby css
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

  9. #9
    Join Date
    Feb 2007
    Location
    Access Violation at 0x00000000
    Posts
    2,865
    Mentioned
    3 Post(s)
    Quoted
    18 Post(s)

    Default

    PHP Code:
    <?php
      
    if ($_POST["go"])
      {
        
    setcookie("fname"$_POST["name"], 60);
      }
    ?>

    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
      "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
      <head>
        <title>loltest</title>
        <style type="text/css">
          body { 
            margin: 0 auto;
          }

          label {
            display: block;
          }
        </style>
      </head>

      <body>
        <form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
          <fieldset>
            <legend>phpcookieform</legend>

            <label for="name">
              <span>Name: </span>
              <input id="name" name="name" type="text" />
            </label>

            <label for="age">
              <span>Age: </span>
              <input id="age" name="age" type="text" />
            </label>

            <input type="submit" name="go" />
          </fieldset>
        </form>

        <?php
            
    if (isset($_COOKIE["fname"]))
            {
                echo 
    "<p>Hello "$_COOKIE['fname'] ."!</p>";
            }
            else
            {
                echo 
    "Hello guest!";
            }
        
    ?>
      </body>
    </html>
    Long time no PHP >.>
    Ce ne sont que des gueux


  10. #10
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Floris,
    Parse error: syntax error, unexpected T_STRING in /home/nsyedcom/public_html/awkwardsaw/phpcookieform.php on line 8.

  11. #11
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    how would you do it without xhtml? =\
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

  12. #12
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    XHTML is not even required lol. You can use noob HTML when just testing .

  13. #13
    Join Date
    Feb 2007
    Location
    Access Violation at 0x00000000
    Posts
    2,865
    Mentioned
    3 Post(s)
    Quoted
    18 Post(s)

    Default

    True that.
    setcookie is just broken. I tried a simple:
    PHP Code:
    <?php
        setcookie
    ("foo""bar"3600);
        if (isset(
    $_COOKIE["foo"]))
        {
            echo 
    $_COOKIE["foo"];
        }
        else
        {
            echo 
    "Epic failz.";
        }
    ?>
    Run it?
    Ce ne sont que des gueux


  14. #14
    Join Date
    Oct 2006
    Location
    ithurtsithurtsithurtsithurts
    Posts
    2,930
    Mentioned
    7 Post(s)
    Quoted
    135 Post(s)

    Default

    setcookie works just fine. The problem is the cookie you're trying to set has an expiry time set in the past. Expiry time is set as a number of seconds since the epoch. Change this:
    PHP Code:
    <?php
      setcookie
    ("fname"name60);
     
    ?>
    To this:
    PHP Code:
    <?php
      setcookie
    ("fname"nametime() + 60);
     
    ?>

  15. #15
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    i will try when im at the right cpu, unless Da 0wner wants to
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

  16. #16
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Sure, I'll do it. Btw, I love you senrath. That seems like it should work .

  17. #17
    Join Date
    Feb 2007
    Location
    Access Violation at 0x00000000
    Posts
    2,865
    Mentioned
    3 Post(s)
    Quoted
    18 Post(s)

    Default

    I think senrath might be true... *goes to php.net*

    EDiT:
    f*** me lol:
    The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
    Ce ne sont que des gueux


  18. #18
    Join Date
    Oct 2006
    Location
    ithurtsithurtsithurtsithurts
    Posts
    2,930
    Mentioned
    7 Post(s)
    Quoted
    135 Post(s)

    Default

    Glad I could help.

  19. #19
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Senrath, http://awkwardsaw.da0wner.info/phpcookieform.php.

    It still doesn't work .

    Quote Originally Posted by Floor66 View Post
    I think senrath might be true... *goes to php.net*

    EDiT:
    f*** me lol:
    The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
    So if it is 0, it's basically a session variable..?

  20. #20
    Join Date
    Feb 2007
    Location
    Access Violation at 0x00000000
    Posts
    2,865
    Mentioned
    3 Post(s)
    Quoted
    18 Post(s)

    Default

    That's what php.net says :P
    Ce ne sont que des gueux


  21. #21
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    da owner, yeah it works =p

    you just need to add a space to the greeting. which is no problem

    and thanks to every one
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

  22. #22
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Oh, it's been working I guess.

    I just had to clear my cache .

  23. #23
    Join Date
    Dec 2008
    Posts
    209
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    just to mention: XHTML is the way foward ;]

  24. #24
    Join Date
    Feb 2007
    Location
    Access Violation at 0x00000000
    Posts
    2,865
    Mentioned
    3 Post(s)
    Quoted
    18 Post(s)

    Default

    Yeah I always make my things verified.
    Ce ne sont que des gueux


  25. #25
    Join Date
    Dec 2008
    Location
    In a galaxy far, far away...
    Posts
    584
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    You should use sessions, they are more safer for quick browsing. Cookies are better when you want to save information onto the user's computer to regain when they visit the website again.

    PHP Code:
    <?php
      
    if (isset($_POST["savecookie"])) //
        
    setcookie("fname"$_POST["name"], 60);
     
    ?>


    <html>
    <body>
    <center>
    <title>loltest</title>


    <form action="phpcookieform.php" method="post">
    Name: <input type="text" name="name" />
    Age: <input type="text" name="age" />
    <input type="submit" name="savecookie" />
    </form>

    <?php
      
    if ($_COOKIE["fname"]) //
        
    echo "Hello" $_COOKIE["fname"] . "!<br />";
      else
        echo 
    "Hello Guest!"
    ?>
    </center>
    </body>
    </html>


    ~NS

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •