SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Makeing a persistent cookie

    Hello all,

    I would like to know how to make a persistent cookie.

    I would like to use it on my entire website, the cookie should do the following:

    First (when there is no cookie yet).
    Show a .php page (ex: index.php).
    Then store a cookie that expires after one hour.
    When the cookie expired and visitors visit any of my webpages,
    I want it to show the index.php page again,
    and so on

    Hope anyone could help me out, thanks in advance!

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    There is plenty out there in google land. Just search for it!

  3. #3
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ginerjm View Post
    There is plenty out there in google land. Just search for it!
    Hey thanks for your reply, actually a have been using google but really didnt find much usefull.
    I searched for stuff like:"How to make a persistent cookie", maybe u can suggest some better keywords.
    I found this website when i was searching Google. =)

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What is your definition of a persistent cookie? Never heard the term before. Since cookies have an expiration date, YOu can make them last for quite some time or until th euser deletes them all. What do you want to achieve?

  5. #5
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ginerjm View Post
    What is your definition of a persistent cookie? Never heard the term before. Since cookies have an expiration date, YOu can make them last for quite some time or until th euser deletes them all. What do you want to achieve?
    Well thats what i found out about cookies here: http://www.allaboutcookies.org/cooki...-the-same.html
    What I want to achieve is in my first post.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, that is not a very persistent cookie. You just want a basic time-out cookie. So what's the difficulty you are having?

  7. #7
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ginerjm View Post
    Well, that is not a very persistent cookie. You just want a basic time-out cookie. So what's the difficulty you are having?
    I have no idea how to send it to the user, probably by a script on my webpage, and i have no idea how to make the cookie.

  8. #8
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    This should do the job (hope so anyway).

    This should go at the top of your pages. A better option would be to make a page called cookie.php, put the "cookie.php" code below in it and include it on every page.
    PHP Code:
    include ("cookie.php"); 
    Note: that this should be called before anything is sent to the browser.

    I set a "test" time of 10 seconds. Change it to 3600 when testing is done. The constant NUM_HOURS is set at 1 at the top of the page. Change as needed.

    Also note, this is a stand alone option that calls the function from the same page, which should work if all pages are within same directory as index.php.
    IF however you are using multiple directories where index.php is not located, then you should:

    1. Remove the part that CALLS the function and put it on each page adjusting the path to index.php accordingly.
    2. Remove the place where the path to the index.php is set to variable $SiteURL.

    How function is called(bottom of cookie.php)
    PHP Code:
    if (!isset($_COOKIE[COOKIE_KEY])){
        
    cookieLogin($SiteURL);

    cookie.php
    PHP Code:
    <?php  
        
    //Cookie.php
        ///////////////////////////////    
        /// Cookie Settings
        
    const COOKIE_KEY 'HomePageCookie';
        const 
    NUM_HOURS 1// number of hours key is valid
        ///////////////////////////////    
        /// Path to redirect page
        
    $SiteURL "index.php";    
        
    ///////////////////////////////    

        
    function cookieLogin($SiteURL) {
            
    // load cookie
               
    if (isset($_COOKIE[COOKIE_KEY])) {
                   
    $parts explode('|'$_COOKIE[COOKIE_KEY]);
        
                   
    $Token     $parts[0];
                   
    $sessionKey $parts[1];
               } else {
                   
    $Token $sessionKey false;
               }
                
               
    // No cookie at all
            
    if (empty($sessionKey) || empty($Token)) {
                
    /////////////////////////////////        
                 
    function generateKey() {        
                    
    // get the largest 999999999 we can rand to.
                    
    $max = (int)str_pad(''strlen(mt_getrandmax()) - 19);
                    
    $min = (int)str_pad('1'strlen($max), 0STR_PAD_RIGHT);
            
                    
    $key '';
                    
                    while (
    strlen($key) < 39) {
                        
    $key .= mt_rand($min$max);
                    }
                    
                    return 
    substr($key039);
                }
                
    /////////////////////////////////    
                 
    function generateToken() {        
                    
    // get the largest 999999999 we can rand to.
                    
    $max = (int)str_pad(''strlen(mt_getrandmax()) - 19);
                    
    $min = (int)str_pad('1'strlen($max), 0STR_PAD_RIGHT);
            
                    
    $key '';
                    
                    while (
    strlen($key) < 4) {
                        
    $key .= mt_rand($min$max);
                    }
                    
                    return 
    substr($key04);
                }
                
    /////////////////////////////////    
        
                
    $key generateKey();
                
    $Token generateToken();
                
                
    // Store key in cookie
                
    setcookie(COOKIE_KEY
                        
    "$Token|$key",       
                        
    //FOR TESTING set time for 10 seconds
                        
    time() + (10 NUM_HOURS));    
                        
    //ELSE USE 3600
                        //time() + (3600 * NUM_HOURS));
            
    header("location: $SiteURL");
            exit;
            }
        }

    if (!isset(
    $_COOKIE[COOKIE_KEY])){
        
    cookieLogin($SiteURL);
    }
    ?>
    You may not even need all that's here as I was using part of this in another program, saving keys and tokens in DB for match but it should do the job.

  9. #9
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Drummin View Post
    This should do the job (hope so anyway).

    This should go at the top of your pages. A better option would be to make a page called cookie.php, put the "cookie.php" code below in it and include it on every page.
    PHP Code:
    include ("cookie.php"); 
    Note: that this should be called before anything is sent to the browser.

    I set a "test" time of 10 seconds. Change it to 3600 when testing is done. The constant NUM_HOURS is set at 1 at the top of the page. Change as needed.

    Also note, this is a stand alone option that calls the function from the same page, which should work if all pages are within same directory as index.php.
    IF however you are using multiple directories where index.php is not located, then you should:

    1. Remove the part that CALLS the function and put it on each page adjusting the path to index.php accordingly.
    2. Remove the place where the path to the index.php is set to variable $SiteURL.

    How function is called(bottom of cookie.php)
    PHP Code:
    if (!isset($_COOKIE[COOKIE_KEY])){
        
    cookieLogin($SiteURL);

    cookie.php
    PHP Code:
    <?php  
        
    //Cookie.php
        ///////////////////////////////    
        /// Cookie Settings
        
    const COOKIE_KEY 'HomePageCookie';
        const 
    NUM_HOURS 1// number of hours key is valid
        ///////////////////////////////    
        /// Path to redirect page
        
    $SiteURL "index.php";    
        
    ///////////////////////////////    

        
    function cookieLogin($SiteURL) {
            
    // load cookie
               
    if (isset($_COOKIE[COOKIE_KEY])) {
                   
    $parts explode('|'$_COOKIE[COOKIE_KEY]);
        
                   
    $Token     $parts[0];
                   
    $sessionKey $parts[1];
               } else {
                   
    $Token $sessionKey false;
               }
                
               
    // No cookie at all
            
    if (empty($sessionKey) || empty($Token)) {
                
    /////////////////////////////////        
                 
    function generateKey() {        
                    
    // get the largest 999999999 we can rand to.
                    
    $max = (int)str_pad(''strlen(mt_getrandmax()) - 19);
                    
    $min = (int)str_pad('1'strlen($max), 0STR_PAD_RIGHT);
            
                    
    $key '';
                    
                    while (
    strlen($key) < 39) {
                        
    $key .= mt_rand($min$max);
                    }
                    
                    return 
    substr($key039);
                }
                
    /////////////////////////////////    
                 
    function generateToken() {        
                    
    // get the largest 999999999 we can rand to.
                    
    $max = (int)str_pad(''strlen(mt_getrandmax()) - 19);
                    
    $min = (int)str_pad('1'strlen($max), 0STR_PAD_RIGHT);
            
                    
    $key '';
                    
                    while (
    strlen($key) < 4) {
                        
    $key .= mt_rand($min$max);
                    }
                    
                    return 
    substr($key04);
                }
                
    /////////////////////////////////    
        
                
    $key generateKey();
                
    $Token generateToken();
                
                
    // Store key in cookie
                
    setcookie(COOKIE_KEY
                        
    "$Token|$key",       
                        
    //FOR TESTING set time for 10 seconds
                        
    time() + (10 NUM_HOURS));    
                        
    //ELSE USE 3600
                        //time() + (3600 * NUM_HOURS));
            
    header("location: $SiteURL");
            exit;
            }
        }

    if (!isset(
    $_COOKIE[COOKIE_KEY])){
        
    cookieLogin($SiteURL);
    }
    ?>
    You may not even need all that's here as I was using part of this in another program, saving keys and tokens in DB for match but it should do the job.
    Thank you verry much!
    Well i use a vBulletin forum, do i need to place it in every single .php file?

  10. #10
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Well you said you wanted them to be able to go to any page, so a link-to or I should say include should be on every page like so.
    PHP Code:
    include ("cookie.php"); 
        
    ///////////////////////////////    
        /// Path to redirect page
        
    $SiteURL "index.php";    
        
    ///////////////////////////////   
    if (!isset($_COOKIE[COOKIE_KEY])){
        
    cookieLogin($SiteURL);

    Note: Remove above lines from cookie.php


Bookmarks

Posting Permissions

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