Skip to content

TheCJGCJG/PHP-Utilities

Repository files navigation

PHP Utilities Alpha 2

Welcome to PHP Utilities, a collection of useful everyday PHP classes that all work as one.

This is currently an Alpha release so I highly recommend that you do not use this on a live server. There are methods in this version that are not utilised but are being implemented where necassary. This is technically a development release so it will change drastically over the Alpha and Beta stages. Feel free to mess around with it and learn how things work.

For the mean time, here are some guides to get you started.

Requirements

  • PHP 5.5.0 or Greater
  • MySQLi or MariaDB
  • Output Buffering Enabled (for redirect_to function, not currently utilised)

Usage Instructions

Below is a a list of usage guides to assist you with getting to learn the API of PHP Utilities

Requiring the main.class file

/*
* All classes in PHP Utilities are linked into the main.class.php file.
* This therefore means you only need to require the main class in order
* to utilise the other classes.
*/
require_once 'class.user.php'

Class Instantiation

/*
* Before you can use the class, you must first instantiate it.
* You establish a database connection during this step.
*/
$User = new User('localhost', 'root', 'password', 'dbname', '3306', 'OptionalPrefix');

Creating a user account

/*
* Once the database connection has been established, you can simply call the createAccount method.
*/
$User->createAccount('John', 'Doe');

/*
* Why not add it into a conditional?
*/
if ($User->createAccount('John', 'Doe'))
{
  echo 'Account created successfully!';
}

Deleting a user account

/*
* To delete a user's account we need to call the deleteAccount method, and pass in the username
* as an argument.
*/
$User->deleteAccount('John');

/*
* This can be used in a conditional too.
*/
if ($User->deleteAccount('John'))
{
  echo 'Account successfully deleted!';
}

Changing/Updating a user's password

/*
* To update a user's password we will utilise the changePassword method.
*/
$User->changePassword('John', 'New Password');

/*
* Again this can be used in a conditional statement
*/
if ($User->changePassword('John', 'New Password'))
{
  echo 'Password successfully updated!';
}

Authenticating a user (Logging In)

/*
* To authenticate a user at login all we need to do is call the authenticate method.
*/
$User->authenticate('John', 'Doe');

/*
* This will most likely be put into a conditional.
* For example purposes let's say we have the username John and his password is 'Password'.
*/
if ($User->authenticate('John', 'Password'))
{
  // The password was correct and we can log them in
  $User->session->login('John');
}
else
{
  // The combination was incorrect.
  echo 'Incorrect Password';
}

Logging Out a user

/*
* Just like authenticating a user we are going to have to reference a session method.
* The logout method takes no arguments.
*/
$User->session->logout();

Banning a user

/*
* PHP Utilities currently comes with a simple banning functionality.
* To ban a user, simply call the ban method.
*/
$User->ban('John');

/*
* As this method checks to see if a user is banned itself, you should
* put this into a conditional statement like so.
*/
if ($User->ban('John'))
{
  // John isn't banned so he will be now!
}
else
{
  // John is already banned, returned FALSE
}

Checking if a user is banned

/*
* When utlising the banning functionality that PHP Utilities offers you will
* need to check to see if a user is currently banned. When you ban a user this
* method is automatically run to see if they are already banned however you may want
* to utilise this method on its own.
*/
if ($User->check_is_banned('John'))
{
  // John is banned
}
else
{
  // John is not banned
}

Unbanning a user

/*
* To unban a user, we simply call the unban method.
*/
$User->unban('John');

/*
* This method also returns either TRUE or FALSE, so we can use it in a conditional.
*/
if ($User->unban('John'))
{
  // John Successfully unbanned.
}

The banWave method

/*
* The final banning method is called banWave, it takes no arguments and simply checks if the
* current logged in user is banned. To use it simply just call it!
*/
$User->banWave();

/*
* Personally though, I would use it after a successful login, otherwise you may get errors.
*/
if ($User->authenticate('John', 'Doe'))
{
  $User->session->login('John');
  $User->banWave();
}

Extras

Here are just some little extra things that you may find useful.

DISPLAYING A USER'S USERNAME:

/*
* Currently to display a user's username, simply use User::username().
* NOTE: This is a static method so call the class directly.
* SUBJECT TO CHANGE THROUGH CODE REFACTORING
*/
echo User::username();

Please report any issues you may find, and also any feature and enhancement requests you may have.

This file is subject to change.

About

Useful everyday PHP classes, that work as one!

Resources

Stars

Watchers

Forks

Packages

No packages published