Alhamduillah.. Masih bisa diberi kesempatan untuk berbagi pengetahuan dengan kawan2.. ;-).
Kebetulan aku nyoba2 buat script PHP untuk keperluan fasilitas Register, login, dan logout seperti pada website jaringan sosial/ social network.
Scipt ini terdiri dari :
• Config.php
• Index.php
• List_user.php
• Login.php
• Logout.php
• Register.php
Script ini diuji dengan PHP 6. Versi PHP 5 Insya Allah Lain kali dibahas.
Dengan muatan asli 6,56 KB dan muatan di harddisk 24,0 KB. Ya, hanya sebuah folder yang bermuatan kecil.
Oke, pertama-tama buat perintah SQL nya di database :
1 2 3 4
| CREATE DATABASE reglog; USE reglog; CREATE TABLE members (Id SMALLINT NOT NULL AUTO INCREMENT, nama VARCHAR(50), email VARCHAR(50), password VARCHAR(50)); |
Config.php
1 2 3 4 5 6 7 8 9 10 11 12
| <? $host = "localhost"; $user_host = "root";
$pass_host = "rosaarini"; $db = "reglog"; $table = "members";
$konek = mysql_pconnect($host, $user_host, $pass_host) or die
("Tidak dapat menghubungkan ke database"); $pilih_db = mysql_select_db ($db, $konek) or die ("Tidak dapat memilih db");
$cookie_nama = $_COOKIE['username']; ?> |
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
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso- 8859-1" /> <style type="text/css"> <!-- .style1 { color: #006600; font-weight: bold; } --> </style> </head>
<body> <p align="center"> <? include "config.php"; ?> <p align="center">Selamat Datang <?
$koki = $_COOKIE['username']; echo "<b>$koki</b> ";
if ($koki == TRUE) { echo " | <a href="logout.php">Logout</a> n
"; echo " <center> <table width="628" border="0"> <tr>
<td> Terima Kasih $koki, Anda telah berkenan bergabung dengan kami. Nikmati Fasilitas2 member di antaranya : 1. Download gratis E-Book Novel berformat jar. 2. Kemudahan berinteraksi dan juga mendapatkan update pengumuman terbaru dari kami. </td> </tr> </table>
</center> n " ; echo " <center> <a href="list_user.php">Lihat User Lain</a>
</center> "; } else { echo " <center><a href="login.php">Login</a> | <a href="register.php">Register</a></center>
n "; } ?> <p align="center"><span class="style1"><? echo $_GET['pesan']; ?
></span> <center> </center> </body> </html> |
List_user.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
| <? include "config.php"; $koki = $_COOKIE['username'];
if (!$koki or $koki == "") { echo "<center> Anda tidak berhak mengakses halaman ini. Silahkan <a href="login.php">Login</a> | <a href="register">register</a> </center>";
} else { $query = mysql_query("SELECT * FROM $table ");
while ($baris = mysql_fetch_array($query)) { $nama = $baris['nama'];
$email = $baris['email']; echo " <table width="350" border="0">
<tr> <td width="21"></td> <td width="75">Nama</td> <td width="132">: $nama</td>
</tr> <tr> <td> </td> <td>Email</td> <td>: <a href="mailto:$email">$email</a></td>
</tr> <tr> <td> </td> <td> </td> <td> </td> </tr>
</table> "; } mysql_close($konek); } ?> |
Login.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
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso- 8859-1" /> <style type="text/css">
<!-- .style1 {font-family: Calibri} --> </style> </head> <? include "config.php"; ?> <body>
<form action="<? $PHP_SELF; ?>" method="post" class="style1"> <p align="center"><strong>Halaman Login</strong> | <a href="index.php">Halaman Utama </a>
<div align="center"> <table width="233" border="0"> <tr> <td width="71">Username</td> <td width="166">: <input type="text" name="username" /></td> </tr>
<tr> <td>Password</td> <td>: <input type="password" name="password" /></td> </tr> </table> <input type="submit" value="Login" name="submit" />
<input type="reset" value="Reset" name="reset" /> </div> </form> <center><? $username = $_POST['username'];
$password = $_POST['password']; $submit = $_POST['submit'];
$perintah_pilih = "SELECT * FROM $table where nama='$username'"; $query = mysql_query($perintah_pilih);
$hasil = mysql_fetch_array($query); $baris = mysql_num_rows($query);
if (isset($submit)) { if ($username == "")
{ $a = "Anda Belum Memasukkan Username "; echo "<center>$a</center>"; }
if ($password == "") { $b = "Anda Belum Memasukkan Password "; echo
"<center>$b</center>"; exit;} } if ($baris != 0)
{ if ($password != $hasil['password']) {
echo "Password Salah ! n"; } else { setcookie("username", $username);
header("Location: index.php"); } } ?> <? echo $_GET['pesan'];
?> </center> </body> </html> |
Logout.php
1 2 3 4 5
| <?
setcookie("username", ""); $pesan = "Anda Berhasil Logout"; header("Location: index.php?pesan=$pesan");
?> |
Register.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
| <? include "config.php"; ?>
<style type="text/css"> <!-- .style1 {font-family: Calibri} .style2 { color: #FF0000; font-weight: bold; font-size: medium; font-family: Calibri; } --> </style> <form action="" method="post" class="style1"> <p align="center"><strong>Registrasi Online</strong> | <a href="index.php">Halaman Utama </a>
<div align="center"> <table width="260" border="0"> <tr> <td width="86">Username</td> <td width="164">: <input type="text" name="user"></td> </tr>
<tr> <td>Email</td> <td>: <input type="text" name="email"></td> </tr> <tr> <td>Password</td>
<td>: <input type="password" name="pass"></td> </tr> <tr> <td>Re-Password</td> <td>: <input type="password" name="repass"></td>
</tr> </table> <input type="submit" value="register" name="submit"> <input type="reset" value="reset" name="reset"> </div> <p align="center"> </form> <div align="center"> <?
$user = $_POST['user']; $email = $_POST['email'];
$pass = $_POST['pass']; $repass = $_POST['repass'];
if ($pass != $repass) echo "Password harus sama n"; $query = mysql_query("SELECT * FROM members");
$hasil = mysql_fetch_array($query); if (isset($user))
{ if (($hasil['nama'] == $user) and ($hasil['nama'] == $user) and
($hasil['nama'] == $user)) { echo ( "<span class="style2">Maaf, Username telah terpakai.</span>");
exit ; } else if (!empty($user) and !empty($pass) and !empty($email) )
{ mysql_query("INSERT INTO $table (nama, email, password) VALUES ('$user', '$email', '$pass')");
$pesan = "Registrasi Berhasil, Silahkan Login."; header("Location: login.php?pesan=$pesan"); }
else if (empty($user) and empty($pass) and empty($email))
{ echo "Field Tidak Boleh Ada Yang Kosong.n"; } else if (!ereg("^.+@.+..+$", $email))
{ echo "Email Salah!. Contoh : blogimam@gmail.com"; } } ?> </div> |
Silahkan coba dan jalankan di PHP 6.
Silahkan download source codenya.Sumber : http://www.archmaster.co.cc