Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Cookies

  1. #1
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Cookies

    This thread is intended to be a tutorial, or article if you will. It is not ment to be some sort of example requiring comments on scurity or whatever, it is part of PHP and people may seek to use it with whatever they want. I am mearly showing them how they can use cookies if they choose to. If they are looking for something more secure I'm sure they'll seek you out.

    If this is not the right forum for a such a post just delete it. It isn't much of a snippet.

    To begin with. We will go over how to set a cookie.

    PHP Code:

    // Its good to use pseudo code to remember what your doing.
    $cookie_name"hello_test"// This is the cookies name which will be used to call it back.
    $cookie_data "Hello World!"// This is the content that will be put inside our cookie
    $expiration_time time()+3600// Set the expiration time: +3600 seconds equals 1 hour on current time.
    setcookie($cookie_name$cookie_data$expiration_time); // Now we'll set the cookie! 
    When we use cookies, we already are assuming the visitor using this script has cookies enabled. Though some people arn't aware of requirments until it is too late. To get around this we'll make sure the cookie is set before we do anything else.

    PHP Code:

    // Add our variables for the cookie above.
    if(!(setcookie($cookie_name$cookie_data$expiration_time))) { // If setcookie() is false, else continue.
      
    die("You <b>must</b> have cookies enabled to continue running this program further!"); // Return error and exit program.

    Next we'll take a look at calling the data.

    PHP Code:

    if(isset($_COOKIE['hello_test'])) { // Check to see if our cookie we made exists
      
    echo $_COOKIE['hello_test']; // Should echo the cookie data.

    That is the more simpler grasp of cookies. Another tip is if you want to have multiple information in cookies, compile it into one. For example:

    PHP Code:

    // Its good to use pseudo code to remember what your doing.
    $cookie_name"hello_test"// This is the cookies name which will be used to call it back.
    $var1 "Hello";
    $var2 "World!";
    $cookie_data $var1 "|" $var2// This is the content that will be put inside our cookie. Seperated by '|' for later use.
    $expiration_time time()+3600// Set the expiration time: +3600 seconds equals 1 hour on current time.
    if(!(setcookie($cookie_name$cookie_data$expiration_time))) { // If setcookie() is false, else continue.
      
    die("You <b>must</b> have cookies enabled to continue running this program further!"); // Return error and exit program.

    Too call this information and add it to seperate variables we'll use the fallowing example:

    PHP Code:

    if(isset($_COOKIE['hello_test'])) { // Make sure our cookie exists.
      
    $cookie_info explode("|"$_COOKIE['hello_test']); // Explode the cookie by '|' into an array.
      
    echo $cookie_info[0] . "&nbsp;" $cookie_info[1]; // $cookie_info[0] = "Hello" and $cookie_info[1] = "World!" with a result of "Hello World!"

    Thats all I have time for right now. Be sure to see also setcookie() for more information and methods.
    Last edited by Element; 11-30-2005 at 08:08 PM.

  • #2
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    This looks appropriate for this forum, and I'm sure many people will find it useful as well.
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #3
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by WA
    This looks appropriate for this forum, and I'm sure many people will find it useful as well.
    Thank you, and I hope so. It just might if I don't get forum bashed with security nerds. Just gotta play it cool, bre. lol.


  •  

    Posting Permissions

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