Membuat Form Login Member Aplikasi Multi-User PHP
Ok Langsung Saja kita praktekan pertama-tama kita buat terlebih dahulu file untuk penghubung ke databasenya :
config.php
01 | <?php |
02 | //membuat koneksi ke database |
03 | mysql_connect( "localhost" , "root" , "" ); |
04 |
05 | //localhost = nama server-nya |
06 | //root = user mysql |
07 | //admin = password user root |
08 |
09 | //memilih database |
10 | mysql_select_db( "login" ); |
11 | ?> |
nah kalo sudah kita buat penghubungnya sekarang kita buat form loginya.
login.php
01 | <?php |
02 | //memulai session |
03 | session_start(); |
04 | //cek adanya session, jika session sudah ada maka diarahkan ke index.php |
05 | if (ISSET( $_SESSION [ 'username' ])){ |
06 | header( "location: index.php" ); |
07 | } |
08 | ?> |
09 | <style type= "text/css" > |
10 | <!-- |
11 | .style1 { |
12 | font-family: Arial, Helvetica, sans-serif; |
13 | font-size: 24px; |
14 | font-weight: bold; |
15 | color: #FFFFFF; |
16 | } |
17 | .style5 {color: #FFFFFF} |
18 | .style9 {color: #000000; font-weight: bold; } |
19 | .style10 {color: #000000} |
20 | --> |
21 | </style> |
22 |
23 | <p> </p> |
24 | <form id= "form1" name= "form1" method= "post" action= "cek.php" > |
25 | <p> </p> |
26 | <table width= "329" border= "0" align= "center" cellpadding= "0" cellspacing= "2" > |
27 |
28 | <tr> |
29 | <td height= "44" colspan= "4" bgcolor= "#999999" ><div align= "center" >CYBER LOG-IN </div></td> |
30 | </tr> |
31 | <tr> |
32 | <td width= "10" > </td> |
33 | <td width= "133" ><span> User Name </span></td> |
34 | <td width= "6" ><span>:<span>:</span></span></td> |
35 | <td width= "306" ><input name= "username" type= "text" /></td> |
36 | </tr> |
37 | <tr> |
38 | <td> </td> |
39 | <td><span>Password</span></td> |
40 | <td><span>:<span>:</span></span></td> |
41 | <td><input name= "password" type= "password" /></td> |
42 | </tr> |
43 | <tr> |
44 | <td> </td> |
45 | <td> </td> |
46 | <td> </td> |
47 | <td><input name= "submit" value= "Login" type= "submit" /></td> |
48 | </tr> |
49 | <tr> |
50 | <td height= "39" colspan= "4" bgcolor= "#999999" ><span>Bukan Member? Silahkan Daftar </span><a href= "signup.php" >Daftar</a> </td> |
51 | </tr> |
52 | </table> |
53 | </form> |
54 | <p> </p> |
cek.php
01 | <?php |
02 | //memulai session |
03 | session_start(); |
04 |
05 | //koneksi ke database |
06 | include "config.php" ; |
07 |
08 | //mengambil data dari form |
09 | $username = $_POST [ 'username' ]; |
10 | $password = $_POST [ 'password' ]; |
11 |
12 | //enkripsi password dengan md5 |
13 | $password_md5 = md5( $password ); |
14 |
15 | //cek username dan password dari database |
16 | $perintah = "select * from user where username='$username'&&password='$password_md5'" ; |
17 | $perintah_di_query = mysql_query( $perintah ); |
18 | $ketersediaan = mysql_num_rows( $perintah_di_query ); |
19 |
20 | //Cek adanya username dan password di database dilanjutkan dengan membuat session |
21 | if ( $ketersediaan >= 1 ){ |
22 | $_SESSION [ 'username' ] = $username ; |
23 | header( "location: index.php" ); |
24 | } else { |
25 | header( "location: login.php" ); |
26 | } |
27 | ?> |
nah sekarang kita buat untuk halaman utamanya !
index.php
01 | <?php |
02 | //memulai session |
03 | session_start(); |
04 |
05 | //cek adanya session |
06 | if (ISSET( $_SESSION [ 'username' ])){ |
07 | echo "Anda Login Sebagai " ; |
08 | echo $_SESSION [ 'username' ]; |
09 | echo "<br><a href='logout.php'>logout</a>" ; |
10 |
11 | //jika tidak ada session |
12 | } else { |
13 | header( "location: login.php" ); |
14 | } |
15 | ?> |
16 | <html><body> |
17 | <br><br><h2> |
18 | </h2> |
19 | <h2>Selamat Datang di HalamanUtama !! <br> |
20 | <br> |
21 | </h2> |
22 | </body></html> |
setelah itu kita buat pendaftaran usernya.
signup.php
01 | <style type= "text/css" > |
02 | <!-- |
03 | .style1 { font-family: Arial, Helvetica, sans-serif; |
04 | font-size: 24px; |
05 | font-weight: bold; |
06 | color: #FFFFFF; |
07 | } |
08 | .style10 {color: #000000} |
09 | .style5 {color: #FFFFFF} |
10 | .style9 {color: #000000; font-weight: bold; } |
11 | --> |
12 | </style> |
13 | <form method= "post" action= "signup_process.php" > |
14 | <pre><h2>Form Pendaftaran</h2> |
15 | </pre> |
16 | <table width= "355" border= "0" align= "center" cellpadding= "0" cellspacing= "2" > |
17 | <tr> |
18 | <td height= "44" colspan= "4" bgcolor= "#999999" ><div align= "center" >REGISTRATION </div></td> |
19 | </tr> |
20 | <tr> |
21 | <td width= "7" > </td> |
22 | <td width= "161" ><span> Masukan User Name </span></td> |
23 | <td width= "10" ><span>:<span>:</span></span></td> |
24 | <td width= "167" ><input name= "username" type= "text" /></td> |
25 | </tr> |
26 | <tr> |
27 | <td> </td> |
28 | <td><span>Masukan Password</span></td> |
29 | <td><span>:<span>:</span></span></td> |
30 | <td><input name= "password" type= "password" /></td> |
31 | </tr> |
32 | <tr> |
33 | <td> </td> |
34 | <td> </td> |
35 | <td> </td> |
36 | <td><input name= "submit" value= "Daftar" type= "submit" /></td> |
37 | </tr> |
38 | <tr> |
39 | <td height= "39" colspan= "4" bgcolor= "#999999" > </td> |
40 | </tr> |
41 | </table> |
42 | <pre> </pre> |
43 | </form> |
signup_process
01 | <?php |
02 | include "config.php" ; |
03 |
04 | ///mengambil data dari form |
05 | $username = $_POST [ 'username' ]; |
06 | $password = $_POST [ 'password' ]; |
07 | $password_md5 = md5( $password ); |
08 |
09 | //cek pengisian data |
10 | if ( $username == '' || $password == '' ){ |
11 | echo "Data tidak lengkap<br--><a href=signup.php>Back</a>" ; |
12 |
13 | //jika data sudah lengkap, dilanjutkan input data ke database |
14 | } else { |
15 | $perintah = "insert into user values ('$username', '$password_md5')" ; |
16 | $perintah_di_query = mysql_query( $perintah ); |
17 |
18 | //Jika input data berhasil, dilanjutkan dengan pemberitahuan pendaftaran berhasil |
19 | if ( $perintah_di_query ) { |
20 | echo "Daftar berhasil, silakan <a href='index.php'>login</a>" ; |
21 |
22 | //jika input data gagal, dilanjutkan dengan pemberitahuan pendaftaran gagal |
23 | } else { |
24 | echo "Daftar gagal atau username telah terdaftar silakan <a href='signup.php'>Ulangi</a> atau <a href='login.php'>Login</a>" ; |
25 | } |
26 | } |
27 | ?> |
Dalam pembuatan Sistem Login ini harus dilakukan dengan sangat teliti.
Selamat belajar Teman-teman sekalian.
No comments:
Post a Comment