TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

April 19, 2024, 08:40:21 PM

Login with username, password and session length
Members
  • Total Members: 3,885
  • Latest: Growner
Stats
  • Total Posts: 195,165
  • Total Topics: 21,219
  • Online today: 266
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 1
  • Guests: 194
  • Total: 195
  • tino

[Block] Guest Page View Limit

Started by Thurnok, November 17, 2006, 04:51:03 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Thurnok

Here's a simple snippet that allows you to force a guest to the registration page after so many page views on your site.  Copy this into a phpblock (left/right/center).  You should make the block have no title or frame so it is basically invisible.

It will redirect the "Guest" to the registration page after they have exceeded the number of page views you set with the $gpvl_MaxPages variable in the code.  To see it in action, you can go to my TP Blocks site as a guest, view 3 pages and you will be redirected to the SMF Registration page.

Here's the code:

//////////////////////////////////////////////
// Guest Page View Limit 1.2
//////////////////////////////////////////////
// Developed by TPH Thurnok
// TinyPortal Hosting - Your premier TP/SMF host
// http://www.tinyportalhosting.com
// November 16, 2006
//
// Last update: March 10, 2007
//    added &gpvl=exceeded to redirect so scripting can be done in registration
//    to allow detecting user got there by exceeding the page view limit
//
// This is designed for a phpblock.
// This allows you to limit the number of pages a guest can view on your site
// before being redirected to the registration page.
//
// Requirements:
// In order for this to work, you must place this in a phpblock that is displayed
// on all pages.  For any pages where this block is not displayed, that page will
// not be counted against the guest.  It is recommended you do not display a title
// or frame for the block so that it is basically invisible to the user.
//
// This script will create a table for the storage of IPs and the count of pages.
// It is a very simple IP based count.  No session info or other info is used.
// Therefore, if you set this to a very high value, and a user has a dynamic IP, you
// may be ineffective as the user may simply get a new IP the next time they visit your site.
//
//////////////////////////////////////////////

/*
****************************************
****************************************
*** !! User Configuration Section !! ***
****************************************
****************************************
*/
// Set the maximum number of pages you want to limit a normal guest to view
$gpvl_MaxPages = 3;
// Set the maximum number of pages to limit "special" guests to (usually for high limit guests)
$gpvl_MaxPagesSpecial = 1000;

// Set the IPs for "special" guests in an array - Example: array('12.1.4.70', '3.88.129.211', '42.150.220.200');
$gpvl_SpecialIPs = array();

// Set the IPs you want excluded from the limit - like google/spiders etc. in an array - same format as "special" guest array
$gpvl_ExcludeIPs = array('127.0.0.1');

// set the table name you want to use (without a prefix - prefix will be auto added)
$gpvl_table = 'guest_page_limit';
/*
****************************************
****************************************
*/

//////////////////////////////////////////////
//
// The rest of this you should leave as is
// unless you are overly industrious :)
//
//////////////////////////////////////////////
// globals for database vars
global $db_prefix, $tp_prefix;
// globals for user information
global $context, $scripturl;

// fix for TP 0.8.6 and lower
if (empty($tp_prefix)){
$tp_prefix = $settings['tp_prefix'];
}
$gpvl_table = $tp_prefix . $gpvl_table;

$myself = $_SERVER['REQUEST_URL'];
// first seperate the parameters from the URL, to avoid possible search string in the URL itself
$parms = str_replace($scripturl, "", $myself);
// then see if we are actually at the registration page (action=register)
$gpvl_at_register = stristr($parms, "action=register") ? true : false;
// or if we are at the login page (action=login)
$gpvl_at_login = stristr($parms, "action=login") ? true : false;
// or if we are at the password reminder page (action=reminder)
$gpvl_at_reminder = stristr($parms, "action=reminder") ? true : false;

// only for limiting guests, so if not a guest - get out (unless at registration/login/reminder page)
if ($context['user']['is_guest'] && !$gpvl_at_register && !$gpvl_at_login && !$gpvl_at_reminder){
// preset to max pages in case we fall thru
$gpvl_NumPages = $gpvl_MaxPages;

// what is our IP?
$gpvl_ip = empty($_SERVER['REMOTE_ADDR']) ? getenv('REMOTE_ADDR') : $_SERVER['REMOTE_ADDR'];
// if we got an IP (and it is not an excluded IP), do the work, otherwise, send them to registration
if ($gpvl_ip && strtoupper($gpvl_ip) != "NULL"){
if (in_array($gpvl_ip, $gpvl_ExcludeIPs)){
$gpvl_NumPages = 0;
} else {
// if they are a "special" guest, set their max pages to the "special" max pages setting
if (in_array($gpvl_ip, $gpvl_SpecialIPs)){
$gpvl_MaxPages = $gpvl_MaxPagesSpecial;
}
// test for table existance
$gpvl_result = @mysql_query('SELECT pageviews FROM '.$gpvl_table.' WHERE ip = "'.$gpvl_ip.'" LIMIT 1');
if (!$gpvl_result){
if (mysql_errno() == 1146){
// table doesn't exist, create it!
@mysql_query('CREATE table '.$gpvl_table.'(ip TEXT, pageviews INT UNSIGNED NOT NULL)');
$gpvl_NumPages = 1;
@mysql_query('INSERT INTO '.$gpvl_table.' VALUES ("'.$gpvl_ip.'", 1)');
} else {
die("Guest Page View Limit - Unexpected error: " . mysql_error());
}
} else {
if (mysql_num_rows($gpvl_result)){
// found this IP already in table
$row = mysql_fetch_assoc($gpvl_result);
$gpvl_NumPages = $row['pageviews'] + 1;
@mysql_query('UPDATE '.$gpvl_table.' SET pageviews = '.$gpvl_NumPages.' WHERE ip = "'.$gpvl_ip.'"');
} else {
// not in table yet, insert it
$gpvl_NumPages = 1;
@mysql_query('INSERT INTO '.$gpvl_table.' VALUES ("'.$gpvl_ip.'", 1)');
}
mysql_free_result($gpvl_result);
}
}
}

// Send to registration if they are at or above max pages
if ($gpvl_NumPages > 0 && $gpvl_NumPages >= $gpvl_MaxPages){
header("Location: ".$scripturl."?action=register&gpvl=exceeded");
}
}

technodragon73

ROFL

As many guests as I get all of the time I may start using this block!

knat

#2
Thanks alot - i needed this  ;)

Edit: Argghh !  :P something is wrong with the script.. when i test it its working fine, but it takes ages for it to send the guest to the registration page... it just sit there loading for a very very long time.. what can be the cause of this ? if i disable the script while the other computer is loading it loads instantly the second i disable the script..  :o

Nokonium

That should upset habitual lurkers  :up:

knat

Quote from: knat on November 17, 2006, 07:10:00 AM
Thanks alot - i needed this  ;)

Edit: Argghh !  :P something is wrong with the script.. when i test it its working fine, but it takes ages for it to send the guest to the registration page... it just sit there loading for a very very long time.. what can be the cause of this ? if i disable the script while the other computer is loading it loads instantly the second i disable the script..  :o

Ok i found a workaround for this.. i only make the script work inside forum.. this way the script dont run on the registration page and now it works fine ... but i guess the page view only counts for the boardindex now :-s

Thurnok

You need to make sure the block doesn't show up on the registration page, because you would go into an endless loop if it did, since the block does a redirect.

In otherwords, your registration page must not show the block area that you've placed the Guest Page View Limit code.  Try putting it in a centerblock and make sure centerblocks are turned off for your forum page.  Or, if you normally turn off right blocks when in your forum, use a right block.  The registration page is part of the forum, so simply put the code into whatever block you "do not" have showing up when in your forum pages.

knat

I use all blocktypes in my forum so this is not an option for me... but i got it working to a level that i am happy with :) i made it show on boardindex and in my most popular board.. and guests are bound to go to the boardindex to get to the categories.. so its all good  ;) Thanks for the code  :)

akulion

wow another great script by thurnok!

Thanks so much for this :D

I do have one question though - what happens to bots if using this?

Like yahoo msn google etc....wouldnt they get redirected as well and as a result end their indexing journey?

Thurnok

Most likely.

Perhaps a list of IPs or domains to exclude from the logic...

Techdomain

Nice code - I will certainly see how this one develops!