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
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
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
while ($data = db_fetch_object($result)) {
$row[$data->nid] = $data;
}
just add
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.
Comments
Comment #1
kbahey CreditAttribution: kbahey commentedPlease 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.
Comment #2
paulhudson CreditAttribution: paulhudson commentedI can confirm this worked for me with Drupal 5.7 and Favorite Nodes version 5.x-1.3.
Thanks for posting.