Kita sering lihat bukan ketika mau masuk ke e-mail, facebook, twiter, dsb kita di haruskan terlebih dahulu untuk login/sign-in akun kita? Nah, pertanyaan pernahkah dibenak Anda (bagi programmer) terpikir bagaimana cara membuat yang seperti demikian?
Kali ini saya akan membahas tentang bagaimana cara membuat Web Login sederhana dengan PHP. Di kemas dengan bahasa yang ringkas dan ringan mudah-mudahan tutorial ini membantu bagi kita yang baru belajar atau pemula dalam pemrograman PHP.
Pertama Anda buat dulu SQL nya di phpMyAdmin dengan skrip berikut:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | -- phpMyAdmin SQL Dump -- version 3.5.2 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Nov 19, 2012 at 06:10 PM -- Server version: 5.5.25a -- PHP Version: 5.4.4 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `login` -- -- -------------------------------------------------------- -- -- Table structure for table `admin` -- CREATE TABLE IF NOT EXISTS `admin` ( `id` int(11) NOT NULL AUTO_INCREMENT, `nama` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; -- -- Dumping data for table `admin` -- INSERT INTO `admin` (`id`, `nama`, `password`) VALUES (1, 'admin', '21232f297a57a5a743894a0e4a801fc3'); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
Setelah itu Anda buat file koneksinya dengan nama “connect.php” kemudian Anda copas (copy-paste) kode di bawah ini dengan notepad, atau dengan program lainnya yang biasa Anda gunakan misalnya Dreamweaver, Geany, dll.
connect.php
1 2 3 4 5 6 7 8 | <?php $server="localhost"; $username="root"; $password=""; $database="login"; mysql_connect($server,$username,$password) or die("Error : Gagal Koneksi ke MySQL"); mysql_select_db($database) or die("Error : Database tidak ditemukan"); ?> |
Kemudian Anda buat style yang berguna untuk mempercantik web login Anda copy-paste-kan kode di bawah ini dengan Notepad, lalu kemudian save dengan nama style.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | body{ /* setting background */ background-color:#f1f1f1; /* set font etc */ font: 12px Verdana, Arial, Helvetica, sans-serif; color: #666666; /* set full page */ padding:0px; margin:0px; } #wrap_login{ /* wrapper login */ height:150px; margin-top:10%; /* background */ background-color:#FFFFFF; background: url('img/body.gif'); background-repeat:repeat-x, repeat-y; } #wrap_login #login_form{ border:0px #000 solid; -moz-box-shadow: 3px 3px 5px 6px #ccc; -webkit-box-shadow: 3px 3px 5px 6px #ccc; box-shadow: 3px 3px 5px 6px #ccc; /* jarak atas dan bawah */ padding-top:30px; padding-bottom:250px; padding-left:35px; /* jarak kanan dan kiri */ margin-left:30%; margin-right:33%; background:white; } #input{ border:0px #000 solid; float:left; display:inline; /*margin-left:10px;*/ } #input h1 { color: blue; margin: 0px 0px 5px; padding: 0px 0px 3px; font: bold 20px Verdana, Arial, Helvetica, sans-serif; border-bottom: 1px dashed #E6E8ED; text-shadow: 1px 2px #ccc; } #gembok{ border:0px #000 solid; float:left; display:inline; margin-left:10px; padding-top:45px; } .textfield { font:Verdana, Geneva, sans-serif; font-size: 13px; color: #333333; background: #F7F7F7; border: 1px solid #CCCCCC; padding-left: 1px; padding:10px 10px; width:200px; background:#F7F7F7; border-bottom: 1px double #171717; border-top: 1px double #171717; border-left:1px double #333333; border-right:1px double #333333; background:#FFFFFF url(img/group.png) no-repeat 4px 4px; padding:4px 4px 4px 25px; border:1px solid #CCCCCC; width:230px; height:18px; } .textfield2 { font-size: 13px; color: #333333; background: #F7F7F7; border: 1px solid #CCCCCC; padding-left: 1px; padding:10px 10px; width:200px; background:#F7F7F7; border-bottom: 1px double #171717; border-top: 1px double #171717; border-left:1px double #333333; border-right:1px double #333333; background:#FFFFFF url(img/key.png) no-repeat 4px 4px; padding:4px 4px 4px 25px; border:1px solid #CCCCCC; width:230px; height:18px; } a { color: #2D3954; cursor: pointer; } a:hover { color: #99CC00; } .err { color: #FF9900; } th { font-weight: bold; text-align: left; } #warning{ text-align:center; font-weight: bold; color: red; } /* CSS Untuk Home Page */ #wrap_home{ width:800px; margin:10px auto; } #home_header{ height:50px; background-color:skyblue; border: 1px solid #ccc; -moz-box-shadow: 1px 3px 3px 1px #ccc; -webkit-box-shadow: 1px 3px 3px 1px #ccc; box-shadow: 1px 3px 3px 1px #ccc; } #home_header h1{ text-align:center; font-size: 20px; text-shadow: 1px 1px #f1f1f1; color:black; } #home_sideleft{ margin:5px 0 0 0; float:left; width:200px; height:400px; border: 1px solid #ccc; -moz-box-shadow: 1px 3px 3px 1px #ccc; -webkit-box-shadow: 1px 3px 3px 1px #ccc; box-shadow: 1px 3px 3px 1px #ccc; background-color:white; padding-left:10px; } #home_content{ margin:5px 0px 0 5px; float:left; width:531px; height:400px; border: 1px solid #ccc; -moz-box-shadow: 1px 3px 3px 1px #ccc; -webkit-box-shadow: 1px 3px 3px 1px #ccc; box-shadow: 1px 3px 3px 1px #ccc; background-color:white; padding:0px 0px 0px 50px; } #home_footer{ clear:both; margin:415px auto; width:800px; border: 1px solid #ccc; height:40px; background-color:skyblue; -moz-box-shadow: 1px 3px 3px 1px #ccc; -webkit-box-shadow: 1px 3px 3px 1px #ccc; box-shadow: 1px 3px 3px 1px #ccc; } #home_footer p{ margin-top:10px; text-align:center; font-weight:bold; text-shadow: 1px 1px #f1f1f1; } /* BUTTONS */ .buttons a, .buttons button{ display:block; float:left; margin:0 7px 0 0; background-color:#f5f5f5; border:1px solid #dedede; border-top:1px solid #eee; border-left:1px solid #eee; font-family:"Lucida Grande", Tahoma, Arial, Verdana, sans-serif; font-size:12px; line-height:130%; text-decoration:none; font-weight:bold; color:#565656; cursor:pointer; padding:5px 10px 6px 7px; /* Links */ } .buttons button{ width:auto; overflow:visible; padding:4px 10px 3px 7px; /* IE6 */ } .buttons button[type]{ padding:5px 10px 5px 7px; /* Firefox */ line-height:17px; /* Safari */ } *:first-child+html button[type]{ padding:4px 10px 3px 7px; /* IE7 */ } .buttons button img, .buttons a img{ margin:0 3px -3px 0 !important; padding:0; border:none; width:16px; height:16px; } /* STANDARD */ button:hover, .buttons a:hover{ background-color:#dff4ff; border:1px solid #c2e1ef; color:#336699; } .buttons a:active{ background-color:#6299c5; border:1px solid #6299c5; color:#fff; } /* POSITIVE */ button.positive, .buttons a.positive{ color:#529214; } .buttons a.positive:hover, button.positive:hover{ background-color:#E6EFC2; border:1px solid #C6D880; color:#529214; } .buttons a.positive:active{ background-color:#529214; border:1px solid #529214; color:#fff; } .urbangreymenu{ width: 190px; /*width of menu*/ } .urbangreymenu .headerbar{ font: bold 13px Verdana; color: white; background: #606060 url(img/arrowstop.gif) no-repeat 8px 6px; margin-bottom: 0; text-transform: uppercase; padding: 7px 0 7px 31px; } .urbangreymenu ul{ list-style-type: none; margin: 0; padding: 0; margin-bottom: 0; } .urbangreymenu ul li{ padding-bottom: 2px; } .urbangreymenu ul li a{ font: normal 12px Arial; color: black; background: #E9E9E9; display: block; padding: 5px 0; line-height: 17px; padding-left: 8px; text-decoration: none; } .urbangreymenu ul li a:visited{ color: black; } .urbangreymenu ul li a:hover{ color: white; background: black; } |
Langkah selanjutkan Anda buat index.php, yang merupakan file utama untuk mengakses web login, seperti biasa copy-paste-kan kode di bawah ini dengan Notepad, kemudian beri nama index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | <?php include("connect.php"); //koneksikan database ke PHP session_start(); //Memulai session untuk mengecek apakah kita sudah login? //cek session apakah kita login di web ini atau tidak? if (!empty($_SESSION["login_username"])){$sudah_login=true;}else{$sudah_login=false;} if (!empty($_SESSION["login_password"])){$sudah_login=true;}else{$sudah_login=false;} //jika sudah login, maka lewatkan form login langsung kita menuju halaman utama if ($sudah_login){header("location: home.php");} //Cek Request, apakah data USERNAME dan PASSWORD kita sesuai if ($_SERVER["REQUEST_METHOD"]=="POST"){ /*Cek Validasi USERNAME dan PASSWORD * --> Cek dulu apakah USERNAME dan PASSWORD terisi/ter-input? */ If (!empty($_POST["username"])){ $username=$_POST["username"]; $terisi=true;} else {$terisi=false;} If (!empty($_POST["password"])){ $password=md5($_POST["password"]); //convert ke MD5 $terisi=true;} else {$terisi=false;} if ($terisi) //Jika Terisi maka cek Validasinya { $query="SELECT * FROM admin WHERE nama='$username' AND password='$password'"; $hasil=mysql_query($query); $validasi=false; //validasi pertama di false kan dalu $i=0; while ($row=mysql_fetch_row($hasil)) { $i++; if (isset($row[$i])){$validasi=true;}else{$validasi=false;} } if ($validasi) //Jika sudah benar, Anda dipersilahkan masuk ke halaman utama { //simpan USERNAME dan PASSWORD ke SESSION $_SESSION["login_username"]=$username; $_SESSION["login_password"]=$password; header("location: home.php"); } else {$warning="Mohon maaf username/password Anda salah!";} } else {$warning="Tolong username/password Anda harus diisi!";} } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Login</title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <form method="POST" action="index.php"> <div id="wrap_login"> <div id="login_form"> <div id="input"> <h1>Login Administrator</h1> <?php if (isset($warning)): ?> <div id="warning"> <?php echo $warning;?> </div> <?php endif;?> <p><b>Username</b></p> <p><input name="username" type="text" class="textfield" id="login" /></p> <p><b>Password</b></p> <p><input name="password" type="password" class="textfield2" id="password" autocomplete="off" /></p> <div class="buttons"> <button type="submit" class="positive" name="login"> <img src="img/login.png" alt=""/> Login </button> </div> </div> <div id="gembok"> <img src="img/gembok.png" /> </div> </div> </div> </form> </body> </html> |
Langkah terakhir Anda buat home.php, yang berfungsi untuk mengecek apakah kita sebagai administrator atau bukan, jika ya berarti Anda dapat masuk ke dalam ruang administrasi ini (home.php).
Anda Copy-Paste-kan kode di bawah ini di Notepad, kemudian beri nama home.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <?php include("connect.php"); //koneksikan database ke PHP session_start(); //Memulai session untuk mengecek apakah kita sudah login? //cek session apakah kita login di web ini atau tidak? if (!empty($_SESSION["login_username"])){$sudah_login=true;}else{$sudah_login=false;} if (!empty($_SESSION["login_password"])){$sudah_login=true;}else{$sudah_login=false;} //jika belum login, maka Anda tidak boleh masuk ke sini! if (!$sudah_login){header("location: index.php");} $halaman_utama="Selamat Datang Administrator"; if (!isset($halaman)){$halaman=$halaman_utama;} if (isset($_GET["aksi"])){ $aksi=$_GET["aksi"]; switch ($aksi){ case "profile": $halaman="Ini Halaman Profile"; break; case "module": $halaman="Ini Halaman Module"; break; case "logout": session_destroy(); header("location: index.php"); break; default: $halaman=$halaman_utama; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Halaman Utama</title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <link rel="stylesheet" type="text/css" href="style.css" /> <script> function menuju(ke){ window.location=ke; } function pesan(text){ alert(text); } function konfirmasi(isi,yes){ var pesan=confirm(isi); if (pesan){menuju(yes);} } </script> </head> <body> <div id="wrap_home"> <div id="home_header"><h1>Ruang Administrasi</h1></div> <div id="home_sideleft"> <div class="urbangreymenu"> <h3 class="headerbar">Menu Navigasi</h3> <ul> <li><a href="home.php">Home</a></li> <li><a href="?aksi=profile">Profile</a></li> <li><a href="?aksi=module">Module</a></li> <li><a class="regular" onclick="konfirmasi('Apakah Anda ingin logout?','?aksi=logout')">Logout</a></li> </ul> </div> </div> <div id="home_content"> <h1><?php echo $halaman;?></h1> </div> </div> <div id="home_footer"><p>Copyright © Ali Software</p></div> </body> </html> |
Jika Anda merasa kesulitan atau malas untuk membaca kode di atas, Anda bisa mengunduh kode sumber (source code) ini :
webnya bagus banget gan mantappp
Thanks
kerenn banget deh makasih ya
Iya sama-sama gan 😀
kalau teknik enskirpsi passwordnya pake hash(“sha512”,$pass); bagus ngk gan..
contoh skripnya begini
//pertama buat key pass
$kunci_pass = “0123456789ABCDEFGHqwertyuiopsdfghjklzxcvbnm!@#$%^&*())_+=][\|”;
$salt_pass_baru =”12345RTYghjvdFGHrt”;
$md5 =md5($kode_pass.$salt_pass);
//akhir dari key
if(isset($_POST[‘pass’]){
$user = mysql_real_escape_string($_POST[‘user’]);
$pass = mysql_real_escape_string($_POST[‘pass’]);
$key_pass : “$new_pass = hash(“sha512″,$md5.$salt_pass_baru.$pass;”;
if(empty($pass){
echo “password kosong”;
} else {
//$query = mysql_query(“select user, pass from dbuser where username=’$user’ and pass=’$key_pass'”)or die(mysql_error());
}
//thanks