How to use the PHPVibe user system (Premium videos example included)

19 Jun 2017
@PHPVibe
3370

The PHPVibe functions for user handling are located in lib/functions.user.php and are as follows:

Note: user refers to “current logged in user”.

is_user() -> Returns if the user is logged in
user_id() -> Returns the user’s id
user_name() -> Returns the user’s name
user_noty() -> Returns the user’s last notify time (more accurate than last login)
my_profile() -> Returns url to user’s profile
user_avatar() -> Returns the user’s avatar
user_group() -> Returns the group the user is assigned to
is_admin() -> Returns if user is an admin
is_moderator() -> Returns if user is the moderator (also true for admin group)

This are the session values, there are some other useful functions for likes, history and others.
You can see them under lib/functions.user.php

To pull this in your custom script/app, just include load.php (Beware: in most cases this is not that simple, since it will conflict with the custom app/script’s functions if they share functions).

Working with a logged in user’s group is easy.
user_group() – returns the group of the user.

A basic example is the is_moderator function.

function is_moderator(){
global $db;
if (!is_user() || user_group() > 2 ) {
return false;
} else {
$check = $db->get_row("SELECT group_id from ".DB_PREFIX."users WHERE id='".user_id()."'");
if($check && ($check->group_id < 3)) {
return true;
} else {
return false;
}

This basically checks if the user is in groups with ids 1 and 2, and can moderate stuff around.

But this can be easily coded for other use.

For example let's say we have a group Premium with the id 7 created from the admin panel, and we want all "Private" videos to be visible to this group and moderators only.

So, let's build a simple function:

function is_premium(){
return ((is_user() && user_group() == 7) || is_moderator());
}

Now you have a premium function to use (drop it at the end of the lib/functions.php file, before the closing php tag) and...use it!

if(is_premium()) {echo "Way to go man";} else {echo "You are free member";}

Now, let's apply it to our real life example : Making the "private" videos available only to subscribers!

Open com/com_video.php

find this line

//Check if it's private 
if(($video->private == 1) && !is_user()) {


and replace it with our new build condition:

//Check if it's private 
if(($video->private == 1) && !is_premium()) {


Now the system will check if the video is private and if the user is not premium.

For multiple groups the syntax is simple:

function is_premium(){
$allowed = array("1","2","7","100"); // Group ids
return ((is_user() && in_array(user_group(), $allowed )) || is_moderator());
}

You can change this to the text and link to a payment gateway/processor, and build a comeback which upgrades the user to group 7 upon paying.
Make sure you refresh his session after the upgrade group id query.

RefreshUser(user_id())


Yes, it's that simple!

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.