Results 1 to 9 of 9

Thread: Set and View Cookies

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Set and View Cookies

    I'm am having some trouble with cookies.

    I can set the cookie and view it on the same page, but I have not been able to get another page (in the same directory) to access the cookie.

    Here is my code:

    PHP Code:
    <?php
      $c_data 
    "[Your Name]";
      
      if (
    setcookie("cookietest"$c_data0"/"""0) == FALSE)
        {
          print 
    "There was an error while attempting to set the cookie.";
          exit(
    0);
        }
      else
        {
          print 
    "The cookie was set to $c_data successfully.<br>";
          
          if (isset(
    $_COOKIE["cookietest"]))
            {
              
    $c_data2 $_COOKIE["cookietest"];
              print 
    "I see the cookie as being set to $c_data2.";
            }
          print 
    "Click <a href=\"test.php\">HERE</a> to test the cookie on another page.<br>";
          exit(
    0);
        }
    ?>
    and here is the code on the test.php page

    PHP Code:
    <?
      if (isset($_COOKIE['cookietest']))
        {
          print "Are you " . $_COOKIE['cookietest'] . "?";
        }
      else
        {
          print "The cookie could not be found.";
        }
      exit (0);
    ?>
    Any ideas on why this is not working?

    Thanks,

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Set and View Cookies

    Try using sessions, they may be better in this case

    I have had simmilar problems with cookies in the past - they yusually involved the directory in which they are set and the reference to that directory but that code seems to be correct.

    Try sessions and see if they work, they are just as easy

    http://www.php.net/sessions

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Re: Set and View Cookies

    Well, the code I need to be setting the cookie/session in is not my own and it seems to already have a session started.

    Sessions terminate when the browser closes, don't they?

    and do they carry on if I were to have a link that opened another browser window?

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Set and View Cookies

    Actually I think they are terminated when the user's internet connection is reset e.g. when they disconnect and then reconnect.


    Cheers,

    Ryanj
    My Blog.

    Ryan Jones.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Re: Set and View Cookies

    I found some code on the net and basically... the sesson won't last between browsers unless I use the SID... so as long as I have that in my link i'll be ok

    but what about having multiple sessions inside of the same page?

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  6. #6
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Set and View Cookies

    Quote Originally Posted by squirrelly1
    I found some code on the net and basically... the sesson won't last between browsers unless I use the SID... so as long as I have that in my link i'll be ok

    but what about having multiple sessions inside of the same page?

    Squirrelly1

    You can do that the same way you can have multiple cookies

    Have a look at these:

    http://uk2.php.net/session
    http://www.oreilly.com/catalog/learn...apter/ch08.pdf

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Re: Set and View Cookies

    yep... i got it I just had to use session_name("whatever")

    i'm gonna play with this for a little while... so much easier than cookies!

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  8. #8
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Set and View Cookies

    Quote Originally Posted by squirrelly1
    yep... i got it I just had to use session_name("whatever")

    i'm gonna play with this for a little while... so much easier than cookies!

    Squirrelly1
    Yea, I agree 100% with that, cookies cna cause too many problems...

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  9. #9
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Set and View Cookies

    Just to clarify, the PHP session module sets a cookie on the client machine by default when the session_start() function is invoked. The difference between this cookie and the cookie you are/were trying to set, is that it only stores a session identifier.

    The session identifier is sent to your scripts and PHP uses this to get the session data, which is stored on the server and populate the $_SESSION array. This array can contain as many or as few varaibles as you wish. By default the session using cookies only lasts for the current browsing session, you can change this by modifying the session.cookie_lifetime directive.

    When to use sessions instead of cookies?
    • You want to store lots of different pieces of data, which would otherwise require multiple / large cookies.
    • You want to store a large amount of data which may put you over the quota per domain allocated to cookies.
    • You want to keep track of personal / sensitive data pertaining to the user. (as sessions are stored on the server, this data never gets stored on the client as a cookie)

    If you find yourself storing only a small piece of information then I would actually recommend a cookie, conversly, if you want to store data for a long time such as user details I would recommend a database.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width