Case Study : Creating a Multi-User Login System with PHP and Mysql.
Requirements : Webserver Packages (AppServ, XAMPP, EasyPHP, etc)
Step 1 : Preparing the Database
- Prepare a database with the name db_multiuser.
- Create a table named tb_user, create the fields – fields like the following picture,
- Insert some sample data to tb_user.
INSERT INTO `db_multiuser`.`tb_user` (`id`, `username`, `password`, `tipe`) VALUES (NULL, 'ani', 'ani','admin'), (NULL, 'budi', 'budi', 'operator');
Step 2 :Create the configuration file
- Type the following script,
<?php
$host = "localhost";
$user = "root"; //adjust to your mysql user
$pass = ""; //i use no password in my mysql
$dbName = "db_multiuser";
mysql_connect($host, $user, $pass);
mysql_select_db($dbName)
or die ("Connect Failed : ".mysql_error());
?> - Save with the name connect.php
- Explanation: connect.php is a configuration file that would link the login system that we created with mysql database.
Step 3 :Create the login form
- Type the following script,
<center>
<h2> <b> Login Page </b> <h2><form method="post" action="proses.php"><table border="0" align="center" cellpadding="5" cellspacing="8"><tr bgcolor="orange">
<td> UserName! </td>
<td> <input name="username" type="text"> </td>
</tr>
<tr bgcolor="orange">
<td> Password ! </td>
<td> <input name="password" type="password"> </td>
</tr>
<tr>
<td> </td>
<td> <input name="submit" type="submit" value="login"> </td>
</tr>
</table>
</form>
</center> - Save with the name formlogin.php
- This script will display a login form for username and password fields. Consider the
action="proses.php"
. This shows that the entries from the username and password will be processed (given the action) by proses.php file. This file will be created in the next step.
Step 4 :Create the processing file for login
- Type the following script,
<?php
session_start(); //startsession
include "connect.php";//get the username and password from the form
$username = $_POST['username'];
$password = $_POST['password'];
//query for retrieve the user data from database according to username in the form
$q = "SELECT * FROM tb_user WHERE username = '$username' ";
$result = mysql_query($q);
$data = mysql_fetch_array($result);
//checking the password
if ($password == $data['password']) {
//save usertype & username in the session
$_SESSION['tipe'] = $data['tipe'];
$_SESSION['username'] = $data['username'];
include "menu.php";
}
//If password doesnt match
else {
$warning = "Wrong Username / Password";
echo $warning;
}
?> - Save with the name proses.php
Step 5 : Make a display special for user
- Type the following script,
<?php
session_start();
include "connect.php";
echo "<center>";
echo "<h3> Menu </h3>";
if ($_SESSION['tipe'] == "admin") {
echo " Anda Login sebagai Admin";
}
else if ($_SESSION['tipe'] == "user") {
echo " Anda Login sebagai User";
}
?> - Save with the name menu.php
- Explanation: This script will display the information in accordance with the users who are logged.
Step 6 : Running the Login System
- Go to http://localhost/
- Select file formlogin.php to start running the application. You will see a display like the following,
- Set the username = ani; password = ani
- System will display the description as follows,
- If you log in as an operator (username: budi; password: budi), it will display information as shown below,
NB. Save all the php file from step 2 – step 5 into the SAME ONE folder.
Okay then, happy coding
Hal yang juga menarik:
Hak Cipta
Semua skrip dan teknik dalam artikel di itx.web.id boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.
bagus bgt ne…patut dicoba
Thankfulness to my father who informed me regarding this blog,
this blog is in fact awesome.