SimpleUsers Method and Member reference

Index

Methods

SimpleUsers::__construct()

Object construct verifies that a session has been started and that a MySQL connection can be established.
It takes no parameters.

Example <?php
    $SimpleUsers 
= new SimpleUsers();
?>

SimpleUsers::createUser( string $username, string $password )

Returns a (int)user id, if the user was created succesfully. If not, it returns (bool)false.

Example <?php

    $SimpleUsers 
= new SimpleUsers();
    
    
$res $SimpleUsers->createUser("John""qwerty");
    if( 
$res )
        echo 
"User was created with userId: ".$res;
    else
        echo 
"The username was already taken.";

?>

SimpleUsers::loginUser( string $username, string $password )

Pairs up username and password as registrered in the database.
If the username and password is correct, it will return (int)user id of the user which credentials has been passed and set the session, for use by the user validating.

Example <?php

    $SimpleUsers 
= new SimpleUsers();

    
$res $SimpleUsers->loginUser("John""qwerty");
    
    if( 
$res )
    {
        
header("Location: ...");
        exit;
    }    
    else
    {
        echo 
"You passed the wrong credentials.";
    }

?>

SimpleUsers::setInfo( string $key, string $value [, int $userId] )

Sets a custom information pair, consisting of a key name and that keys value for a user. Setting the third parameter sets the provided information for a given user, instead of the one calling it.

Example #1 <?php

    $SimpleUsers 
= new SimpleUsers();

    
// Store information for the current user
    
$SimpleUsers->setInfo("fullname""John Doe");
?>

Example #2 <?php

    $SimpleUsers 
= new SimpleUsers();

    
// Store information for the user with userId provided in the third parameter.
    // This is useful for administrators when wanting to assign, edit or empty stored information about a user
    
$SimpleUsers->setInfo("fullname""John Doe"354);
?>

SimpleUsers::getInfo( string $key [, int $userId] )

Use this function to retrieve stored custom user information.
Setting the third parameter gets the stored information for a given user, instead of the one calling it.

Example #1 <?php

    $SimpleUsers 
= new SimpleUsers();

    
// Get stored information for the active user
    
$fullname $SimpleUsers->getInfo("fullname");
?>

Example #2 <?php

    $SimpleUsers 
= new SimpleUsers();

    
// Get stored information for the user with userId provided in the third parameter.
    // This is useful for administrators when wanting to retrieve stored information about a user
    
$fullname $SimpleUsers->getInfo("fullname"354);
?>

SimpleUsers::getInfoArray( [int $userId] )

Returns an associative array with all the custom information stored about the current user.
Passing a userId as a parameter will return an associative array with all the custom information for that specific user.

Example #1 <?php

    $SimpleUsers 
= new SimpleUsers();

    
// Gets all stored information for the active user and returns an associative array
    
$info $SimpleUsers->getInfoArray();
    
    
print_r($info);
?>

Example #2 <?php

    $SimpleUsers 
= new SimpleUsers();

    
// Gets all stored information for a given user and returns an associative array
    // This is useful for administrators when wanting to get all stored information about a user 
    
$info $SimpleUsers->getInfoArray(354);
    
    
print_r($info);
?>

SimpleUsers::logoutUser()

Logout the active user, unsetting the users session.

Example <?php

    $SimpleUsers 
= new SimpleUsers();

    
$SimpleUsers->logoutUser();
?>

SimpleUsers::setPassword( string $password [, int $userId] )

Update the users password with this function.
It generates a new salt and a sets the users password provided by the first parameter.
Providing a userId in the second paramater will set the password for that specific user.

Example #1 <?php

    $SimpleUsers 
= new SimpleUsers();

    
// Sets a new password for the current active user
    
$SimpleUsers->setPassword("difficult");
?>

Example #2 <?php

    $SimpleUsers 
= new SimpleUsers();

    
// Sets a new password for the userId identified by the second parameter.
    // This is useful if an administrative action is needed for setting a new password.
    
$SimpleUsers->setPassword("difficult"354);
?>

SimpleUsers::getUsers()

Gives you an array with all registered users and their basic information (last activity, creation date, username and userId)

Example <?php

    $SimpleUsers 
= new SimpleUsers();

    
$users $SimpleUsers->getUsers();

    
print_r($users);
?>

SimpleUsers::getSingleUser( [int $userId] )

Gives you an associative array, containing the basic information (last activity, creation date, username and userId) for the current active user.
If a userId is provided as parameter, the returned associative array will contain the basic information for that specific user.

Example #1 <?php

    $SimpleUsers 
= new SimpleUsers();

    
$user $SimpleUsers->getSingleUser();

    
print_r($user);
?>

Example #2 <?php

    $SimpleUsers 
= new SimpleUsers();

    
// Retrieving the basic information for a specific user.
    // This is useful for administrative purposes.
    
$user $SimpleUsers->getSingleUser(354);

    
print_r($user);
?>

SimpleUsers::deleteUser( int $userId )

Deletes a given user and stored custom information with it.

Example <?php

    $SimpleUsers 
= new SimpleUsers();

    
$SimpleUsers->deleteUser(354);
?>

SimpleUsers::getToken( [bool] )

Returns a hidden input field with a unique token value for CSRF to be used with post data.
The token is saved in a session for later validation.
The boolean parameter is default set to true which returns the input field as XHTML - provide (bool) false for HTML 4 compliance.

Example <?php
    $SimpleUsers 
= new SimpleUsers(); 
?>
<form method="post" action="...">
    <input ...>
    <?php echo $SimpleUsers->getToken(); ?>
</form>

SimpleUsers::validateToken()

Use this method when you wish to validate the CSRF token from your post data.
The method returns true upon validation, otherwise false.

Example <?php

    $SimpleUsers 
= new SimpleUsers();

    
$csrf $SimpleUsers->validateToken();
    
    if(!
$csrf)
    {
        
header("Location: request_denied.php");
        exit;
    }
    
    
// Otherwise, continue with processing

?>

Members

SimpleUsers::logged_in

This tells wether a user is logged in or not.
Value is (bool)true or (bool)false.

Example <?php

    $SimpleUsers 
= new SimpleUsers();

    if( 
$SimpleUsers->logged_in )
        echo 
"You are logged in";
    else
        echo 
"You are NOT logged in!";
?>

SimpleUsers::userdata

This is auto-populated array containing the information stored for the logged in user.
If no user is logged in, this member consists of an empty array.

Example <?php

    $SimpleUsers 
= new SimpleUsers();

    if( 
$SimpleUsers->logged_in )
        echo 
"Hello " $SimpleUsers->userdata["uUsername"] . " - your last activity was at " $SimpleUsers->userdata["uActivity"];

?>