Difference between revisions of "PHP Login"
From KOP KB
(→Checking the Login) |
(→Setting up the First Page) |
||
Line 20: | Line 20: | ||
== Setting up the First Page == | == Setting up the First Page == | ||
I name the file myself to mlogin.php but its mostly html thats the function. I made it php so if I needed to do anything with it I can | I name the file myself to mlogin.php but its mostly html thats the function. I made it php so if I needed to do anything with it I can | ||
+ | <syntaxhighlight lang="php> | ||
+ | <? | ||
+ | session_start(); | ||
+ | $rand =""; | ||
+ | //gen the random number | ||
+ | for ($x=0; $x<32; $x++) { | ||
+ | $rand = $rand . mt_rand(0,9); | ||
+ | } | ||
+ | //kill the comparison cookie | ||
+ | setcookie("sesscompa", "$rand", time()-3600); | ||
+ | setcookie("sesscompb","$rand", time()-3600); | ||
+ | |||
+ | //starting and overwriting the first cookie | ||
+ | setcookie("sesscooka", "$rand", time()+3600); | ||
+ | setcookie("sesscookb","$rand", time()+3600); | ||
+ | ?> | ||
+ | </syntaxhighlight> | ||
<syntaxhighlight lang="html4strict"> | <syntaxhighlight lang="html4strict"> | ||
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> | <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> | ||
Line 50: | Line 67: | ||
</table> | </table> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
== Checking the Login == | == Checking the Login == | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> |
Revision as of 00:45, 8 August 2014
Contents
PHP MyAdmin Create database
CREATE TABLE `members` (
`id` int(4) NOT NULL auto_increment,
`username` varchar(65) NOT NULL default '',
`password` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
--
-- Dumping data for table `members`
--
-- use the md5 bracket to hash the passwords
-- create as many insert into commands as you need for how much
-- there could be other values as well depending on what is needed for login
INSERT INTO `members` VALUES (1, 'john', md5('1234'));
Setting up the First Page
I name the file myself to mlogin.php but its mostly html thats the function. I made it php so if I needed to do anything with it I can
<?
session_start();
$rand ="";
//gen the random number
for ($x=0; $x<32; $x++) {
$rand = $rand . mt_rand(0,9);
}
//kill the comparison cookie
setcookie("sesscompa", "$rand", time()-3600);
setcookie("sesscompb","$rand", time()-3600);
//starting and overwriting the first cookie
setcookie("sesscooka", "$rand", time()+3600);
setcookie("sesscookb","$rand", time()+3600);
?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="clogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</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="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
Checking the Login
<?php
$host="mysqlv105"; // Host name
$username="sgphplogin"; // Mysql username
$password="Sgphplogin1"; // Mysql password
$db_name="sgphplogin"; // 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);
$checkpassword= md5($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$checkpassword'";
$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"
$rand = rand(100000, 999999);
session_register("myusername");
setcookie("sesscook", "$rand");
setcookie("sesscomp", "$rand");
session_register("mypassword");
header("location:loggedin.php");
}
else {
echo "Wrong Username or Password";
}
?>
Logged in Page
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>