Reply to Re: Session/Cookie--To Create a "My Favorites" list

Your name:

Reply:


Posted by Hilarion on 12/22/05 16:56

> What I am trying to do is this:
> I want to have a link on each of several pages and a "My Favorites" section
> that displays on all of these pages so that if someone clicks on the "add to
> my favorites" link the current page will get added to the "My Favorites"
> section.
>
> I want this list to be persistent so that each time the same person comes
> back (of course from the same computer) that their favorites list remains
> there. I don't want to require users to have to log in first.
>
> How do I set something like this up? Should I use cookies only, cookies
> with session id's connected to a database, something else?
>
> Maybe it's just the end of the year wearing me out, but my brain is just not
> working around this problem at all. I started working with using just
> cookies, but then I had problems dealing with the additional links; say
> someone can keep 5 favorites, how do you deal with the 6th? Do you offer an
> interface to choose which to replace, or just bump the first one off of the
> list?


I'd not use session and store anything on the server side when you do not
even know if the user is going to come back. I'd go for cookies.
You do not have to limit the links count - use array (which means multiple
cookies, but that's not a problem).

<?php
// read all favorites from cookies
if (isset( $_COOKIE['favorites'] ) && is_array( $_COOKIE['favorites'] ))
$favorites = $_COOKIE['favorites'];
else
$favorites = array();

// Function which sets cookie for specified favorite.
function set_favorite( $favorites, $id, $remove = false )
{
if ($remove)
$expires = time() - 60*60*24*365*2; // expired 2 years ago
else
$expires = time() + 60*60*24*30; // expires in 30 days
$favorite = $favorites[$id];
setcookie(
'favorites['.$id.'][name]', // this makes it work as array on PHP side
strval( $favorite['name'] ), // store name
$expires,
'/', // accesible for all pages in the domain
'.domain.com', // include subdomains like 'www.domain.com' or 'my.domain.com'
0 // works not only with HTTPS but also HTTP
);
setcookie(
'favorites['.$id.'][address]',
$favorite['address'], // store address
$expires,
'/',
'.domain.com',
0
);
}

// Refresh cookies each time the user acceses your webpage
// and remove favorites selected for removal.
$num = 0;
foreach( $favorites as $id => $favorite )
{
if (isset( $_REQUEST['remove_favorite'][$id] ))
{
set_favorite( $favorites, $id, true );
unset( $favorites[ $id ] );
}
else
{
if (intval($id) > $num )
$num = intval($id);
set_favorite( $favorites, $id );
}
}
unset( $i );
unset( $favorite );

// add new favorite
if (isset( $_REQUEST['add_to_favorites'] ))
{
$num++;
$favorites[$num] = $_REQUEST['add_to_favorites'];
set_favorite( $favorites, $num );
}
unset( $num );

?>
<html>
<head>
<title>Your favorites</title>
</head>
<body>
<form
action="<?php echo htmlspecialchars( $_SERVER['REQUEST_URI'] ); ?>"
method="post"
>
<table>
<tbody>
<?php
foreach( $favorites as $id => $favorite )
{
?>
<tr>
<td>
<a
href="<?php echo htmlspecialchars( $favorite['address'] ); ?>"
>
<?php echo htmlspecialchars( $favorite['name'] ); ?>
</a>
<td>
<td>
<input
type="checkbox"
name="remove_favorite[<?php echo htmlspecialchars( $id ); ?>]"
value="Remove"
/>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<input type="submit" value="Remove selected favorites" />
</form>
<form
action="<?php echo htmlspecialchars( $_SERVER['REQUEST_URI'] ); ?>"
method="post"
>
Add current page to favorites as
<input type="text" name="add_to_favorites[name]" value="" />.
<input type="hidden" name="add_to_favorites[address]"
value="<?php echo htmlspecialchars( $_SERVER['REQUEST_URI'] ); ?>" />
<input type="submit" />
</form>
</body>
</html>


The above code is a page that displays stored favorites and allows to add
current page to favorites (which may not be a good idea).
The code which is before <html> and the code which is in second <form> should
be on each page (probably in some separate files included on each page,
first one at the very start of the script - before any output is sent,
and the second one in some visually appropriate place).
The code in first <form> should be where you want to show the
saved favorites (maybe on each page too, but in that case you should
only show first few entries and without the "remove favorites" mechanisms,
but with some link to "favorites management page"), maybe followed
by the code from the second <form>.
You could replace "action" attribute value for both <form> tags to
some specific page ("favorites management page"). I used 'REQUEST_URI'
to keep the user on the same page (it'll not work if the page got some
important data by "post" request and did not store it somewhere).


Hilarion

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация