Hi friends, computer science tutorials some time ago already discussed about CRUD with PHP and MySQL I wrote became part 1 and part 2. Now we will make Simple Login System with PHP and MySQL. How it works Simple Login System is that we will input a username and password and then will connect to the MySQL database. So the steps to make the Simple Login System with PHP and MySQL are as follows:
Create a database with the name tik76 in phpMyAdmin
Create a table tw_login with the following structure
DROP TABLE IF EXISTS `tw_login`; CREATE TABLE `tw_login` ( `id_login` int(11) NOT NULL auto_increment, `username` varchar(32) NOT NULL, `password` varchar(32) NOT NULL, PRIMARY KEY (`id_login`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ; INSERT INTO `tw_login` VALUES (1, 'tutorialweb', 'tutorialweb');
Create a login form and save it as index.php
<html> <head> <title>Simple Login System by TUTORIALWEB.NET</title> </head> <body> <!--/ FORM LOGIN /--> <form action="" method="post"> <table> <tr> <td>Username</td><td>:</td><td><input type="text" name="username"/></td> </tr> <tr> <td>Password</td><td>:</td><td><input type="password" name="password"/></td> </tr> <tr> <td></td><td></td><td><input type="submit" name="login" value="Login"/></td> </tr> </table> </form> </body> </html>
PHP source code for the login process
<?php
// KONEKSI KE DATABASE
$koneksi = mysql_connect("localhost", "root", "root") or die("Tidak bisa terhubung ke Database!");
mysql_select_db("tutorialweb_tutorial", $koneksi) or die("Tidak ada Database yang dipilih!");
?>
<?php
// PROSES LOGIN
if($_POST['login']){
$user = $_POST['username'];
$pass = $_POST['password'];
if($user && $pass){
$cek_db = "SELECT * FROM tw_login WHERE username='$user'";
$query = mysql_query($cek_db);
if(mysql_num_rows($query) != 0){
$row = mysql_fetch_assoc($query);
$db_user = $row['username'];
$db_pass = $row['password'];
if($user == $db_user && $pass == $db_pass){
echo '<p><b>Anda berhasil Login!</b></p>';
// masukkan script lainnya disini
// seperti Session atau redirect ke halaman admin
}else{
echo '<p>Username dan Password tidak cocok!</p>';
}
}else{
echo '<p>Username tidak ada dalam Database!</p>';
}
}else{
echo '<p>Username dan Password masih kosong!</p>';
}
}
?>
If you are still confused can see the video below.
0 komentar:
Posting Komentar