Simple user registration, log-in and operation authentication framework

This page is a functional demonstrator, where you can test the various functions of the user framework. On the left you can register new users, log in, update your email address or password, log out, or request password resets.

On the right you can see information pertaining to you, as current user. When registering or logging in it will show your session identifier and authentication status, POST information received by the user system, as well as the system's information and error log content. The code can be downloaded from github.

user name
email address
password
password (again)
user name
password
email address

session token for guest user: -1
(authenticated: no)


POST: Array
(
)


INFO LOG: Array
(
)


ERROR LOG: Array
(
)

Using the system

The authentication script is very simple in use. Simply have the user.php script sitting somewhere that your other scripts can access it, and then start any independent PHP script with these lines:

	require_once("user.php");
	$USER = new User();

From that point on, there will be a $USER variable available in your script, with the following properties:

There is a second way to create a new user, which allows you to pass the name of the function that should be called when a new user registers with the system. This is something you typically use only in the script that acts as main entry point:

	require_once("user.php");
	$USER = new User("registration_completed_function_name");

This will call your function as registration_completed_function_name($username, $email, $userdir), providing it with the newly registered user's username, email address and data directory location. This lets you hook into the registration process to ensure that any tasks that you need performed when a new user joins (such as file or database manipulations for your own system) can be triggered.

Note that it doesn't matter whether you use a single entry script, or several distinct scripts. If you have six scripts to perform various tasks as separate files (meaning they don't require or include each other), simply add the require_once and new User() lines at the start of each file, and each script will be able to deal with session-authenticated users. Simply make tasks conditional on $USER->authenticated, and things will just work. This includes scripts that you only call through GET and POST request via XHR ("ajax" requests).