Login PHP
Masih Ingat dengan cara masuk Facebook. Pasti dia minta cara dengan masukin nama user dan password. Kepikiran gak gimana cara buat program itu. Nah ini salah satu aplikasinya, pake php lo lumayan sulit sih tapi mungkin mudah bagi kamu. Jadi coba pelajari aja.<html>
<head>
<title>Login here</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
table {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
}
input {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
height: 20px;
}
-->
</style>
</head>
<body>
<div align="center">
<form action="periksa.php" method="post" name="login">
<table width="286" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr bgcolor="blue">
<td height="19" colspan="2" align="center" valign="middle">
<font color="#FFFFFF">LOGIN </font></td>
</tr>
<tr>
<td width="106" height="27"> </td>
<td width="180"> </td>
</tr>
<tr>
<td height="18" align="right" valign="middle">Username : </td>
<td valign="middle">
<input name="username" type="text" id="username" size="20"></td>
</tr>
<tr>
<td height="18" align="right" valign="middle">Password : </td>
<td valign="middle">
<input name="password" type="password" id="password" size="20"></td>
</tr>
<tr>
<td height="19"> </td>
<td></td>
</tr>
<tr>
<td height="18" valign="top"><!--DWLayoutEmptyCell--> </td>
<td valign="middle">
<input name="login" type="submit" id="login" value=" Login "></td>
</tr>
<tr>
<td height="28"> </td>
<td></td>
</tr>
<tr bgcolor="blue">
<td height="18" colspan="2" valign="top"><!--DWLayoutEmptyCell--> </td>
</tr>
</table>
</form>
</div>
</body>
</html>
Pemeriksaan Login
<?php
session_start();
#**************** koneksi ke mysql *****************#
$host = "database";
$user = "bobby";
$pass = "310787";
$dbname = "db_bobby";
$conn = mysql_connect($host,$user,$pass);
if($conn) {
//select database
$sele = mysql_select_db($dbname);
if(!$sele) {
echo mysql_error();
}
}
#jika ditekan tombol login
if(isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = mysql_query("SELECT * FROM user WHERE username='$username' &&
password='$password'");
$num = mysql_num_rows($sql);
if($num==1) {
// login benar //
$_SESSION['user'] = $username;
$_SESSION['passwd'] = $password;
?><script language="JavaScript">alert('Anda berhasil login');
document.location='berhasil.php'</script><?
} else {
// jika login salah //
?><script language="JavaScript">alert('Username atau password Anda
salah'); document.location='login.php'</script><?
}
}
?>
Login berhasil dan halaman logout
<?
session_start();
//periksa apakah user telah login atau memiliki session
if(!isset($_SESSION[‘user’]) || !isset($_SESSION[‘passwd’])) {
?><script language=’javascript’>alert(‘Anda belum login. Please login dulu’);
document.location=’login.php’</script><?
} else {
?>
<html>
<head>
<title>Berhasil</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="746" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="746" height="23" align="center" valign="top"><font face="Verdana, Arial,
Helvetica, sans-serif">Selamat
Datang <? echo $_SESSION['user'] ?> <a
href="logout.php"><strong>LOGOUT</strong></a></font></td>
</tr>
<tr>
<td height="88"> </td>
</tr>
<tr align="center" valign="middle">
<td height="33"><font color="#FF0000" size="5" face="Arial, Helvetica, sans-serif">ANDA
BERHASIL LOGIN !!</font></td>
</tr>
<tr>
<td height="62"> </td>
</tr>
</table>
</body>
</html>
Sumber : Achmad Solichin
session_start();
//periksa apakah user telah login atau memiliki session
if(!isset($_SESSION[‘user’]) || !isset($_SESSION[‘passwd’])) {
?><script language=’javascript’>alert(‘Anda belum login. Please login dulu’);
document.location=’login.php’</script><?
} else {
?>
<html>
<head>
<title>Berhasil</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="746" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="746" height="23" align="center" valign="top"><font face="Verdana, Arial,
Helvetica, sans-serif">Selamat
Datang <? echo $_SESSION['user'] ?> <a
href="logout.php"><strong>LOGOUT</strong></a></font></td>
</tr>
<tr>
<td height="88"> </td>
</tr>
<tr align="center" valign="middle">
<td height="33"><font color="#FF0000" size="5" face="Arial, Helvetica, sans-serif">ANDA
BERHASIL LOGIN !!</font></td>
</tr>
<tr>
<td height="62"> </td>
</tr>
</table>
</body>
</html>
Sumber : Achmad Solichin
0 komentar:
Posting Komentar