- 相關(guān)推薦
php注冊和登錄界面的實(shí)現案例
當初接觸電腦編程的時(shí)間覺(jué)得一個(gè)網(wǎng)站上注冊和登錄這兩個(gè)功能很神奇,后來(lái)自己研究一下發(fā)現其實(shí)道理很簡(jiǎn)單,下面小編就為大家帶來(lái)一篇php注冊和登錄界面的實(shí)現案例,感興趣的同學(xué)一起來(lái)看看怎么實(shí)現的吧。
首先在電腦上建了幾個(gè)文件:
login.html (登錄頁(yè)面)
register.html(注冊頁(yè)面)
success.html(登錄成功跳轉頁(yè)面)
return.html(注冊成功頁(yè)面)
login.php
register.php
登錄界面和注冊界面以及success.html并沒(méi)有
什么都是些html標記如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登錄界面</title>
</head>
<body>
<form method="post" action="login.php">
賬號:
<input type="text" name="usernamel"><br/><br/>
密碼:
<input type="password" name="passwordl">
<input type="submit" value="登錄" name="subl">
<a href="http://127.0.0.1:8080/register.html">沒(méi)有賬號,注冊</a>
</form>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>會(huì )員注冊</title>
</head>
<body>
<form method="post" action="register.php">
賬 戶(hù):
<input type="text" name="username"><br/><br/>
密 碼:
<input type="password" name="password"><br/><br/>
密碼確認:
<input type="password" name="password2">
<input type="submit" value="注冊" name="sub">
</form>
</body>
</html>
return.html是注冊成功之后呈現的頁(yè)面,里面有一段js代碼是用來(lái)定時(shí)返回登錄界面的
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無(wú)標題文檔</title>
</head>
<body>
注冊成功!<br/>
5秒后返回登錄界面<br/>
你也可以直接點(diǎn)擊回到<a href="http://127.0.0.1:8080/login.html">登錄頁(yè)面</a>
<script type="text/javascript">
setTimeout("ren()",5000);
function ren()
{
window.location="http://127.0.0.1:8080/login.html";
}
</script>
</body>
</html>
register.php這是與注冊頁(yè)面相對應后臺頁(yè)面
<?php
$link=mysql_connect("localhost","root","207207");//鏈接數據庫
header("Content-type:text/html;charset=utf-8");
if($link)
{
//echo"鏈接數據庫成功";
$select=mysql_select_db("login",$link);//選擇數據庫
if($select)
{
//echo"選擇數據庫成功!";
if(isset($_POST["sub"]))
{
$name=$_POST["username"];
$password1=$_POST["password"];//獲取表單數據
$password2=$_POST["password2"];
if($name==""||$password1=="")//判斷是否填寫(xiě)
{
echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."請填寫(xiě)完成!"."\"".")".";"."</script>";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/register.html"."\""."</script>";
exit;
}
if($password1==$password2)//確認密碼是否正確
{
$str="select count(*) from register where username="."'"."$name"."'";
$result=mysql_query($str,$link);
$pass=mysql_fetch_row($result);
$pa=$pass[0];
if($pa==1)//判斷數據庫表中是否已存在該用戶(hù)名
{
echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."該用戶(hù)名已被注冊"."\"".")".";"."</script>";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/register.html"."\""."</script>";
exit;
}
$sql="insert into register values("."\""."$name"."\"".","."\""."$password1"."\"".")";//將注冊信息插入數據庫表中
//echo"$sql";
mysql_query($sql,$link);
mysql_query('SET NAMES UTF8');
$close=mysql_close($link);
if($close)
{
//echo"數據庫關(guān)閉";
//echo"注冊成功!";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/return.html"."\""."</script>";
}
}
else
{
echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."密碼不一致!"."\"".")".";"."</script>";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/register.html"."\""."</script>";
}
}
}
}
?>
login.php登錄界面對應后臺文件
<?php
header("Content-type:text/html;charset=utf-8");
$link=mysql_connect("localhost","root","207207");
if($link)
{
$select=mysql_select_db("login",$link);
if($select)
{
if(isset($_POST["subl"]))
{
$name=$_POST["usernamel"];
$password=$_POST["passwordl"];
if($name==""||$password=="")//判斷是否為空
{
echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."請填寫(xiě)正確的信息!"."\"".")".";"."</script>";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/login.html"."\""."</script>";
exit;
}
$str="select password from register where username="."'"."$name"."'";
mysql_query('SET NAMES UTF8');20 $result=mysql_query($str,$link);
$pass=mysql_fetch_row($result);
$pa=$pass[0];
if($pa==$password)//判斷密碼與注冊時(shí)密碼是否一致
{
echo"登錄成功!";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/success.html"."\""."</script>";
}
{
echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."登錄失!"."\"".")".";"."</script>";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/login.html"."\""."</script>";
}
}
}
}
?>
以上就是小編為大家帶來(lái)的php注冊和登錄界面的實(shí)現案例全部?jì)热萘,自己閑來(lái)無(wú)事做的還有許多要完善的地方,歡迎大家提問(wèn)討論,提供更簡(jiǎn)便的方法!
【php注冊和登錄界面的實(shí)現案例】相關(guān)文章:
php用cookie實(shí)現記住登錄狀態(tài)10-16
PHP中使用session實(shí)現保存用戶(hù)登錄信息09-07
php實(shí)現編輯和保存文件的方法08-07
php的curl實(shí)現get和post的代碼07-07
PHP實(shí)現文件上傳和多文件上傳07-31
實(shí)現PHP實(shí)現限制IP訪(fǎng)問(wèn)11-11
PHP實(shí)現股票趨勢圖和柱形圖08-18