Verification Code with PHP & MySQL

Pertama Buatlah sebuah Database,
sebagai contoh dengan nama dblatihan
kemudian buat table dengan nama tbl_auth_user
dengan field user_id(varchar), user_password(varchar).
setelah database terbentuk buat file conect.php dengan script sbb :
$koneksi=mysql_connect("localhost","root","");
mysql_select_db("dblatihan");


kemudian buat file untuk create number nya. dengan script berikut simpan dengan nama file createnumber.php
<?php
session_start();

// generate 4 digit random number
$rand = rand(1000, 9999);

// create the hash for the random number and put it in the session
$_SESSION['image_random_value'] = md5($rand);

// create the image
$image = imagecreate(60, 30);

// use white as the background image
$bgColor = imagecolorallocate ($image, 255, 255, 255);

// the text color is black
$textColor = imagecolorallocate ($image, 0, 0, 0);

// write the random number
imagestring ($image, 5, 5, 8, $rand, $textColor);

// send several headers to make sure the image is not cached
// taken directly from the PHP Manual

// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0
header("Pragma: no-cache");

// send the content type header so the image is displayed properly
header('Content-type: image/jpeg');

// send the image to the browser
imagejpeg($image);

// destroy the image to free up the memory
imagedestroy($image);
?>

 

Kemudian buat file loginnya, dengan script berikut dan simpan dengan nama file flogin.php
 <?php
// we must never forget to start the session
session_start();

$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
// first check if the number submitted is correct
$number = $_POST['txtNumber'];

if (md5($number) == $_SESSION['image_random_value']) {
include "conect.php";

$userId = $_POST['txtUserId'];
$password = $_POST['txtPassword'];

// check if the user id and password combination exist
$sql = "SELECT user_id
FROM tbl_auth_user
WHERE user_id = '$userId'
AND user_password = '$password'";

$result = mysql_query($sql) or
die('Query failed. ' . mysql_error());

if (mysql_num_rows($result) == 1) {
// the user id and password match,
// set the session
$_SESSION['image_is_logged_in'] = true;

// remove the random value from session
$_SESSION['image_random_value'] = '';

// after login we move to the main page
header('Location: main.php');
exit;
} else {
$errorMessage = 'Sorry, wrong user id / password';
}

} else {
$errorMessage = 'Sorry, wrong number. Please try again';
}
}
?>
<html>
<head>
<title>Basic Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
if ($errorMessage != '') {
?>
<p>&nbsp;</p><p align="center"><strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p>
<?php
}
?>
<form action="" method="post" name="frmLogin" id="frmLogin">
<table width="500" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td width="150">User Id</td>
<td><input name="txtUserId" type="text" id="txtUserId"></td>
</tr>
<tr>
<td width="150">Password</td>
<td><input name="txtPassword" type="password" id="txtPassword"></td>
</tr>
<tr>
<td width="150">Enter Number Verification Code</td>
<td><p>Verification Code <img src="createnumber.php"> </p>
<p>
<input name="txtNumber" type="text" id="txtNumber" value="">
&nbsp;</p>
<p>&nbsp;</p></td>
</tr>

<tr>
<td width="150">&nbsp;</td>
<td><input name="btnLogin" type="submit" id="btnLogin" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>


kemudian satu lagi, buat file sebagai header ketika login & verification code sukses dilakukan, simpan dengan nama main.php
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body text="#FF0000">
<div align="center">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Connection
Sucess... </strong></font></p>
<p>&nbsp;</p>
<p><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Selamet
ya Bro...</font></strong></p>
<p>&nbsp;</p>
<p><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><a href="flogin.php">Back</a></font></strong></p>
</div>
</body>
</html>


kemudian jalankan di browser, jangan lupa aktifkan webserver anda.
Selamat Mencoba.. :)
0 Responses