Nasıl kullanıcıların son site geçmişini (PHP) depolamak için çerezleri kullanabilirim?

2 Cevap php

Kullanıcıların daha önce tıkladım bağlantıları görmek için sağlayan yeni bir görünüm kutusunu yapmaya karar verdi. Bir gönderme tıkladığında, Gönderiyor kimliği bir çerez saklanır ve son görünümü kutu görüntüler alır.

Benim ad.php, ben Gönderiyor id depolayan bir definerecentview işleve sahip (yani örneğin veritabanından başlık, fiyat olarak ilanı bilgi almak için çalışırken ben daha sonra arayabilirsiniz) bir tanımlama. Bunun için nasıl bir tanımlama dizi oluşturabilirim?

        **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>";
     }

Nasıl bir kullanıcı bir reklama tıkladığı zaman, bu çerez bir dizi yapar bunu yapmak için döngü veya foreach döngüsü için kullanabilirim? Yani gibi olurdu ..

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);

Sonra displayrecentitem işlevi, sadece ancak birçok çerezler ayarlandı echo?

Ben sadece tamamen çerezleri sets döngü için bir oluştururken kayboldum. Herhangi bir yardım mutluluk duyacağız

2 Cevap

Birden fazla çerez koymayın - (tefrika) bir dizi içeren bir set. Eğer diziye eklemek zaman, önce mevcut çerez okunan veri eklemek, sonra üzerine.

// 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);

Bunun yerine sadece bir çerez kullanmak, her yeni arama için bir çerez yaratmak olmamalıdır. Bu fikirler aşağıdakileri deneyin:

  • Each value in the cookie must be separated from the other with an unique separator, you can use . , ; or |. E.g: 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.

Bu oldukça fazla bulunuyor.