Curriculum Vitae / Introduction / Tutorials and other resources for web and E-Commerce programming!

Sitemap

Why this website?

Technologies used

 

The On Demand Global Workforce - oDesk

Ask for a help in Smarty

Ask for service quotation

 

 

Check email

in Gaindakot

in Gaindakot
(Government)

 

If you think of working with me, contact me by an email now, and you need to fill up your project needs here.

Accredited degrees online

Certified expert level of workmanship :-)

My approved qualifications for internet applications, and your business: You will need to login as a service buyer.
oDesk Certified MySQL Database Administrator oDesk Certified Advanced Php Developer oDesk Certified PHP4 Developer oDesk Certified PHP5 Developer

You will find more reasons to satisfy if you work with me. Save your budget. Find an expert level solution.

Instant Contact

Leave me a message right now

Proven expertises in: MySQL Stored Procedures, Regular Expressions, Smarty Template Engine, CURL, Data processing n Sanitization, Debugging (are your projects abandoned?), and high quality programming in shorter time period. Please ask for a detailed information if you needed it. Need a support? Ask me now. Let us discuss it.

Professional Community Presence

Watch my forum updates in Smarty. I frequently share my skills at the Smarty Forum.

Send your business requirements

Please post your needs here, and our team will contact you at our earliest.

| Home » CV | My Tutorials | My Articles | Glossary | My Photos | Contact Me

 

Tutorial Menus

Power of PHP | Database Connection | Database Class | HTML Spam Filter | Domain Name | Database based *CONFIG* variables | Synchronizing Databases | Mistakes with website dates | Random Users | URL GET Class | MySQL Class | DB to HTML Form | Link Roller

Written By:
Bimal Poudel,
Location: Kathmandu, Nepal.
Email: poudel@bimal.org.np < Please leave me any suggestions you can have! >

I am happy to receive your PHP codes and put it into this site, under your own name. You can send them in my email. I just want to make this site rich with quality PHP tutorials.


Creating Random Users
(c) Bimal Poudel

Theme: We are going to make an automated login system with random (un-guessable) usernames and passwords for applications like online examination for Intranet.

Say, you have at least 10 computers in your office, or school, or in the traing center or in the examination hall, or at least in the computer laboratory. We assume that they all are in a perfect network. Our system below will now let 10 students log into the system with a random user name and password for them. This is similar like conducting an online examination within the netwrok of the office or the college.

Our basic theme is to generate a radnom username and password for the students taking the online examination. The system allows the students to login into our examination network. They all have a common time, with a range of 10 minutes, and all students must login within that time frame. However, we are not discussing to take the examination or schedule the questions.

Now, install the MySQL database server in one of the computer, and a webserver in the other (preferebly, same as the database server). Then configure the webserver to parse the .php files. This is a common platform we assume before generating the random usernames and password for the students.

There should be a communication possible between PHP and MySQL.

Then create a database called "random" in the MySQL with the command: "CREATE DATABASE random". Then create a temporary (or let it be permanent) table to store the random user names and corresponding passwords. The user names must the UNINUQ, ie. non-repeating in nature. You can define it in the MySQL table under the database "random" with the script like:

CREATE TABLE random.random_users (
user VARCHAR(10) NOT NULL UNIQUE,
password VARCHAR(10) NOT NULL,
flag CHAR(1) NOT NULL DEFAULT 'N',
from_time TIMESTAMP,
to_time TIMESTAMP,
ip VARCHAR(15)
);

This should create the table look like as below, if you give the "DESC random_users" command.

+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| user      | varchar(10) |      | PRI |         |       |
| password  | varchar(10) |      |     |         |       |
| flag      | char(1)     |      |     | N       |       |
| from_time | timestamp   | YES  |     | NULL    |       |
| to_time   | timestamp   | YES  |     | NULL    |       |
| ip        | varchar(15) | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

If you want to put your data into the table, you can simple call the following sql in the MySQL pormpt as:

mysql> INSERT INTO random_users VALUES('student1', 'passone23', 'N', '2005-05-10
13:30:00', '2005-05-10 13:45:00', '192.168.0.10');
Query OK, 1 row affected (0.05 sec)

The result is that you have inserted a new entry tot he table random.random_users. You now guess, what the user and password columns in the table are meant for. Similalry, we have a very specific meaning to the flag, from_time, to_time and ip columns of the table as well. The flag column with the value 'N' is used to login for the user for the first time. When the student logs in, we set this variable to 'Y' and block any more logging using the same user name and password. We allow the student to login into the system within the date range defined by from_time and to_time. Remember, from_time is always smaller, and to_time is few minutes more than from_time value. We generate many random values for user and password fields, and give that to the different students. Each student is provided a different username. Let us not just consider the password right now.

The example above means that the exam is scheduled on 10th May, 2005, and the time of the exam is 1:30 PM. The student can enter into the exam hall (our network of computers) no later than 1:45 PM in the same day of examination. The column ip is used to record the Internet Address of the computer the student used to login, so that the teacher can identify the answers given by the different students.

In the network, each of the computers are having their own unique ip address, that look similar to 192.168.0.XXX, where XXX may be the 3 digits (maximum) number to identify the particular computer within your network. PHP can easily read their IP address, to determine, who is connecting to the examination web server and the database server.

Designing the intensive application for the online testing is beyond the scope of this text. We are just creating the random values of usernames and password to access this examination server within your network. Say, 10 students are taking the examination, and we have to give them 10 different user names and password to access the online examination services. Now, let us do that first! But remember, you MUST always generate the random users more than the actual number of students, because, some student may lose it or type it wrognly. This should not be the case, because a such situation would deprive the particular student to take the examination. You can so generate, 20 or 30 or more random users and passwords, and give them only one at a time. You can use the rest, if a student is unsuccessful to use those values, or s/he lost those.

Here is a sample PHP script to populate the table: random.random_users with 20 unique usernames and passwords. Please look at it very carefully. Remeber the SQL string in the above, to see how we had inserted one record into the table.

Open a file called random_users.php in the editor in the web root that can be accessed as http://localhost/random_users.php in the webserver. Type as below. But the values in the parameters may be different for your own computer.

<?php

# Database parameters

$__database__ ='random'; # MySQL Database for us
$__user__ ='root'; # MySQL user name
$__password__ ='PaSsWoRd'; # MySQL password
$__host__ ='localhost'; # MySQL Host

# PHP connects to the database, and stores it in the $link_id.
$link_id=mysql_connect($__host__, $__user__, $__password__);

# Be specific to our won database called: 'random'.
mysql_query('USE '.$__database__);

# Now your are connected to database: random.

# Empty the table: random_users for the refreshed use.
mysql_query('TRUNCATE random_users', $link_id);

# Now create 20 random users from the FOR loop
# But, actually, we have 10 students to take the exam.
for($i=1; $i<=20; ++$i)
{
$random_sql="
INSERT INTO random_users VALUES(
SUBSTRING(MD5(".$i.microtime()."), 5, 7),
SUBSTRING(MD5(".microtime().(1000-$i)."), 10, 7),
'N',
'$from',
'$to'
);";

# The SQL has been just built. Now fire it!
mysql_query($random_sql, $link_id);
}

# For better readability, CAPITALISE the username and password
mysql_query(
'UPDATE random_users SET user=UPPER(USER), password=UPPER(password);',
$link_id
);

echo('20 Users created!');

?>

Okay, you are almost done. Now pull the PHP file from the webserver as: http://localhost/random_users.php. While you visit this page, the PHP connect to the database, and puts 20 different records into the database. If you do not belive it, get back to the MySQL client and command: "SELECT * FROM random.random_users"; You will see a list something similar to below. But never worry, I will tell you how that works. Here is the mysql result after visiting the page two times:

mysql> \T c:\random.txt
Logging to file 'c:\random.txt'
mysql> select * from random.random_users;

+---------+----------+------+---------------------+---------------------+-----------------+
| user    | password | flag | from_time           | to_time             | ip              |
+---------+----------+------+---------------------+---------------------+-----------------+
| D456001 | 6AFB091  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| 0264DEB | 53E9F8D  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| 3B606F5 | 46AA6A1  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| BF87EF5 | 8932592  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| 0ACC3E5 | 6C94001  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| 83A37C3 | 8E894CF  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| 9B1A6A1 | F7BFF74  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| 8F8EF4C | 1F1207F  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| BF0ACEA | E7BDD60  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| 723DD54 | 6C5166F  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| A2201DD | B650221  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| BD49C30 | CA44D46  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| 8AE51F5 | 76DF0A0  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| F314BE5 | D14C1BC  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| A1FA35E | 07A0D13  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| 87A1807 | 6DE1E8C  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| DA591A0 | CCF50EA  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| F4E8521 | 624EE69  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| 74030C2 | 17E889B  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
| B4066FD | CB2D6F4  | N    | 2005-02-21 21:59:49 | 2005-05-10 13:45:00 | 000.000.000.000 |
+---------+----------+------+---------------------+---------------------+-----------------+
20 rows in set (0.00 sec)

mysql> \t

[ RE-LOAD HTTP://LOCALHOST/RANDOM_USERS.PHP AGAIN ! ]

mysql> \T
Logging to file 'c:\random.txt'
mysql> select * from random.random_users;

  +---------+----------+------+---------------------+---------------------+-----------------+
  | user    | password | flag | from_time           | to_time             | ip              |
  +---------+----------+------+---------------------+---------------------+-----------------+
  | F94F074 | 8870594  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | 07EA274 | 75407EB  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | 2016347 | FCD2CD3  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | E4AAB81 | E78FE35  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | E54CAE4 | 94DF6A2  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | A05E3D3 | 6696F41  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | 3D01354 | 7751DBF  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | F1DC417 | 587B5E7  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | E73CA74 | 291BF66  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | FF92360 | AF425D8  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | 81B4262 | F907368  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | 50D0C22 | C4BC5E6  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | 1D2C1EF | 4C36D99  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | 432A552 | 8FECD05  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | 8ECBBD0 | 26E130A  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | 9F46421 | 5A715EE  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | F89DFA3 | 73157C2  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | 1B8C1EF | 89661F7  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | 5A8EEB2 | 87C2980  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  | 4F8E811 | 6A70A0C  | N    | 2005-02-21 22:01:17 | 2005-05-10 13:45:00 | 000.000.000.000 |
  +---------+----------+------+---------------------+---------------------+-----------------+
  20 rows in set (0.05 sec)

Have you noticed that, each time you visit the web page, you get refreshed table in the database! So, the values in teh user and password column are never the same. Yes, that is what we have done! It is a combined work of PHP and MySQL. PHP or MySQL only are able to produce random strings themselves only, but they may not be of our use!

Look into the for() loop in the PHP code above. (Do not get confused with the single and double quotes used at there.) The variable $random_sql is always holding a valid SQL for different loops called by the for(). micortime() reads the current Micro Time of the system clock of the web server (where PHP is loaded). But the for() loop may complete within one micro second, and thus yeilding same values for all of the loops. So, we have used a technique of using the for() loop indicator variable: $i, and 1000-$i. So, for each loop, the combination supplies the different value, and that is passed to the MySQL.

MySQL then calcualtes the Message Digest string with MD5() function. It returns the 32 bytes long result. Since, it is too long to remember or type, we have taken only 7 characters for username and password with the help with SUBSTRING, and that start from position 5 and 10. Check in the PHP script above. Though it is a little long process, we can be sure of generating the UNIQUE values for the user names and passwords. Are not we? Also, new value are too unique, if you re-generate them from http://localhost/random_users.php page. Below is one more screen shot from MySQL shell for you to prove that the MD5() function generates different values for different strings we pass to it.

mysql> SELECT MD5('test');

  +----------------------------------+
  | MD5('test')                      |
  +----------------------------------+
  | 098f6bcd4621d373cade4e832627b4f6 |
  +----------------------------------+
  1 row in set (0.05 sec)

mysql> SELECT MD5('This is a long text to pass message digest!') AS result;

  +----------------------------------+
  | result                           |
  +----------------------------------+
  | a07aac4897c904ca93782704bd5f576d |
  +----------------------------------+
  1 row in set (0.00 sec)

You can now ask the usage of this entrie article; what's the use then? Yes, it is now your job to imply this set of random user names and passwords into any of your password protected web applications like, online examination. Use the table random.random_users to login the examination taking students. But before starting to log in, you MUST give the random username and corresponding password to your student, just before you allow you them to sit for the examination. You can print this data, cut into smaller pieces without damaging the username and password values, and give each piece to the student. The student then logs into your system with the combination of username and password you have given.

But while logging, you must check that the time logged in is within the date/time range defined in the from_time and to_time columns, and the flag is with 'N' value. When the student gives the correct password for the valid username, please do some more things like below, to add extra security to your system:

  • Reset the login time range to '0000-00-00 00:00:00' both
  • Reset the flag value to 'Y'
  • Reset the '000.000.000.000' value in the ip with the real IP address of the computer who just connected to your systm. Hint: the variable: $_SERVER['REMOTE_ADDR'] gives you the IP address of the client browser.
  • Optionally, reset the user and/or password columns to some other random values so that no body can login twice using the same user name and password.

This mehtod strictly prohibits the double login using the same username and password. If you destrory the data (one at a time) when the login was successful, no re-login is possible. You must stricly warn your students to type the username and password very carefully, and NEVER press the BACK button in the web browser in the first page, because they will lose their login. If any such accidental cases arise; hmm; you are in the safer side: because you have already created 20 users for 10 students! But, once you have printed this random list and gave students their username and password, you MUST NOT regenerate the random users again, until the exams are over.

You can use this logic not only for online examinations, but also in some other beautiful applications like, lottery system, scratch and win programs (as done by the noodle manufacturers), and for more examples, think yourself. One best example could be like this: IT world (this magazine) wants to give something ONLY FOR its readers. The team publishes such random users, prints them, cuts into pieces, and sticks each piece in the magazines middle of the page, so that it is available only for its readers! Same thing can be (is) done for the Internet using CD ROMs that you can purchase in the market for Everest-Net or InfoCom.

And, finally, how do you love this (great) idea? Just, email me before you go to create your own system now. And, would you tell me what you are going to do with this logic? Bye for now.

Bimal Poudel
PHP Programmer

10:56 PM 2/21/05

  website and the contents unless speficied are owned by - Bimal Poudel. All rights reserved.
This website contains personal materials. Contact Me. Last modification: 2007-03-01 06:02:05