Script users register, login

( From: http://coursesweb.net )

This is a PHP script to Register and Log users in a website, it's created with the method based on classes and objects (OOP). It uses PHP, MySQL (with PDO) and Ajax / JavaScript (with jQuery effects) [it can work also in browsers with JavaScript disabled]. The HTML code is valid HTML5, also, works in Internet Explorer browsers which don't recognise HTML5 tags
The script includes button "Remember" that gives the posibility to automaticaly login the users in the next visits.
Also, it has option to Log in with Facebook, Yahoo, or with a Google account.
The connection to MySQL database is made with PHP PDO, so, the PHP server must have the PDO module installed and enabled.
If the user forgot the password, the script can send it to his e-mail.
By default, this script is set to send a message to the user's e-mail, after the registration, with a link to confirm the registration, but this setting can easily be reset to let the user to log in imediately after registration, without any confirmation.
The script can send the emails via a local mail server (with mail() function), or through SMTP via an GMail account, with the PHPMailer class.
Each user has a personal page that contains: an image uploaded by the user, the registration date, the last visit date, the number of visits /logins, and other personal data (name, pronoun, birthday, personal website, occupation, hobby).
In his personal page, the user can receive messages added by other users (will be notified by email), and can add 12 favorite links.
The logged user can delete the messages in his page, can modify the password and the e-mail address used to registration, then he/she will receive an email with the new data.
The messages posted in the user's page are paginated, and can be included smiles, an image (displayed in full page when click on it), the text can be formated with Bold, Italic, Underline (with BBCODE).
The user which writes the message can select the option to receive email notification when new message is added.
The script displays the total number of users, the last registered user and the Online users (updated with Ajax every 2 minutes) with link to their page.
The script contains a page that shows a table with all registered users, that are paginated and can be ordered by Name, Last Visit, Registration date, Visits number, and Rank.
When the tables are created in database, it is also created a first account for Admin, with Rank 9, and the Name, Password, and E-mail set in the "config.php" file (in "usrincls/" directory).
The Admin can delete users, modify their data, adds /delete messages and favorite links, and change their Rank, from -1 to 9 . The Rank -1 means banned, 0 is unconfirmed account, and a Rank 9 gives to the user Administration privileges.

Installation and usage

Perform exactly the following 5 steps:
1. After you have unzipped the archive "register_login_script_users_online.zip", open the "config.php" file (in the "usrincls/" folder) and change the following data for connecting to MySQL database, with your data.
- $mysql['host'] = "localhost";               - instead of localhost add your MySQL server address.
- $mysql['user'] = "userdb";                   - instead of userdb write your username for database.
- $mysql['pass'] = "passdb";                  - instead of passdb write your password for database
- $mysql['bdname'] = "dbname";          - instead of dbname add the name of your database.

- The following codes are for the Administrator data:
// Set administrator login data, and e-mail
define('ADMINNAME', 'admin');                      // the admin name (at least 3 characters)
define('ADMINPASS', 'pass123');                    // the admin password (at least 7 characters)
define('ADMINMAIL', '[email protected]');         // Here add the Administrator e-mail
- The sender email address which appears in registration confirmation message or in the email for recovery password, is the Admin's e-mail added in the constant "ADMINMAIL (here "[email protected]").
- If the server of your website can't use mail() function to send emails with link to confirm registration, or recovery user's data, and notification when new message are added; the script has implemented the PHPMailer class to send the mails via GMail.
So, if you want the script to send the mails via GMail, and you have a GMail account, set the value of USEGMAIL constant to 1, and add your GMail Username and Password to GMAILUSER, and GMAILPASS, to the following code in "config.php":
// If you want to use a GMail account for sending mails to user (Link confirmation, Recovery data)
// set USEGMAIL to 1, and add your GMail Username and Password to GMAILUSER, and GMAILPASS
define('USEGMAIL', 0);
define('GMAILUSER', '[email protected]');
define('GMAILPASS', 'gmailpass');

- Settings for log in with Facebook, Yahoo, Google

- To add the "Button for Login with Facebook"
First you need a Developer APP ID, and Secret Key, from this page: https://developers.facebook.com/apps/ (after you login in facebook)
Click "Register as a developer", then you need to confirm your phone number.
For more details, see the documentation: Facebook Login for Server-side Apps
Then, in "config.php" set the value of FBCONN constant to 1, and add your FaceBook ID Developer (APP ID) to FBID, and the Secret Key (APP Secret) to FBSK .
// If you want to add Connect with FaceBook,
// Set FBCONN to 1, and add your FaceBook ID Developer (APP ID), and the secret key (APP Secret)
define('FBCONN', 1);
if(FBCONN == 0) {
  define('FBID', 'YOUR_APP_ID');
  define('FBSK', 'YOUR_APP_SECRET');
}

- By default, the script include "Button for Login with Yahoo". If you want to remove it, set the value of YHCONN constant to 0 (in "config.php").
// Value of 1 include button Connect with Yahoo, 0 removes it
define('YHCONN', 0);

- By default, the script include "Button for Login with Google". If you want to remove it, set the value of GOCONN constant to 0 (in "config.php").
// Value of 1 include button Connect with Google, 0 removes it
define('GOCONN', 0);
If you get "Forbide, 403 error" in redirected page from Yahoo or Google login, the problem is from a configuration in Apache server, mod_security is set up to block URLs in querystrings. In this case, contact server administrator to remove the blocking of URLs in querystrings.

- The following lines of code set options for user account

// if RANK is 0, the script will send a link to the user's e-mail, to confirm the registration 
// if RANK > 0, the user can log in immediately after registration
define('RANK', 0);
define('ACCOUNT', 1);       // If the value is different from 1, allow to create multiple accounts with same IP
define('ALLOWIMG', 1);                             // allows upload images in message (1), not allow (0)
define('ALLOWMAIL', 1);                            // allows mail notification when new message (1), not allow (0)
define('ROWSPAGE', 12);                            // numbers of messages displayed in the page
// HERE you cand edit the permissions for the image uploded by User
$imguprule = array(
  'dir' => 'usrimgup/',                // directory to store uploded images
  'allowext' => array('gif', 'jpg', 'jpe', 'png'),        // allowed extensions
  'maxsize' => 200,       // maximum allowed size for the image file, in KiloBytes
  'width' => 800,         // maximum allowed width, in pixeli
  'height' => 600         // maximum allowed height, in pixeli
);
- If you want the user to confirm the registration to his e-mail address, let the value '0' at the constant RANK, so, after registration, a message will be sent to his e-mail with a link through which the user can confirm the registration. He can log in only after confirmation.
- If you want to remove the requirement of registration confirmation, replace the '0' with '1' (or other positive number) at the constant: RANK

- The elements of the Array variable: $imguprule define the allowed values for the image that can be uploaded by user in his page, and in message (directory name in which the images are saved ["usersimg"], allowed file extension, maximum size in kilobytes and the maximum dimensions allowed).

2. After you have added and saved your data for MySQL connection, copy on your server the "users.php" file (the "test.php" file can be used for test), and the directories: "icos", "usrimgup", "usrincls", "usrjs", and "usrtempl" (with all their content, in the same directory in which the file you want to include the script is).

3. Set to the directory "usrimgup", and to the file "ltmsgs_reset.txt" (in the folder "usrimgup/"), read-write-execute permissions for PHP (by setting CHMOD 0777 (or 0755) with your FTP application).
- The "usrimgup/" directory stores the images uploaded by the user, and the "ltmsgs_reset.txt" file stores the last reset date of the checking messages by Admin.
When the Admin logs in, in the user page, under messages appears a link "Latest Messages Posted to all Users". After you click on this link, will open the file "latestmsgs.php". This file will show all the messages added in all the user pages from the last reset. To reset the time of the last checking, you must add the admin password in the text box at the bottom of the page "latestmsgs.php", then click "Reset" button.

4. Access in your browser the file "create_tables.php" (in "usrincls/" folder) to create the tables in your database
(fo example, access in your browser - http://domain/usrincls/create_tables.php ).
If the tables are successfully created, you'll see a confirmation text.

5. Then add the following codes in the PHP file in which you want to use this script: - In that file, where you want to appear the login form, add the following code:
<?php echo $login; ?>
- In the place you want to appear the Total number of users, Newest user, and Online users, add:
<?php
echo $objUsers->users['total'];
echo $objUsers->users['last'];
echo $objUsers->users['online'];
?>

- You can see how this code is added in the file "test.php".

- If you want to use the script in other language, just translate the texts in the file "texts.php" (in "usrincls/" directory).
- To change the number of paginated rows with users displayed in the page with all registered users, modify in "allusers.php" (in "usrtempl/" directory), at line 5, the value of the following variable:
$objUsers->rowsperpage = 30;               // number of users paginated in the page with all users
- To change the logo image, modify the image in "logo.gif", in "icos" folder.

Security features

How to display in your web page a content for visitors and another content for logged users:

How to display a content in your web page according to the user's rank

Script structure

Tables in database

This script uses 6 tables in a MySQL database (the connection is made with PDO):
  1. users - has the following columns:
  2. fbusers - For users loged in with Facebook; has the following columns:
  3. gousers and yhusers - For users loged in with Google, and Yahoo; have the following columns:
  4. logattempt - used to limit the number of failed login attempts (stores the name, the number of login attempts, the user IP, and date). The attempts older than 10 minutes are deleted.
  5. usersdat - stores the optional user's data (name, pronoun, address, birthday, occupation, hobby, ...), in the following columns: name, pronoun, country, city, bday, ym, msn, site, img (image's path and name), ocupation, interes, transmit, fav (favorite links, into a string with JSON format).
  6. useron - stores the online users. Updated with Ajax every 2 minutes.
  7. msgs - to store the messages added in user page. Having these columns:

Directories and files


- This script is free (does not provide assistance or personal modifications).

It was successfully tested for a general configuration on localhost with XAMPP (with Internet Explorer, Mozilla Firefox, and Opera), and also on the server of the website coursesweb.net. If on other systems does not work properly, depends on their configuration. Problems can appear especially on free hostings which have restrictions on the "session", or not support PDO.

Respectfully, Marius
    coursesweb.net