Restrict Module access by User Level and/or Forum Posts

  Post new topicReply to topicPrintable Version
<< View previous topic View next topic >>
Share: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
#1   Restrict Module access by User Level and/or Forum Posts
mjhufford
CZ Active Member
 Codezwiz Site Donator
mjhufford has been a member for over 20 year's 20 Year Member
usa.gif arkansas.gif
Occupation: IT Industry
Age: 47
Gender: Male
Fav. Sports Team: Da Bears.
Website:
Status: Offline
Joined: Jul 01, 2004
0.04 posts per day
Posts: 288
Points: 15,094
  MSN Messenger 
I took some of the code from the "limit arcade access" mod found in the download section here.

I wanted a way to be able to restrict access to a specific module on my site. I restricted it so the user has to have at least 25 posts in the forums AND 2 posts in the last 7 days in order to have access. I also put some code in there about User_Level so that you can grant access automatically to certain user types. (Admins and Staff in my case).

I placed this code in the beginning of the index.php file for the respective module. There's probably a better way to do it, but this is what I came up with. Hope you like it! You can see it in action [ Register or login to view links on this board.] . I have this working on my Backing Tracks module (need to register to see it).

Hope you like it!

//My little mod to make people post in the forums
global $db, $prefix, $user;
getusrinfo($user);

if (is_user($user)) {$uid = $userinfo['user_id'];
                $ulevel = $userinfo['user_level'];}

$secs = 86400;
$days = 7;
$posts = 2;
$min_posts = 25;
$current_time = time();
$old_time = $current_time - ($secs * $days);

$result = $db->sql_query("SELECT * FROM ".$prefix."_bbposts WHERE poster_id = $uid and post_time BETWEEN $old_time AND $current_time");
$Amount_Of_Posts = $db->sql_numrows( $result );

$total_result = $db->sql_query("SELECT * FROM ".$prefix."_bbposts WHERE poster_id = $uid");
$total_posts = mysql_num_rows($total_result);

if ($ulevel != 2 && $ulevel !=3) {
   if ($Amount_of_Posts < $min_posts || $Amount_Of_Posts < $posts)
   {include("header.php");
   $diff_posts = $posts - $Amount_Of_Posts;
   OpenTable();
   
      $ThemeSel = get_theme();
       if (file_exists("themes/$ThemeSel/images/media.gif")) {
         echo "<br><center><a href=\"modules.php?name=$module_name\"><img src=\"themes/$ThemeSel/images/media.gif\" border=\"0\" alt=\"\"></a><br><br></center>";
             } else {
         echo "<br><center><a href=\"modules.php?name=$module_name\"><img src=\"modules/$module_name/images/media.gif\" border=\"0\" alt=\"\"></a><br><br></center>";
          }
      echo "<center><b>Thanks for your interest in our $module_name module!</b></center><br><br>

            We offer access to this area free of charge.  We just need a bit of your time!            
            Users must have at least $min_posts total posts in the forums and need at least $posts posts every $days days to check out the $module_name module. 
            We require this to help our community grow and look forward to your participation!<br><br><br>

                 <center><b>Right now, you have $total_posts total posts and $Amount_Of_Posts posts in the last $days days.</b></center><br><br><br>
         
         
            Please keep in mind that we have forum moderators who check for profanity, spam and other types of abuse in our forums. 
                 If users are caught abusing the forums they stand the chance of being banned from the site. 
            This is how we keep our site great for players of all ages!  See you in the forums!<br><br><br>



            <center><a href='modules.php?name=Forums'>Click here to go to the forums.</a></center><br><br><br>



            <center><b>Thanks for helping the <a href='index.php'>YOUR_SITE_NAME_HERE</a> community grow!</b></center>";
   
   CloseTable();
   include("footer.php");
   }
} else {
//END MY MOD




_________________
"The pursuit of easy things makes men weak."
-David O. McKay
Back to top Reply with quote
#2   re: Restrict Module access by User Level and/or Forum Posts
Dauthus
CZ Addict
 Codezwiz Site Donator
Dauthus has been a member for over 20 year's 20 Year Member
usa.gif illinois.gif
Age: 61
Gender: Male
Website:
Status: Offline
Joined: Mar 17, 2004
0.06 posts per day
Posts: 426
Points: 15,917
   
All I get is a parse error:

Parse error: parse error, unexpected $ in /blah/blahblah/Module_Name/index.php on line 598


Line 598 is the last line in the index.html file and is

?>




_________________
[ Register or login to view links on this board.]
Vivere disce, cogita mori
Back to top Reply with quote
#3   
mjhufford
CZ Active Member
 Codezwiz Site Donator
mjhufford has been a member for over 20 year's 20 Year Member
usa.gif arkansas.gif
Occupation: IT Industry
Age: 47
Gender: Male
Fav. Sports Team: Da Bears.
Website:
Status: Offline
Joined: Jul 01, 2004
0.04 posts per day
Posts: 288
Points: 15,094
  MSN Messenger 
Hmmm...working on my site. I'm running 7.6 and pasted this code right at the top of the index.html file just after:

if (!eregi("modules.php", $_SERVER['PHP_SELF'])) {
    die ("You can't access this file directly...");
}
$index = 1;
require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
get_lang($module_name);
$pagetitle = "- $module_name";




_________________
"The pursuit of easy things makes men weak."
-David O. McKay
Back to top Reply with quote
#4   
mjhufford
CZ Active Member
 Codezwiz Site Donator
mjhufford has been a member for over 20 year's 20 Year Member
usa.gif arkansas.gif
Occupation: IT Industry
Age: 47
Gender: Male
Fav. Sports Team: Da Bears.
Website:
Status: Offline
Joined: Jul 01, 2004
0.04 posts per day
Posts: 288
Points: 15,094
  MSN Messenger 
Which module are you doing this with? My Backing Tracks module is very similar to the Web Links module if that helps any.




_________________
"The pursuit of easy things makes men weak."
-David O. McKay
Back to top Reply with quote
#5   re: Restrict Module access by User Level and/or Forum Posts
Dauthus
CZ Addict
 Codezwiz Site Donator
Dauthus has been a member for over 20 year's 20 Year Member
usa.gif illinois.gif
Age: 61
Gender: Male
Website:
Status: Offline
Joined: Mar 17, 2004
0.06 posts per day
Posts: 426
Points: 15,917
   
Same error putting it the place you mentioned. I am using Telli's Giveaways Module and trying to implement this into it.




_________________
[ Register or login to view links on this board.]
Vivere disce, cogita mori
Back to top Reply with quote
#6   
Telli
Site Admin
Occupation: Self Employed
Age: 46
Gender: Male
Fav. Sports Team: Detroit Red Wings
Website:
Status: Offline
Joined: May 26, 2003
1.03 posts per day
Posts: 8089
Points: 494,440
   
I think this:


   if ($Amount_of_Posts < $min_posts || $Amount_Of_Posts < $posts)


Should be this:


   if ($total_posts < $min_posts || $Amount_Of_Posts < $posts)



That getusrinfo($user); is producing queries on every page load you should just add this into your mainfile and use it as a global:


global $user;
if (is_user($user)) {
    $userinfo = getusrinfo($user);
}


Also you just make it a function so that you don't have to paste all that code into everypage just put the function in your mainfile.php:


function extra_auth($user_id, $ulevel) {
//My little mod to make people post in the forums
global $db, $prefix, $sitename, $module_name;

$user_id = intval($user_id);

$secs = 86400;
$days = 7;
$posts = 2;
$min_posts = 25;
$current_time = time();
$old_time = $current_time - ($secs * $days);

$Amount_Of_Posts = $db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_bbposts WHERE poster_id ='$user_id' AND post_time BETWEEN $old_time AND $current_time"));

$total_posts = $db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_bbposts WHERE poster_id ='$user_id'"));

if ($ulevel < 2) {
   if ($total_posts < $min_posts || $Amount_Of_Posts < $posts) {
       include("header.php");
       $diff_posts = $posts - $Amount_Of_Posts;
       OpenTable();
       $ThemeSel = get_theme();
             if (file_exists("themes/$ThemeSel/images/media.gif")) {
                 echo "<br><center><a href=\"modules.php?name=$module_name\"><img src=\"themes/$ThemeSel/images/media.gif\" border=\"0\" alt=\"\"></a><br><br></center>";
             } else {
                 echo "<br><center><a href=\"modules.php?name=$module_name\"><img src=\"modules/$module_name/images/media.gif\" border=\"0\" alt=\"\"></a><br><br></center>";
             }
                 echo "<center><b>Thanks for your interest in our $module_name module!</b></center><br><br>We offer access to this area free of charge.  We just need a bit of your time! Users must have at least $min_posts total posts in the forums and need at least $posts posts every $days days to check out the $module_name module. We require this to help our community grow and look forward to your participation!<br><br><br><center><b>Right now, you have $total_posts total posts and $Amount_Of_Posts posts in the last $days days.</b></center><br><br><br>Please keep in mind that we have forum moderators who check for profanity, spam and other types of abuse in our forums. If users are caught abusing the forums they stand the chance of being banned from the site. This is how we keep our site great for players of all ages!  See you in the forums!<br><br><br><center><a href='forums.html'>Click here to go to the forums.</a></center><br><br><br><center><b>Thanks for helping the <a href='index.html'>$sitename</a> community grow!</b></center>";
        CloseTable();
        include("footer.php");
        exit;
    }
}
//END MY MOD
}


Then to use it (if you add the $userinfo as a global)


global $userinfo;
extra_auth($userinfo['user_id'], $userinfo['user_level']);


And one more thing when your using the database and numbers its a good Idea to intval the value.



_________________
The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee. Ezekiel 25:17
Back to top Reply with quote
#7   
Dauthus
CZ Addict
 Codezwiz Site Donator
Dauthus has been a member for over 20 year's 20 Year Member
usa.gif illinois.gif
Age: 61
Gender: Male
Website:
Status: Offline
Joined: Mar 17, 2004
0.06 posts per day
Posts: 426
Points: 15,917
   
Need to add

$module_name


to your globals for the module to show up. At least I had to in mine. icon_biggrin.gif



_________________
[ Register or login to view links on this board.]
Vivere disce, cogita mori
Back to top Reply with quote
#8   
Telli
Site Admin
Occupation: Self Employed
Age: 46
Gender: Male
Fav. Sports Team: Detroit Red Wings
Website:
Status: Offline
Joined: May 26, 2003
1.03 posts per day
Posts: 8089
Points: 494,440
   
Sorry forgot that one. Added it above if anyone wants to use it.




_________________
The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee. Ezekiel 25:17
Back to top Reply with quote
#9   re: Restrict Module access by User Level and/or Forum Posts
mjhufford
CZ Active Member
 Codezwiz Site Donator
mjhufford has been a member for over 20 year's 20 Year Member
usa.gif arkansas.gif
Occupation: IT Industry
Age: 47
Gender: Male
Fav. Sports Team: Da Bears.
Website:
Status: Offline
Joined: Jul 01, 2004
0.04 posts per day
Posts: 288
Points: 15,094
  MSN Messenger 
Thanks for taking a look at the code Telli...

I forgot one other thing. If you're using the code I originally pasted, it's an "if" statement. So you need to add a right brace } right before ?> at the end of the file.




_________________
"The pursuit of easy things makes men weak."
-David O. McKay
Back to top Reply with quote
#10   
Dauthus
CZ Addict
 Codezwiz Site Donator
Dauthus has been a member for over 20 year's 20 Year Member
usa.gif illinois.gif
Age: 61
Gender: Male
Website:
Status: Offline
Joined: Mar 17, 2004
0.06 posts per day
Posts: 426
Points: 15,917
   
any way to "simply" add user_points to this so a user needs xx points to access the module?

BTW, really useful mod. Thanks for sharing!




_________________
[ Register or login to view links on this board.]
Vivere disce, cogita mori
Back to top Reply with quote
Display posts from previous:      
Add To: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
<< View previous topic View next topic >>
Post new topicReply to topic

Jump to 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum