php 实现简单登陆示例(带html代码和登录验证)

感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试HTML代码如下:

<form action="verify.PHP" method="post">
User Name:<br>
<input type="text" name="username"><br><br>
Password:<br>
<input type="password" name="password"><br><br>
<input type="submit" name="submit" value="Login">
</form>

<!--   来自 编程之家 jb51.cc (jb51.cc)-->
verify.PHP

/**
 * 
 *
 * @param 
 * @author 编程之家 jb51.cc jb51.cc
 **/
if(isset($_POST['submit'])){ 
$dbHost = "localhost"; //Location Of Database usually its localhost 
$dbUser = "xxxx"; //Database User Name 
$dbPass = "xxxxxx"; //Database Password 
$dbDatabase = "db_name"; //Database Name 

$db = MysqL_connect($dbHost,$dbUser,$dbPass)or die("Error connecting to database."); 
//Connect to the databasse 
MysqL_select_db($dbDatabase,$db)or die("Couldn't select the database."); 
//Selects the database 

/* 
The Above code can be in a different file,then you can place include'filename.PHP'; instead. 
*/ 

//Lets search the databse for the user name and password 
//Choose some sort of password encryption,I choose sha256 
//Password function (Not In all versions of MysqL). 
$usr = MysqL_real_escape_string($_POST['username']); 
$pas = hash('sha256',MysqL_real_escape_string($_POST['password'])); 
$sql = MysqL_query("SELECT * FROM users_table 
WHERE username='$usr' AND 
password='$pas' 
LIMIT 1"); 
if(MysqL_num_rows($sql) == 1){ 
$row = MysqL_fetch_array($sql); 
session_start(); 
$_SESSION['username'] = $row['username']; 
$_SESSION['fname'] = $row['first_name']; 
$_SESSION['lname'] = $row['last_name']; 
$_SESSION['logged'] = TRUE; 
header("Location: users_page.PHP"); // Modify to go to the page you would like 
exit; 
}else{ 
header("Location: login_page.PHP"); 
exit; 
} 
}else{ //If the form button wasn't submitted go to the index page,or login page 
header("Location: index.PHP"); 
exit; 
} 
users_page.PHP

/**
 * 
 *
 * @param 
 * @author 编程之家 jb51.cc jb51.cc
 **/
session_start(); 
if(!$_SESSION['logged']){ 
header("Location: login_page.PHP"); 
exit; 
} 
echo 'Welcome,'.$_SESSION['username']; 



/***   代码来自编程之家 jb51.cc(jb51.cc)   ***/

相关文章

Hessian开源的远程通讯,采用二进制 RPC的协议,基于 HTTP 传输。可以实现PHP调用Java,Python,C#等多语...
初识Mongodb的一些总结,在Mac Os X下真实搭建mongodb环境,以及分享个Mongodb管理工具,学习期间一些总结...
边看边操作,这样才能记得牢,实践是检验真理的唯一标准.光看不练假把式,光练不看傻把式,边看边练真把式....
在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较。 echo与print的区别: (...
在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring exte...
变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变...