简体   繁体   中英

How do I use cookies to store users' recent site history(PHP)?

I decided to make a recent view box that allows users to see what links they clicked on before. Whenever they click on a posting, the posting's id gets stored in a cookie and displays it in the recent view box.

In my ad.php, I have a definerecentview function that stores the posting's id (so I can call it later when trying to get the posting's information such as title, price from the database) in a cookie. How do I create a cookie array for this?

        **EXAMPLE:** user clicks on ad.php?posting_id='200'

     //this is in the ad.php
     function definerecentview()
     {

         $posting_id=$_GET['posting_id'];
         //this adds 30 days to the current time
         $Month = 2592000 + time();
         $i=1;
         if (isset($posting_id)){
                      //lost here
             for($i=1,$i< ???,$i++){             
                 setcookie("recentviewitem[$i]", $posting_id, $Month);
             }
         }
     }


     function displayrecentviews()
     {
        echo "<div class='recentviews'>";
        echo "Recent Views";
        if (isset($_COOKIE['recentviewitem'])) 
        {
            foreach ($_COOKIE['recentviewitem'] as $name => $value) 
            {
                echo "$name : $value <br />\n"; //right now just shows the posting_id
            }
        }
        echo "</div>";
     }

How do I use a for loop or foreach loop to make it that whenever a user clicks on an ad, it makes an array in the cookie? So it would be like..

1. clicks on ad.php?posting_id=200 --- setcookie("recentviewitem[1]",200,$month);
2. clicks on ad.php?posting_id=201 --- setcookie("recentviewitem[2]",201,$month);
3. clicks on ad.php?posting_id=202 --- setcookie("recentviewitem[3]",202,$month);

Then in the displayrecentitem function, I just echo however many cookies were set?

I'm just totally lost in creating a for loop that sets the cookies. any help would be appreciated

Don't set multiple cookies - set one that contains an array (serialized). When you append to the array, read in the existing cookie first, add the data, then overwrite it.

// define the new value to add to the cookie
$ad_name = 'name of advert viewed';

// if the cookie exists, read it and unserialize it. If not, create a blank array
if(array_key_exists('recentviews', $_COOKIE)) {
    $cookie = $_COOKIE['recentviews'];
    $cookie = unserialize($cookie);
} else {
    $cookie = array();
}

// add the value to the array and serialize
$cookie[] = $ad_name;
$cookie = serialize($cookie);

// save the cookie
setcookie('recentviews', $cookie, time()+3600);

You should not be creating one cookie for each recent search, instead use only one cookie. Try following this ideas:

  • Each value in the cookie must be separated from the other with an unique separator, you can use . , ; or | . Eg: 200,201,202

  • When retrieving the data from the cookie, if it exists, use explode(',',CookieName); , so you'll end up with an array of IDs.

  • When adding data to the cookie you could do, again, explode(',',CookieName); to create an array of IDs, then check if the new ID is not in the array using in_array(); and then add the value to the array using array_push(); . Then implode the array using implode(',',myString); and write myString to the cookie.

That's pretty much it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM