Mining a User Management System in PHP and MySQL

Basic user management—registration, logging in, and logging out—is a key aspect of today’s dynamic websites. This article walks you through some of the simple ways you can extend the code to provide added functionality to your website.

From the author of

As developed in the book PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide, the only real purpose of the database is to validate a user’s credentials when she or he logs in. This is a fine start, but as with any application that’s database-driven, there are always a multitude of ways you can make use of stored data. In this article, I’ll demonstrate a few features you can add using the database as defined in the book, and then show you a few more things made possible by just adding one new column to the database’s users table.

At the very least, such a table needs to store the user credentialsemail address and password being the most common combination. Other particulars about the site’s users, such as names and so forth, would also be put there. From that information alone, a simple COUNT() query can fetch the total number of registered users:

SELECT COUNT(user_id) FROM users

In the book’s specific example, after registering, users receive a confirmation email. To activate the account, the user must click a link in that email, which has the net effect of clearing the active value from the database record. To differentiate between registered users and valid, active users, add a clause to the above query:

SELECT COUNT(user_id) FROM users WHERE active IS NULL

Next, for most database tables, I tend to define a date_added or date_created column, which is an automatically populated timestamp that records when the record was inserted. In this table, that’s registration_date. When you have a column like this in your own tables, another COUNT() query, this time using a WHERE clause, can retrieve the number of people that have registered within a given period of time (say, a day or week):

The WHERE clause checks that the registration_date value is greater than the current momentNOW()minus seven days.

Adding Data to Mine

In the book, one suggestion I makewhich I don’t flesh out thereis to add a last_login column to the users table:

ALTER TABLE `users` ADD `last_login` DATETIME NOT NULL AFTER `active`

This column will be used to reflect the last time each user logged in to the site. After adding the column, you’ll need to change the login.php script so that it updates that column’s value when the user successfully logs in to the site:

Although it doesn’t seem like much, this simple addition brings all sorts of good data to the table (pun intended). For starters, you can now let the user know the last time he or she logged in. To do that, select a formatted version of the last_login value as part of the login query:

SELECT user_id, first_name, user_level, DATE_FORMAT(last_login, '%a, %b %e at %l:%i%p') AS last_login FROM users WHERE (email='$e' AND pass=SHA1('$p')) AND active IS NULL

Once fetched, you can show that date to the user. In the above query, the specific format will be something like Fri, Oct 21 at 11:20AM.

You can also use the last_login column to indicate data about the site’s overall user activity. You can show how many people are currently onlineactively using the siteby counting the number of people that logged in within the past X minutes:

That query specifically checks for people that have logged in within the past 15 minutes, but you can increase or decrease this value based upon the actual site activity and how busy you’d like to make your site look.

NOTE

In theory, you could create a column whose value gets set when a person logs in and is cleared when the user logs out, and then use this value to determine who is online. However, many people don’t always log out of websites, so such information would be less reliable.

Conclusion

The power of databases is that once you’ve stored data, you can retrieve it in a myriad of ways, presenting all sorts of information to administrators and public users alike. Even with a simple example, such as a users table, the data already present can be mined more exhaustively than to just validate each user’s login credentials. And, as you’ve now seen, adding just one more bit of information creates several new opportunities.