TinyPass Developer's Resources

Metered Browsing

Under this model publishers can allow their users to access protected resources a specified number of times. It is up to the publisher to decide what to do once a user exceeds the specified number of “views”. For example, publishers could require the user makes a purchase for continued access, or allow him or her to continue, but be asked to donate once they've reached the specified limit.

Publishers can store meter information in either the reader's browser cookie (the most popular approach) or in local data storage (database). One should be aware that the first approach is not entirely secure and any user could reset the meter by simply clearing his or her browser cookies.

In the example below a publisher lets its users view 20 pages within a day. Once a user exceeds 20 views, he will be asked to pay for continued access. Here is how it's done:

//Load meter information from a browser cookie
$cookieName = "YOUR_CUSTOM_COOKIE_NAME";
$meter = TPMeterHelper::loadMeterFromCookie($cookieName, $_COOKIE);
 
//If meter doesn't exist, create one
if ($meter == NULL) {
   $maxAccessAttempts = 20;
   $trialPeriod = "1 day";
   $meter = TPMeterHelper::createViewBased($cookieName, $maxAccessAttempts, $trialPeriod);
}
 
//increment the view count
$meter->increment();
 
//always set the cookie. Cookie should be set on the right domain, preferably top level (.yourdomain.com)
$domain = "yourdomain.com";
$encryptedToken = TPMeterHelper::serialize($meter);
setcookie($cookieName, $encryptedToken, time() + 60 * 60 * 24 * 30, '/', $domain);
 
//If trial period is active then show the content
if ($meter->isTrialPeriodActive()) {
   //show the content - meter is valid
} else {
   //trial is over, force or ask user to purchase
}

//Load meter information from a browser cookie
String cookieName = "YOUR_CUSTOM_COOKIE_NAME";
String cookieStr = request.getHeader("cookie");
Meter meter = MeterHelper.loadMeterFromCookie(cookieName, cookieStr);
 
//If meter doesn't exist, create one
if (meter == null) {
   int maxAccessAttempts = 20;
   String trialPeriod = "1 day";
   meter = MeterHelper.createViewBased(cookieName, maxAccessAttempts, trialPeriod);
}
 
//increment the view count
meter.increment();
 
//always set the cookie. Cookie should be set on the right domain, preferably top level (.yourdomain.com)
String encryptedToken = MeterHelper.serialize(meter);
//... set cookie
 
//If trial period is active then show the content
if (meter.isTrialPeriodActive()) {
   //show the content - meter is valid
} else {
   //trial is over, force or ask user to purchase
}

//Load meter information from a browser cookie
string cookieName = "YOUR_CUSTOM_COOKIE_NAME";
string cookieStr = Request.Headers["cookie"];
Meter meter = MeterHelper.LoadMeterFromCookie(cookieName, cookieStr);
 
//If meter doesn't exist, create one
if (meter == null) {
   int maxAccessAttempts = 20;
   string trialPeriod = "1 day";
   meter = MeterHelper.CreateViewBased(cookieName, maxAccessAttempts, trialPeriod);
}
 
//increment the view count
meter.Increment();
 
//always set the cookie. Cookie should be set on the right domain, preferably top level (.yourdomain.com)
string encryptedToken = MeterHelper.Serialize(meter);
//... set cookie
 
//If trial period is active then show the content
if (meter.IsTrialPeriodActive()) {
   //show the content - meter is valid
} else {
   //trial is over, force or ask user to purchase
}

# Load meter information from a browser cookie
cookie_name = "your_rid" #this MUST match your RID
meter = Tinypass::MeterHelper.load_meter_from_cookie(cookie_name, cookies)
 
# If meter doesn't exist, create one
if meter == nil
    max_views = 20
    trial_period = "1 day"
    meter = Tinypass::MeterHelper.create_view_based(cookie_name, max_views, trial_period)
end
 
# increment the view count
meter.increment
 
# always set the cookie. Cookie should be set on the right domain, preferably top level (.yourdomain.com)
domain = "yourdomain.com"
encrypted_token = Tinypass::MeterHelper.serialize(meter)
 
cookies[cookie_name.to_sym] = {
    :value => encrypted_token,
    :expires => Time.now + 60 * 60 * 24 * 30
}
 
# If trial period is active then show the content
if trial_period_active?
    # show the content - meter is valid
else
    # trial is over, force or ask user to purchase
end