News: When asking for support, it helps us and you, if you read the Posting Guidelines

Login  |  Register
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media
Welcome, Guest. Please login or register.
Did you miss your activation email?

January 22, 2020, 02:42:34 AM

Login with username, password and session length

Recent

Members
Stats
  • Total Posts: 189421
  • Total Topics: 20795
  • Online Today: 362
  • Online Ever: 917
  • (January 21, 2020, 09:02:26 AM)
Users Online
Users: 0
Guests: 40
Total: 40

Author Topic: [Block] Guest Page View Limit  (Read 32820 times)

0 Members and 1 Guest are viewing this topic.

Offline Thurnok

  • Friends
  • *
  • Posts: 784
[Block] Guest Page View Limit
« on: November 16, 2006, 09:51:03 PM »
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:
Code: [Select]
//////////////////////////////////////////////
// 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");
}
}
« Last Edit: March 10, 2007, 04:38:41 PM by Thurnok »

technodragon73

  • Guest
Re: [Block] Guest Page View Limit
« Reply #1 on: November 16, 2006, 10:04:11 PM »
ROFL

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

knat

  • Guest
Re: [Block] Guest Page View Limit
« Reply #2 on: November 17, 2006, 12: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
« Last Edit: November 17, 2006, 12:19:58 AM by knat »

Offline Nokonium

  • Aging Hippie
  • Friends
  • *
  • Posts: 1384
    • Our-Local
Re: [Block] Guest Page View Limit
« Reply #3 on: November 17, 2006, 12:18:05 AM »
That should upset habitual lurkers  :up:

knat

  • Guest
Re: [Block] Guest Page View Limit
« Reply #4 on: November 17, 2006, 12:23:01 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

Offline Thurnok

  • Friends
  • *
  • Posts: 784
Re: [Block] Guest Page View Limit
« Reply #5 on: November 17, 2006, 01:54:55 AM »
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

  • Guest
Re: [Block] Guest Page View Limit
« Reply #6 on: November 17, 2006, 02:21:56 AM »
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

  • Guest
Re: [Block] Guest Page View Limit
« Reply #7 on: November 17, 2006, 02:56:22 AM »
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?

Offline Thurnok

  • Friends
  • *
  • Posts: 784
Re: [Block] Guest Page View Limit
« Reply #8 on: November 17, 2006, 03:52:08 AM »
Most likely.

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

Techdomain

  • Guest
Re: [Block] Guest Page View Limit
« Reply #9 on: November 17, 2006, 04:15:18 AM »
Nice code - I will certainly see how this one develops!