ඔයාල කැමතිද මේ වගේ ලොගින් සිස්ටම් එකක් තනියම හදල බලන්න.
1. මේකට database හා php දැනුම ඕනේ ටිකක්. ඉස්සල්ලම php, server හා database පරිගණකයේ ස්ථාපනය කරගත යුතුය.
sever එක දාගෙන නැති අය මෙතනින් ගිහින් දාගන්න හැටි බලන්න.
එක පාර wamp වගේ server එකක් දාගන්නත් පුළුවන්.
ඉස්සල්ලම browser එකේ http://localhost/phpmyadmin කියල ගහන්න.
database>create new database ගිහිල්ල test කියල database එකක් හදන්න. collation කියන එකට utf8 _genaral _ci silect කරන්න.
ඒකේ members කියල table ක් හදන්න. ඒකේ id, username, password, email කියල fields 4ක් හදන්න. id කියන ඒකේ type 1 INT, length එක 5, collation utf8 _genaral _ci දෙන්න. අනිත් fields වල type 1 VARCHAR, length එක 50, collation utf8 _genaral _ci දෙන්න.
2. දැන් main_login.php file 1 හදමු.
<html>
<head>
<title>Login</title>
</head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>User Login here:</strong></td>
</tr>
<tr>
<td width="78">User name</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<hr/>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<form name="form2" method="post">
<tr><td colspan="3"><strong>Sigin up here</strong></td></tr>
<tr><td><input type="submit" name="signup" value="Join"></td></tr>
</form>
</table>
</body>
<?php
if(isset($_POST['signup'])){
header("location:register.php");
}
?>
</html>
3. දැන් register.php file 1 හදමු.
මේකේ //mail($email,$subject,$message); කියන line එක // වලින් coment කරලා තියෙන්නේ localhost ඒකේ mail function එක වැඩකරන්නේ නැති නිසා. sever එකට හොස්ට් කරනකොට // අයින් කරලා දාන්න.
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$database = 'test';
$con = mysql_connect($host, $user, $pass);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $con);
$query = "SELECT * FROM `".$database."`.`members`";
$results = mysql_query($query);
$persons = array();
if($results && mysql_num_rows($results) > 0) {
while($row = mysql_fetch_assoc($results)) {
$persons[] = $row;
}
}
$querySelectGraphics= "SELECT username FROM `".$database."`.`members`";
$resultsSelectGraphics = mysql_query($querySelectGraphics);
$Graphics = array();
if($resultsSelectGraphics && mysql_num_rows($resultsSelectGraphics) > 0) {
while($rowGraphic = mysql_fetch_assoc($resultsSelectGraphics)) {
$Graphics[] = $rowGraphic;
}
}
?>
<html>
<head>
<title>Register</title>
</head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Sign up here</strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input id="usern" type="text" name="usern">
</td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input id="passw" type="password" name="passw">
</td>
<tr>
<td>Repeat Password</td>
<td>:</td>
<td><input id="rpassw" type="password" name="rpassw">
</td>
<tr>
<td>Email</td>
<td>:</td>
<td><input id="mail" type="email" name="mail">
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" VALUE="submit" name="Button" >
</td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
if (!isset($_POST['passw']))
{
$_POST['passw'] = "undefine";
}
if (!isset($_POST['usern']))
{
$_POST['usern'] = "undefine";
}
if (!isset($_POST['mail']))
{
$_POST['mail'] = "undefine";
}
$usname=$_POST['usern'];
$pword=$_POST['passw'];
$email = $_POST["mail"];
$subject = "Thank you for registering!";
$message = 'You have just registered the following details :<hr/>
<table bgcolor="#E0E0E0">
<strong>Username: </strong>'. $usname .'<br/>
<strong>Password: </strong>'. $pword .'</table>
<hr/>
<h3><a href="#">Click here to login</a></h3>';
$dbunames = mysql_query("SELECT * FROM `".$database."`.`members` WHERE username='$usname'");
if(isset($_POST['Button'])){
if (empty($_POST['usern']) || empty($_POST['passw']) || empty($_POST['rpassw']) || empty($_POST['mail']))
{
echo "Please fill every fields!";
}
elseif (($_POST['passw'] != $_POST['rpassw']))
{
echo "Password and Repeat passwords did not match!";
}
elseif (mysql_num_rows($dbunames) > 0)
{
echo "The username is already registered. ! Try another name. eg.: kasunaaa instead of kasun ";
}
else
{
mysql_query("INSERT INTO `test`.`members` (`username`, `password`, `email`) VALUES ('".$_POST['usern']."','".$_POST['passw']."', '".$_POST['mail']."');");
//mail($email,$subject,$message);
header("location:main_login.php");
}
}
?>
</body>
</html>
4. දැන් checklogin.php file 1 හදමු.
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_start();
$_SESSION['login'] = "1";
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
5. දැන් login_success.php file 1 හදමු.
<?php
// Check if session is not registered, redirect back to login page.
// Put this code in first line of web page.
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header("location:main_login.php");
}
?>
<?php
// if log out buttion is pressed sent to login page and no more can visit members only pages
// Put this code and logout form in any where of web page.
if(isset($_POST['logout'])){
session_destroy();
header("location:main_login.php");
}
?>
<html>
<body>
Login Successful
<hr/>
your content here
<hr/>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<form name="logout form" method="post">
<td colspan="3"><strong>Log out</strong></td>
<td><input type="submit" name="logout" value="log out"></td>
</form>
</table>
</body>
</body>
</html>
මේ file ඒකේ your content here කියන තැන ඔයාට ඕන content දාන්න log උන අයට විතරක් පෙන්නන ඕනේ.
හැම content page එකක්ම මේ විදියට හදන්න.
මේ files ඔක්කොම sever ඒකේ www folder ඒකේ sub folder එකක save කරන්න.
ඊටපස්සේ main_login.php එක run කරන්න.
කොහොමද? අවුලක් නම් අහන්න.