篇文章主要介紹PHP登錄功能如何編寫,感興趣的朋友參考下,希望對大家有所幫助。
具體代碼如下:
1 login.php
登錄界面中,javascript腳本用ajax方式異步請求dologin.php,dologin.php負責用戶信息驗證(包括驗證碼,php生成驗證碼可以自行搜索).登錄界面的代碼如下:
<?php session_start();?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>login</title>
<link rel="stylesheet" type="text/css" href="CSS/login.css" />
<script src="JS/ajaxhelper.js" type="text/javascript"></script>
<script src="JS/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function chkForm() {
if (m$('username').value=="") {
alert('用戶名不能為空.');
m$('username').focus();
return false;
}
if (m$('password').value=="") {
alert('密碼不能為空.');
m$('password').focus();
return false;
}
if (m$('password').value !="" && m$('username').value !="") {
var xmlhttp=createRequest();
if (xmlhttp) {
m$('loading').innerHTML="<font color='red'>loading...</font>";
var username=m$('username').value;
var pwd=m$('password').value;
var code=m$('txtCode').value;
var url="dologin.php";
xmlhttp.open("POST", url, true);
xmlhttp.onreadystatechange=ValidateResult;
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("username=" + escape(username) + "&password=" + escape(pwd) + "&code=" + escape(code));
} else {
alert('xmlHttp創(chuàng)建失敗.');
}
function ValidateResult() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
if (xmlhttp.responseText !="") {
//window.alert(xmlhttp.responseText);
var obj=eval("(" + xmlhttp.responseText + ")");
if (obj.result==true) {
alert("提示:" + obj.info);
window.location='index.php';
} else {
alert("錯誤:" + obj.info);
}
} else {
window.alert("從服務(wù)器獲取失敗");
window.location.reload();
}
m$('loading').innerHTML="";
}
}
}
}
}
function m$(id) {
return document.getElementById(id);
}
function changeCode() {
var xmlhttp=createRequest();
if (xmlhttp) {
m$('loading').innerHTML="<font color='red'>loading...</font>";
var dt=new Date().getTime();
// alert(dt);
var url="function/imagecode.php?dummay" + escape(dt);
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange=ValidateResult;
xmlhttp.send(null);
} else {
alert('xmlHttp創(chuàng)建失敗.');
}
function ValidateResult() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
var dt=new Date().getTime();
var url="function/imagecode.php?dummay" + escape(dt);
m$('imgCode').src="function/imagecode.php?dummay" + escape(dt);
m$('loading').innerHTML="";
}
}
}
}
function showTool() {
$('#pToolTip').css("display", "block");
}
function hideTool() {
$('#pToolTip').css("display", "none");
}
window.onload=initPage;
function initPage() {
$('#pToolTip').css("display", "none");
}
</script>
</head>
<body>
<p style="background-color:#2A3F55; height:80px;">
</p>
<p style="min-height:500px;">
<p class="left">
<p style="margin:120px auto auto auto; height:300px; text-align:left">
<p style="font-size:26px;color:#2A3F55; text-align:center;">Ajax PHP Demo System
<img src="Images/appstorm-icon.png" alt="appcation storm image" style="position:relative;top:-18px; left:-12px; vertical-align:middle; text-align:center;font-size:10px; width:65px; height:46px;"/>
</p>
<br/>
<hr style="border:dashed thin #2A3F55;width:70%; text-align:center;"/>
<p style="font-size:13px;color:#999999; margin:20px auto 0 auto; padding-left:200px;">
Author:<a href="#" onmousemove="showTool();" onmouseout="hideTool();">wangming</a>
</p>
<p style="font-size:13px;color:#999999;margin:20px auto auto auto; padding-left:200px;">DateTime:2009-9-1</p>
<p style="font-size:13px;color:#999999;margin:20px auto auto auto; padding-left:200px;">Version:1.0.0</p>
<p style="font-size:13px;color:#999999;margin:20px auto auto auto; padding-left:200px;">Email:wangmingemail@163.com </p>
<p id="pToolTip">
<img src="Images/ming.jpg" height="86px;"/>
<span class="authordes">
<br/>
姓名:wangming<br/>
電商06-2<br/>
</span>
</p>
</p>
</p>
<p class="right">
<form>
<br/>
<table class="flogin">
<tr>
<td>用戶名:</td>
<td><input type="text" name="username" id="username"/></td>
<td></td>
</tr>
<tr>
<td>密 碼:</td>
<td><input type="password" name="password" id="password" /></td>
<td></td>
</tr>
<tr>
<td>驗證碼:</td>
<td>
<input type="text" name="txtCode" id="txtCode" size="12" />
<img src="function/imagecode.php" id="imgCode" alt="image code" height="22px;" style=" vertical-align:bottom;"/>
</td>
<td><input type="button" class="btnrefresh" onclick="changeCode();" /></td>
</tr>
<tr>
<td></td>
<td><input type="button" class="btnlogin" onclick="chkForm();" /></td>
<td></td>
</tr>
<tr>
<td></td>
<td><span id="loading"></span></td>
<td><span id="code"></span></td>
</tr>
</table>
</form>
</p>
</p>
<p style="background-color:#2A3F55; height:60px; margin:auto 0 0 0; clear:both; text-align:center; line-height:60px; color:#FFFFFF;font-size:12px;">
?Copyright 2015.
</p>
</body>
</html>
2 ajaxhelper.js
function createRequest() {
try {
request=new XMLHttpRequest();
} catch (tryMS) {
try {
request=new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request=new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request=null;
}
}
}
return request;
}
function getActivatedObject(e) {
var obj;
if (!e) {
// early version of IE
obj=window.event.srcElement;
} else if (e.srcElement) {
// IE 7 or later
obj=e.srcElement;
} else {
// DOM Level 2 browser
obj=e.target;
}
return obj;
}
function addEventHandler(obj, eventName, handler) {
if (document.attachEvent) {
obj.attachEvent("on" + eventName, handler);
} else if (document.addEventListener) {
obj.addEventListener(eventName, handler, false);
}
}
3 dologin.php
<?php
session_start();
header("Content-type:text/html;charset=gb2312");//防止返回的中文亂碼
$name=$_POST['username'];
$pwd=$_POST['password'];
$imagecode=$_POST['code'];
if(strtoupper($imagecode)==$_SESSION["code"])
{
include("conn/conn.php");
$sql="select studentName,studentPwd from tbstudent where studentId='".$name."'";
$result=mysql_query($sql,$conn);
if($row=mysql_fetch_assoc($result))
{
if($pwd==$row['studentPwd'])
{
$_SESSION['username']=$row['studentName'];
//echo "{'result':true,'info':'登陸成功!','code':'".$_SESSION["code"]."'}";
echo "{'result':true,'info':'登陸成功!'}";
}
else
{
echo "{'result':false,'info':'密碼錯誤!'}";
}
}
else
{
echo "{'result':false,'info':'該用戶不存在!'}";
}
}
else
{
echo "{'result':false,'info':'驗證碼錯誤!'}";
}
?>
4 conn.php
<?php
$conn=$mysql_connect("localhost","root", "");
mysql_select_db("bbs",$conn);
mysql_query("SET NAMES GB2312");
?>
5
<?php
class Users {
function Users() {
}
function checkLogin($username, $userpwd) {
try {
mysql_connect("localhost", "root", "123");
mysql_select_db("studentdb");
mysql_query("SET NAMES GB2312");
$sql="select userid from tbuser where username='$username' and userpwd='" . md5(trim($userpwd)) . "'";
$result=mysql_query($sql);
if ($result) {
$arr=mysql_fetch_row($result);
$uid=$arr[0];
if ($uid !="") {
return "true|$uid login ok.$sql";
mysql_close();
} else {
return "false|login failed!$sql";
mysql_close();
}
} else {
return "false|$result link db failed!";
mysql_close();
}
}
catch(Exception $ex) {
return "false|$ex";
mysql_close();
}
}
function AddUser($name, $pwd) {
try {
mysql_connect("localhost", "root", "123");
mysql_select_db("studentdb");
mysql_query("set names gb2312");
$sql0=mysql_query("select userid from tbuser where username='$name'");
$info0=mysql_fetch_array($sql0);
$userid=$info0[0];
if ($info0 !=false) {
return "false | $name is exisis.(id:$userid)";
}
$pwd=md5(trim($pwd));
$query=mysql_query("insert into tbuser(username,userpwd)values('$name','$pwd')");
$error=mysql_errno();
if ($query) {
return "true | add ok";
} else {
return "false | $error";
}
}
catch(Exception $ex) {
return "false | $ex";
}
}
function DeleteUser($name) {
mysql_connect("localhost", "root", "123");
mysql_select_db("studentdb");
mysql_query("set names gb2312");
$name=trim($name);
$sql0=mysql_query("select userid from tbuser where username='$name'");
$info0=mysql_fetch_array($sql0);
if ($info0 !=false) {
if (mysql_query("delete from tbuser where username='$name'")) {
return "true | delete ok.(id:" . $info0[0] . ")";
} else {
return "false | 刪除失敗";
}
} else {
return "false | 刪除失敗 $name 不存在";
}
}
function UpdateUser($id, $name, $pwd) {
if (is_numeric(intval(trim($id)))) {
if ($id && $name && $pwd) {
mysql_connect("localhost", "root", "123");
mysql_select_db("studentdb");
mysql_query("set names gb2312");
$pwd=md5(trim($pwd));
$isexists=mysql_query("select * from tbuser where userid='$id'");
if (mysql_fetch_array($isexists)) {
$sql0=mysql_query("update tbuser set username='$name',userpwd='$pwd' where userid=$id");
if ($sql0) {
return "ture | update ok";
} else {
return "false | 更新失敗";
}
} else {
return "false | usrid=$id not exists.";
}
} else {
return "false |id=$id name=$name and pwd=$pwd .At least one of them is null.";
}
} else {
return "false | $id is not type of int.";
}
}
}
?>
總結(jié):以上就是本篇文的全部內(nèi)容,希望能對大家的學習有所幫助。
相關(guān)推薦:
php 無限分類 樹形數(shù)據(jù)格式化代碼實例詳解
PHP從二維數(shù)組得到N層分類樹的實現(xiàn)方法
php fseek實現(xiàn)函數(shù)讀取大文件的兩種方法
以上就是PHP登錄功能如何編寫的詳細內(nèi)容,更多請關(guān)注其它相關(guān)文章!
更多技巧請《轉(zhuǎn)發(fā) + 關(guān)注》哦!
篇文章主要介紹PHP登錄功能如何編寫,感興趣的朋友參考下,希望對大家有所幫助。
具體代碼如下:
1 login.php
登錄界面中,javascript腳本用ajax方式異步請求dologin.php,dologin.php負責用戶信息驗證(包括驗證碼,php生成驗證碼可以自行搜索).登錄界面的代碼如下:
<?php session_start();?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>login</title>
<link rel="stylesheet" type="text/css" href="CSS/login.css" />
<script src="JS/ajaxhelper.js" type="text/javascript"></script>
<script src="JS/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function chkForm() {
if (m$('username').value=="") {
alert('用戶名不能為空.');
m$('username').focus();
return false;
}
if (m$('password').value=="") {
alert('密碼不能為空.');
m$('password').focus();
return false;
}
if (m$('password').value !="" && m$('username').value !="") {
var xmlhttp=createRequest();
if (xmlhttp) {
m$('loading').innerHTML="<font color='red'>loading...</font>";
var username=m$('username').value;
var pwd=m$('password').value;
var code=m$('txtCode').value;
var url="dologin.php";
xmlhttp.open("POST", url, true);
xmlhttp.onreadystatechange=ValidateResult;
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("username=" + escape(username) + "&password=" + escape(pwd) + "&code=" + escape(code));
} else {
alert('xmlHttp創(chuàng)建失敗.');
}
function ValidateResult() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
if (xmlhttp.responseText !="") {
//window.alert(xmlhttp.responseText);
var obj=eval("(" + xmlhttp.responseText + ")");
if (obj.result==true) {
alert("提示:" + obj.info);
window.location='index.php';
} else {
alert("錯誤:" + obj.info);
}
} else {
window.alert("從服務(wù)器獲取失敗");
window.location.reload();
}
m$('loading').innerHTML="";
}
}
}
}
}
function m$(id) {
return document.getElementById(id);
}
function changeCode() {
var xmlhttp=createRequest();
if (xmlhttp) {
m$('loading').innerHTML="<font color='red'>loading...</font>";
var dt=new Date().getTime();
// alert(dt);
var url="function/imagecode.php?dummay" + escape(dt);
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange=ValidateResult;
xmlhttp.send(null);
} else {
alert('xmlHttp創(chuàng)建失敗.');
}
function ValidateResult() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
var dt=new Date().getTime();
var url="function/imagecode.php?dummay" + escape(dt);
m$('imgCode').src="function/imagecode.php?dummay" + escape(dt);
m$('loading').innerHTML="";
}
}
}
}
function showTool() {
$('#pToolTip').css("display", "block");
}
function hideTool() {
$('#pToolTip').css("display", "none");
}
window.onload=initPage;
function initPage() {
$('#pToolTip').css("display", "none");
}
</script>
</head>
<body>
<p style="background-color:#2A3F55; height:80px;">
</p>
<p style="min-height:500px;">
<p class="left">
<p style="margin:120px auto auto auto; height:300px; text-align:left">
<p style="font-size:26px;color:#2A3F55; text-align:center;">Ajax PHP Demo System
<img src="Images/appstorm-icon.png" alt="appcation storm image" style="position:relative;top:-18px; left:-12px; vertical-align:middle; text-align:center;font-size:10px; width:65px; height:46px;"/>
</p>
<br/>
<hr style="border:dashed thin #2A3F55;width:70%; text-align:center;"/>
<p style="font-size:13px;color:#999999; margin:20px auto 0 auto; padding-left:200px;">
Author:<a href="#" onmousemove="showTool();" onmouseout="hideTool();">wangming</a>
</p>
<p style="font-size:13px;color:#999999;margin:20px auto auto auto; padding-left:200px;">DateTime:2009-9-1</p>
<p style="font-size:13px;color:#999999;margin:20px auto auto auto; padding-left:200px;">Version:1.0.0</p>
<p style="font-size:13px;color:#999999;margin:20px auto auto auto; padding-left:200px;">Email:wangmingemail@163.com </p>
<p id="pToolTip">
<img src="Images/ming.jpg" height="86px;"/>
<span class="authordes">
<br/>
姓名:wangming<br/>
電商06-2<br/>
</span>
</p>
</p>
</p>
<p class="right">
<form>
<br/>
<table class="flogin">
<tr>
<td>用戶名:</td>
<td><input type="text" name="username" id="username"/></td>
<td></td>
</tr>
<tr>
<td>密 碼:</td>
<td><input type="password" name="password" id="password" /></td>
<td></td>
</tr>
<tr>
<td>驗證碼:</td>
<td>
<input type="text" name="txtCode" id="txtCode" size="12" />
<img src="function/imagecode.php" id="imgCode" alt="image code" height="22px;" style=" vertical-align:bottom;"/>
</td>
<td><input type="button" class="btnrefresh" onclick="changeCode();" /></td>
</tr>
<tr>
<td></td>
<td><input type="button" class="btnlogin" onclick="chkForm();" /></td>
<td></td>
</tr>
<tr>
<td></td>
<td><span id="loading"></span></td>
<td><span id="code"></span></td>
</tr>
</table>
</form>
</p>
</p>
<p style="background-color:#2A3F55; height:60px; margin:auto 0 0 0; clear:both; text-align:center; line-height:60px; color:#FFFFFF;font-size:12px;">
?Copyright 2015.
</p>
</body>
</html>
2 ajaxhelper.js
function createRequest() {
try {
request=new XMLHttpRequest();
} catch (tryMS) {
try {
request=new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request=new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request=null;
}
}
}
return request;
}
function getActivatedObject(e) {
var obj;
if (!e) {
// early version of IE
obj=window.event.srcElement;
} else if (e.srcElement) {
// IE 7 or later
obj=e.srcElement;
} else {
// DOM Level 2 browser
obj=e.target;
}
return obj;
}
function addEventHandler(obj, eventName, handler) {
if (document.attachEvent) {
obj.attachEvent("on" + eventName, handler);
} else if (document.addEventListener) {
obj.addEventListener(eventName, handler, false);
}
}
3 dologin.php
<?php
session_start();
header("Content-type:text/html;charset=gb2312");//防止返回的中文亂碼
$name=$_POST['username'];
$pwd=$_POST['password'];
$imagecode=$_POST['code'];
if(strtoupper($imagecode)==$_SESSION["code"])
{
include("conn/conn.php");
$sql="select studentName,studentPwd from tbstudent where studentId='".$name."'";
$result=mysql_query($sql,$conn);
if($row=mysql_fetch_assoc($result))
{
if($pwd==$row['studentPwd'])
{
$_SESSION['username']=$row['studentName'];
//echo "{'result':true,'info':'登陸成功!','code':'".$_SESSION["code"]."'}";
echo "{'result':true,'info':'登陸成功!'}";
}
else
{
echo "{'result':false,'info':'密碼錯誤!'}";
}
}
else
{
echo "{'result':false,'info':'該用戶不存在!'}";
}
}
else
{
echo "{'result':false,'info':'驗證碼錯誤!'}";
}
?>
4 conn.php
<?php
$conn=$mysql_connect("localhost","root", "");
mysql_select_db("bbs",$conn);
mysql_query("SET NAMES GB2312");
?>
5
<?php
class Users {
function Users() {
}
function checkLogin($username, $userpwd) {
try {
mysql_connect("localhost", "root", "123");
mysql_select_db("studentdb");
mysql_query("SET NAMES GB2312");
$sql="select userid from tbuser where username='$username' and userpwd='" . md5(trim($userpwd)) . "'";
$result=mysql_query($sql);
if ($result) {
$arr=mysql_fetch_row($result);
$uid=$arr[0];
if ($uid !="") {
return "true|$uid login ok.$sql";
mysql_close();
} else {
return "false|login failed!$sql";
mysql_close();
}
} else {
return "false|$result link db failed!";
mysql_close();
}
}
catch(Exception $ex) {
return "false|$ex";
mysql_close();
}
}
function AddUser($name, $pwd) {
try {
mysql_connect("localhost", "root", "123");
mysql_select_db("studentdb");
mysql_query("set names gb2312");
$sql0=mysql_query("select userid from tbuser where username='$name'");
$info0=mysql_fetch_array($sql0);
$userid=$info0[0];
if ($info0 !=false) {
return "false | $name is exisis.(id:$userid)";
}
$pwd=md5(trim($pwd));
$query=mysql_query("insert into tbuser(username,userpwd)values('$name','$pwd')");
$error=mysql_errno();
if ($query) {
return "true | add ok";
} else {
return "false | $error";
}
}
catch(Exception $ex) {
return "false | $ex";
}
}
function DeleteUser($name) {
mysql_connect("localhost", "root", "123");
mysql_select_db("studentdb");
mysql_query("set names gb2312");
$name=trim($name);
$sql0=mysql_query("select userid from tbuser where username='$name'");
$info0=mysql_fetch_array($sql0);
if ($info0 !=false) {
if (mysql_query("delete from tbuser where username='$name'")) {
return "true | delete ok.(id:" . $info0[0] . ")";
} else {
return "false | 刪除失敗";
}
} else {
return "false | 刪除失敗 $name 不存在";
}
}
function UpdateUser($id, $name, $pwd) {
if (is_numeric(intval(trim($id)))) {
if ($id && $name && $pwd) {
mysql_connect("localhost", "root", "123");
mysql_select_db("studentdb");
mysql_query("set names gb2312");
$pwd=md5(trim($pwd));
$isexists=mysql_query("select * from tbuser where userid='$id'");
if (mysql_fetch_array($isexists)) {
$sql0=mysql_query("update tbuser set username='$name',userpwd='$pwd' where userid=$id");
if ($sql0) {
return "ture | update ok";
} else {
return "false | 更新失敗";
}
} else {
return "false | usrid=$id not exists.";
}
} else {
return "false |id=$id name=$name and pwd=$pwd .At least one of them is null.";
}
} else {
return "false | $id is not type of int.";
}
}
}
?>
總結(jié):以上就是本篇文的全部內(nèi)容,希望能對大家的學習有所幫助。
相關(guān)推薦:
php 無限分類 樹形數(shù)據(jù)格式化代碼實例詳解
PHP從二維數(shù)組得到N層分類樹的實現(xiàn)方法
php fseek實現(xiàn)函數(shù)讀取大文件的兩種方法
以上就是PHP登錄功能如何編寫的詳細內(nèi)容,更多請關(guān)注其它相關(guān)文章!
更多技巧請《轉(zhuǎn)發(fā) + 關(guān)注》哦!
們先新建一個database.php,用來寫數(shù)據(jù)庫連接,之后每次需要使用的時候直接調(diào)用這個文件這樣就不需要在每個文件中重復的寫同樣的代碼,圖一貼了database的代碼,代碼中都有注釋。
(圖一)database.php
寫完數(shù)據(jù)庫連接,我們在來寫一個登錄的頁面,新建一個login.php,在圖二html代碼中寫上用戶名和密碼的輸入框,代碼中使用下拉框選擇的用戶名,如果需要使用文本框輸入,請參考登錄密碼的寫法。
(圖二)login.php
(圖三)
我們運行一下login.php文件看下效果(圖三),可以看到基本的效果已經(jīng)達到了,我們繼續(xù)在代碼的最上端加上連接數(shù)據(jù)庫的語句(圖四)。
(圖四)
數(shù)據(jù)庫的基本工作已經(jīng)做好了,我們是不是應(yīng)該去建個數(shù)據(jù),我們在MySql中新建一個數(shù)據(jù)庫命名為register,在其中建立一個users表,user_id(自增長列),userName(用戶名),password(密碼)。建好后在users表中增加幾條數(shù)據(jù)如(圖五)。
(圖五)
在用戶名的下拉框中沒有可以選擇的用戶名,我們在增加一段php代碼,使其能獲取到數(shù)據(jù)庫中的用戶名,并且顯示在下拉框中(圖六)。
(圖六)
我們刷新一下頁面看下是不是能顯示出數(shù)據(jù)庫中的2個用戶名(圖七)在不使用數(shù)據(jù)庫后記得關(guān)閉數(shù)據(jù)庫哦。
(圖七)
現(xiàn)在輸入用戶名和密碼點擊登錄是沒有反應(yīng)的,我們在加上一個from,使用post傳值,這樣login.php這個頁面就寫完了,整段代碼粘貼到(圖八)。
(圖八)
然后我們來寫判斷登錄的php文件logins.php(圖九),這段代碼我就不分開說了因為比較短,都有打備注。
(圖九)
最后測試,密碼輸入1顯示登錄成功并跳轉(zhuǎn)home頁面,輸入其他密碼顯示登錄失敗并跳回登錄頁面。沒有問題,如果大家有看不懂的地方,或者我有寫錯的地方麻煩大家評論或者私信我我都會一一回復的。
今天這個連接數(shù)據(jù)庫登錄的頁面寫完了,下一篇文章我們來寫注冊,第一次寫文章可能寫的不是太好望見諒。
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。