本文目錄一覽:
PHP做一個用戶登錄頁面
index.html登錄頁面代碼如下:
!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”
html xmlns=””
head
meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ /
title登錄示例/title
/head
body
form id=”forms” name=”forms” method=”post” action=”loginchk.php”
用戶名:input type=”text” id=”uname” name=”uname” value=””/br/
密碼:input type=”password” id=”upass” name=”upass” value=””/br/
input type=”submit” id=”loginbtn” value=”立即登錄”/
input type=”reset” id=”resetbtn” value=”重新填寫”/
/form
/body
/html
loginchk.php 的PHP程序代碼如下:
?php
$uname=trim($_REQUEST[“uname”]);
$upass=trim($_REQUEST[“upass”]);
if($uname==”admin”$upass==”admin”)
{
echo “登錄成功”;
}
else
{
echo “登錄失敗,a href=’index.html’重新登錄/a”;
}
?
以上只是一個簡單示例,真正的開始,需要考到很多因素,比如說登錄前有效性檢查,加入登錄驗證碼,程序需要連接資料庫進行用戶匹配等。
希望對你有幫助 。
如果使用資料庫進行進行匹配的話,PHP程序可以這樣改進一下。
?php
$uname=trim($_REQUEST[“uname”]);
$upass=trim($_REQUEST[“upass”]);
$con = mysql_connect(“localhost”,”root”,”root”);
mysql_select_db(“dbname”, $con);
$result = mysql_query(“select * from dusers where uname=’$uname’ and upass=’$upass'”);
$rs = mysql_fetch_array($result);
if($rs)
{
echo “登錄成功”;
}
else
{
echo “登錄失敗,a href=’index.html’重新登錄/a”;
}
?
不過你需要連接到你自己的指定的資料庫和數據表。
用php製作用戶登錄認證網頁(cookie方法和session方法)
?php
//用戶登陸
if(isset($_POST[“sub”]))
{
$conn=mysql_connect(“localhost”,”root”,”root”)or die(“資料庫伺服器連接錯誤”.mysql_error());
mysql_select_db(“hu”,$conn);
$mysql=”SELECT id FROM user
WHERE name = ‘$_POST[name]’
AND PASSWORD = ‘$_POST[password]’
“;
$result=mysql_query($mysql,$conn);
$isrows=mysql_num_rows($result);
if ($isrows0)
{
$row=mysql_fetch_assoc($result);
$time=time()+3600;//cookie保存的時間
SETCOOKIE(“name”,$_POST[name],$time);
SETCOOKIE(“uid”,$row[id],$time);
SETCOOKIE(“islogin”,true,$time);
HEADER(“Location: index.php”);//登陸成功,你要跳轉的頁面
}
else
{
echo “用戶密碼有誤”;
}
}
?
html
titlecookie test/title
body
form action=”login.php” method=”post”
table align=”center” border=”1″ width=”250″
captionh1用戶登錄/h1/caption
tr
th 用戶名/th
td
input type=”text” name=”name”
/td
/tr
tr
th密 碼/th
td
input type=”password” name=”password”
/td
/tr
tr
td colspan=”2″ align=”center”
input type=”submit” name=”sub” value=”登錄”
/td
/tr
/table
/form
/body
/html
用php製作用戶登錄認證網頁
將用戶名和密碼提交到指定的頁面,如checkform.php,然後在該頁面中以傳來的用戶名和密碼為條件,在資料庫中查找,如果有記錄的話,成功登陸,如果沒有,就說明沒有該用戶,活著用戶名錯誤
原創文章,作者:WO4TG,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/130413.html