|
||||||
How to Create New User Accounts with PHPAdding Users to an Application Using PHP Code and MySQL TablesAn Internet application administrator can obtain information from new users and then create account for them, or they can use PHP code to do it all automatically
One very good reason for using PHP is the way it enables a web site programmer to automate server tasks. Take, for example, the task of creating a new user account. Instead of a system administrator obtaining a new user's details and then entering them onto a MySQL database, PHP will do all of that. Automatically. And it can even check to see if the new user name has already been used or not. The PHP programmer does all of this by writing code that will:
And, this can all be achieved with just a few lines of PHP code. Identifying Whether or Not the User Has Entered Their DetailsThe web page should display:
The decision as to which one to do can be made quite simply: <?php
$username = $_REQUEST["username"];
if (!isset($username)) {
echo "<form method=post>
Enter New Username<input name=username><br>
<input type=submit></form>";
} else {
Here code will be executed only if the user has entered details into a text box and then pressed the submit button. Processing the User InputsIf the user input is to be processed then the code's first job is to connect to the correct MySQL database, for example: $mysql_host = "localhost";
$mysql_database = "my_web_site";
$mysql_user = "my_web_site_user";
$mysql_password = "dt56o96";
mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database);
This can be added into the PHP file itself, but it is much more efficient to save that to PHP file of its own, and then to use the include statement to access the code. Does the User Already Exist?The purpose of this particular PHP script is to create a new user account. It's first job, therefore, should be to ensure that this proposed account does not already exist. This can be done by formulating an appropriate SQL statement (and assumes that a table "USERS" already exist on the MySQL database): $SQL = "select * from USERS where username='$username'";
And then running this query on the database: $result = mysql_query($SQL) or die (mysql_error());
The programmer can now use the PHP mysql_numrows function to obtain the number of rows returned in the recordset: $num = mysql_numrows($result);
If the number returned is not zero then the user name already exists. Creating a new User NameIf any rows are returned from the above query then the user should be notified that their chosen user name has already been used: if ($num > 0) {
echo "Username already exists<br>";
However, if the user name is available then a simple insert statement can be used to create it: } else {
$SQL = "insert into USERS (username) values ('$username')";
mysql_query($SQL) or die (mysql_error());
}
And then then connection to the database can be closed: mysql_close();
}
?>
If the programmer saves all of this into a PHP web page (for example "add_user.php") then they will have produced an easy and effective way of users being able to create their own accounts on any Internet based application.
The copyright of the article How to Create New User Accounts with PHP in PHP Programming is owned by Mark Alexander Bain. Permission to republish How to Create New User Accounts with PHP in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||