Patch to allow anonymous users (uid = 0) have favorites on their own

stefan_sick - September 6, 2008 - 04:47
Project:Favorite Nodes
Version:5.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs work
Description

When your site is used by a majority of anonymous users you will notice that the favorite nodes block will overflow because all anonymous users count as one, the user with uid 0. So every guest will see every favorite set by every other guest.

The solution ist just a small patch that keeps track of favorites in the session of a guest user.

1. Function favorite_nodes_add

After the db querys add the following code

<?php

   
if ($user->uid == 0) {
       
$data->nid = $nid;
       
$data->title = $node->title;
       
$data->uid = $user->uid;
       
$data->last = time();

       
$sess_favorites = unserialize($_SESSION['sess_favorites']);
       
$sess_favorites[$nid] = $data;
       
$_SESSION['sess_favorites'] = serialize($sess_favorites);
    }
?>

2. function favorite_nodes_delete

After the db query add the following code

<?php
   
if ($user->uid == 0) {
       
$sess_favorites = unserialize($_SESSION['sess_favorites']);
        if (isset(
$sess_favorites[$nid])) {
            unset(
$sess_favorites[$nid]);
        }
       
$_SESSION['sess_favorites'] = serialize($sess_favorites);
    }
?>

3. function favorite_nodes_get

After the following lines

<?php
     
while ($data = db_fetch_object($result)) {
       
$row[$data->nid] = $data;
      }
?>

just add

<?php
   
if ($user->uid == 0) {
       
$sess_favorites = unserialize($_SESSION['sess_favorites']);
        foreach(
$row as $nid => $data) {
            if (
$sess_favorites[$nid] == $data) {
               
$filtered_row[$nid] = $data;
            }
        }
       
$row = $filtered_row;
    }
?>

This solution keeps the basic functionality unchanged - only the output for the guest user will be filtered to his own favorites set during his visit.

#1

kbahey - September 8, 2008 - 03:58
Version:5.x-1.2» 5.x-1.x-dev
Status:needs review» needs work

Please submit this a proper patch so it can be easily reviewed by others.

See http://drupal.org/patch for details.

Also, make it against 5.x-1.x-dev as well as the 6 dev version too.

#2

realworksmedia - January 13, 2009 - 12:51

I can confirm this worked for me with Drupal 5.7 and Favorite Nodes version 5.x-1.3.

Thanks for posting.

 
 

Drupal is a registered trademark of Dries Buytaert.