Cookie preferences: Manage site user preferences stored in cookies

Recommend this page to a friend!
  Info   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 57%Total: 1,402 All time: 2,765 This week: 209Up
Version License Categories
cookie_preferences 1.0.0GNU General Publi...HTTP, User Management
Description Author

This class is meant to store and retrieve preference values of an user of a site using cookies.

It can be used to store any kind of site user access context value, not only site usage preferences, but also other things like items added by an anonymous user to a shopping cart.

The preferences values are stored in an obfuscated format using the ROT12 algorithm to make it a little more difficult for curious users to understand what is being stored.

If a preference can not be stored or retrieve, eventually because the user browser is not accepting cookies, the class will just output a PHP Notice, which can be turned on or off in your app by using PHP error reporting control functions.

Picture of Ken Stanley
Name: Ken Stanley <contact>
Classes: 2 packages by
Country: United States United States
Age: 43
All time rank: 1370189 in United States United States
Week rank: 523 Up59 in United States United States Up


Don't let the name fool you, cookie_preference is merely an API
aimed at allowing you, the web developer, to easily implement
user preferences (either directly, or indirectly) as quickly
as possible. To do this I've developed this class, and even tested
it on my own shopping cart.

It's not a very complex script to implement, and it's error
handling is at a minimum, allowing you the developer to be more
elequent. If a preference can not be set, read, or whatever,
the class will just output a PHP Notice, which can be turned
off in your app by using error_reporting(E_ALL & ~E_NOTICE).

API Reference:

void cookie_preference ( [preference_name[, cookie_expiry[, cookie_secure[, rot13]]]] )

	Yes, this is the contructor, but it is worth mentioning
	because you can set up your default environment by passing
	certain arguements when creating your class object.
	preference_name is the name of the cookie that holds your preferences.
	cookie_expiry is the length of time you want your cookie to live.
	cookie_secure is for when setting secure cookies over HTTPS.
	rot_13 is to obfuscate your cookie using the rot13 encoding (has automatic support for PHP <4.2.0)

	$prefs = new cookie_preferences("my_prefs", time()+3600, 1, true);

bool preference_new ( preference_name, preference_value )

	Sets a new preference to your cookie, but only if it doesn't already exist. Returns
	TRUE on success, or FALSE on failure.

	if ($prefs->preference_new('foo', 'bar') === FALSE)
		trigger_error("Preference foo already exists");

bool preference_update ( preference_name, preference_value )

	Updates an already existing cookie with a new value.

	if ($prefs->preference_update('foo', 'bar') === FALSE)
		trigger_error("Could not update preference foo");

string preference_get ( preference_name )

	Returns the value of preference_name on success, and FALSE on failure.

	if (($foo = $prefs->preference_get('foo')) == FALSE)
		trigger_error("Could not read preference foo");

Bug reports, suggestions, or even patches should be sent to me directly,
so I can keep up with any and all development. Remember, if someone forks
the code--and you use their fork--and it doesn't work; I can't help you.
But, if you do use my code, and need any help (or whatever), you can shoot
me an email at phpclasses (at) kennethpaul (dot) com. Happy coding! :)
  Files folder image Files  
File Role Description
Plain text file Class The PHP class for cookie_preferences
Accessible without login Plain text file README.cookie_preferences Doc. Documentation with examples on how to use cookie_preferences

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:2,765
This week:209Up
 User Ratings  
 All time

For more information send a message to info at phpclasses dot org.