SHOW:
|
|
- or go back to the newest paste.
| 1 | <?php | |
| 2 | session_start(); | |
| 3 | #**************** koneksi ke mysql *****************# | |
| 4 | $host = "localhost"; | |
| 5 | $user = "root"; | |
| 6 | $pass = ""; | |
| 7 | $dbname = "belajar"; | |
| 8 | - | $conn = mysql_connect($host,$user,$pass); |
| 8 | + | $conn = mysql_connect($host,$user,$pass)or die(mysql_error()); |
| 9 | - | if($conn) {
|
| 9 | + | /*percuma kl gk bisa connect.. */ |
| 10 | - | //select database |
| 10 | + | $sele = mysql_select_db($belajar); |
| 11 | - | $sele = mysql_select_db($belajar); |
| 11 | + | |
| 12 | - | if(!$sele) {
|
| 12 | + | |
| 13 | - | echo mysql_error(); |
| 13 | + | |
| 14 | #jika ditekan tombol login | |
| 15 | if(isset($_POST['login'])) | |
| 16 | {
| |
| 17 | $username = $_POST['username']; | |
| 18 | $password = $_POST['password']; | |
| 19 | - | if(isset($_POST['login'])) {
|
| 19 | + | $sql="SELECT * FROM user WHERE username='$username' && |
| 20 | password='$password'"; | |
| 21 | - | $username = $_POST['username']; |
| 21 | + | $q = mysql_query($sql) |
| 22 | - | $password = $_POST['password']; |
| 22 | + | or die(mysql_error()."<br>sql=$sql"); |
| 23 | - | $sql = mysql_query("SELECT * FROM user WHERE username='$username' &&
|
| 23 | + | $num = mysql_num_rows($sql); |
| 24 | - | password='$password'"); |
| 24 | + | |
| 25 | - | $num = mysql_num_rows($sql); |
| 25 | + | if($num==1) {
|
| 26 | - | if($num==1) {
|
| 26 | + | // login benar // |
| 27 | - | // login benar // |
| 27 | + | $_SESSION['user'] = $username; |
| 28 | - | $_SESSION['user'] = $username; |
| 28 | + | $_SESSION['passwd'] = $password; |
| 29 | - | $_SESSION['passwd'] = $password; |
| 29 | + | echo "<script language=\"JavaScript\">alert('Anda berhasil login');
|
| 30 | - | echo "<script language=\"JavaScript\">alert('Anda berhasil login');
|
| 30 | + | document.location='berhasil.php'</script>"; |
| 31 | - | document.location='berhasil.php'</script>"; |
| 31 | + | } |
| 32 | - | } |
| 32 | + | else |
| 33 | - | else |
| 33 | + | {
|
| 34 | // jika login salah // | |
| 35 | - | // jika login salah // |
| 35 | + | echo "<script language=\"JavaScript\">alert('Username atau password Anda
|
| 36 | - | echo "<script language=\"JavaScript\">alert('Username atau password Anda
|
| 36 | + | salah'); document.location='login.php'</script>"; |
| 37 | - | salah'); document.location='login.php'</script>"; |
| 37 | + | |
| 38 | } | |
| 39 | ||
| 40 | } | |
| 41 | ?> |