PHP Classes
elePHPant
Icontem

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    
Last Updated Ratings Unique User Downloads Download Rankings
2004-08-01 (12 years ago) RSS 2.0 feedStarStarStar 57%Total: 1,393 All time: 2,703 This week: 871Up
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

Details
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]]]] )

Description:
	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)

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

bool preference_new ( preference_name, preference_value )

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

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

bool preference_update ( preference_name, preference_value )

Description:
	Updates an already existing cookie with a new value.

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

string preference_get ( preference_name )

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

Example:
	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 cookie_preferences.inc 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  
 0%
Total:1,393
This week:0
All time:2,703
This week:871Up
 User Ratings  
 
 All time
Utility:80%StarStarStarStarStar
Consistency:90%StarStarStarStarStar
Documentation:80%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:57%StarStarStar
Rank:1441