Online computer courses, code, programming tutorial and sidebar information for monitoring Canadian S&P/TSX index. Build friendship and networking. Welcome to visit my blogs often!!! I also have two other sites: YouTube Channel and Google site.
Adsense
Popular Posts
- PHP: add a download as pdf file button in report page
- How to blend adsense inside your post?
- Formatting my post
- PHPWind-- A PHP forum script applcaition in China
- Notepad++ - Add C++ compiler
- PHP connect IBM db2 database in XAMPP
- Datatable export excel wraptext and newline
- phpexcel toggle expand and hide column in EXCEL and summary
- Sweet Alert JS library - beautiful replacement of JavaScript Alert
- ActionScript 3.0 demo: create a falling snow in flash CS6
Tuesday, December 18, 2012
A simple PHP login system
To build a simple PHP login system. Here we used PHP session to login and logout, and a form and MySQl database to manage user login.
First we need to create a user table and create two entries
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`pwd` varchar(64) NOT NULL,
`email` varchar(50) NOT NULL,
`lasttime` datetime NOT NULL,
`regtime` date NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `user` VALUES (1, 'admin', '123456', 'admin@example.com',
'0000-00-00 00:00:00', '0000-00-00');
INSERT INTO `user` VALUES (2, 'jiansen', '123456', 'jiansen@example.com',
'0000-00-00 00:00:00', '0000-00-00');
config.inc.php to connect to database
<?php
//Example of using MySQL in PHP
$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "test";
$con=mysql_connect("$db_host","$db_username","$db_pass");
if($con)
{echo "connect to MySQL server ".$db_host."<br />";}
else die ("could not connect to mysql");
if(mysql_select_db("$db_name"))
{echo "connect to dtabase ".$db_name."<br />";}
else die("no database");
?>
login.html to post a from using login.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>User Login</title>
</head>
<body>
<form name="Login" method="post" action="login.php">
<table border="0" cellpadding="8" width="350" align="center">
<tr>
<td colspan="2" align="center" class="alert">User Login Interface</td>
</tr><td>Login</td>
<td><input name="username" type="text" id="username" class="textinput" /></td>
</tr><tr><td>Password</td>
<td><input name="pwd" type="password" id="pwd" class="textinput" /></td></tr>
<tr><td colspan="2" align="center">
<input type="submit" class="btn" value="login">
<input type="reset" class="btn" value="reset">
</td></tr></table>
</form>
</body>
</html>
login.php
<?php
include('config.inc.php');
//remove white space and prevent MySQL injection
session_start();
$username = addslashes(trim($_POST['username']));
$password = addslashes(trim($_POST['pwd']));
if (empty($username)||empty($password)){
echo "User name or password is empty";
exit();
}else{
$sql = "SELECT * FROM user WHERE username='$username' AND pwd='$password'";
$result = mysql_query($sql);
if ($result && mysql_num_rows($result)==1) {
$_SESSION['login'] = 'true';
$_SESSION['user']=$username;
echo "User ".$username." log in";
mysql_free_result($result);
mysql_close($con);
}else{
echo "User ".$username." not matched";
}
}
?>
is_login.php to test if user already login
<?php
session_start();
if (empty($_SESSION['login'])) {
echo "You are not login";
}else{
echo "You are login";
}
?>
logout.php
<?php
session_start();
unset( $_SESSION['login']);
echo "You are log out";
?>
Vedio for this demo:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment