是的,注冊(cè)和登陸用的是一個(gè)數(shù)據(jù)庫。相當(dāng)于你在一個(gè)保險(xiǎn)柜放一個(gè)東西,在別的保險(xiǎn)柜是拿不到的。(中大型項(xiàng)目除外)
創(chuàng)新互聯(lián)建站專注于鹿城企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站建設(shè)。鹿城網(wǎng)站建設(shè)公司,為鹿城等地區(qū)提供建站服務(wù)。全流程按需制作網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
創(chuàng)建conn.php,連接數(shù)據(jù)庫。
$dns = 'mysql:host=127.0.0.1;dbname=test';
$username = 'root';
$password = 'root';
// 1.連接數(shù)據(jù)庫,創(chuàng)建PDO對(duì)象
$pdo = new PDO($dns,$username,$password);
創(chuàng)建login.html,登陸頁面。
用戶名
密 碼
創(chuàng)建login.php,驗(yàn)證賬號(hào)密碼。
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST["submit"])){
exit("錯(cuò)誤執(zhí)行");
}//檢測(cè)是否有submit操作
include('conn.php');//鏈接數(shù)據(jù)庫
$name = $_POST['name'];//post獲得用戶名表單值
$pwd = sha1($_POST['password']);//post獲得用戶密碼單值
if ($name $pwd){//如果用戶名和密碼都不為空
$sql = "select * from user where username = '$name' and password='$pwd'";//檢測(cè)數(shù)據(jù)庫是否有對(duì)應(yīng)的username和password的sql
$stmt = $pdo-prepare($sql);
$stmt-execute();
if($stmt-fetch(PDO::FETCH_BOUND)){//0 false 1 true
header("refresh:0;url=welcome.html");//如果成功跳轉(zhuǎn)至welcome.html頁面
exit;
}else{
echo "用戶名或密碼錯(cuò)誤";
echo "
setTimeout(function(){window.location.href='login.html';},1000);
";//如果錯(cuò)誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試;
}
}else{//如果用戶名或密碼有空
echo "表單填寫不完整";
echo "
setTimeout(function(){window.location.href='login.html';},1000);
";
//如果錯(cuò)誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試;
}
$pdo = null;
創(chuàng)建signup.html,注冊(cè)頁面
用戶名:
密 碼:
創(chuàng)建signup.php
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST['submit'])){
exit("錯(cuò)誤執(zhí)行");
}//判斷是否有submit操作
$name=$_POST['name'];//post獲取表單里的name
$pwd = sha1($_POST['password']);//post獲取表單里的password
include('conn.php');//鏈接數(shù)據(jù)庫
$sql="insert into user(id,username,password) values (null,'$name','$pwd')";//向數(shù)據(jù)庫插入表單傳來的值的sql
$stmt = $pdo-prepare($sql);
$stmt-execute();
$stmt-fetch(PDO::FETCH_BOUND);
if (!$stmt){
die('Error: ' . $stmt-getMessage());//如果sql執(zhí)行失敗輸出錯(cuò)誤
}else{
echo "注冊(cè)成功";//成功輸出注冊(cè)成功
}
$pdo = null;//關(guān)閉數(shù)據(jù)庫
注冊(cè)頁面:reg.html
form action="reg.php" method="POST"
table
trtd用戶名:/tdtdinput type="username" size="20"/td/tr
trtd密碼:/tdtdinput type="userpass" size="20"/td/tr
trtd確認(rèn)密碼:/tdtdinput type="ruserpass" size="20"/td/tr
trtd郵箱:/tdtdinput type="email" size="50"/td/tr
trtd電話:/tdtdinput type="telphone" size="20"/td/tr
trtdinput type="Submit" value="注冊(cè)"/td/tr
/table
/form
接收頁面:reg.php
%php
$db = mysql_connect("localhost", "root", "12345");
mysql_select_db("dataname", $db);
mysql_query("insert into tablename(username, userpass, email, telphone) values('$_POST[username]', '$_POST[userpass]', '$_POST[email]', '$_POST[telphone]')");
echo "注冊(cè)成功";
%
首先得到提交的數(shù)據(jù)
鏈接數(shù)據(jù)庫,查詢數(shù)據(jù)庫,查詢username 和pwd
提交的username 和 pwd? 跟數(shù)據(jù)庫查詢的username 和pwd做對(duì)比,
都相等那就是登陸成功
?php
mysql_connect('localhost','root','123');
mysql_select_db('lx');
mysql_query("SET?CHARACTER?SET?utf8");
mysql_query("SET?NAMES?utf8");
//數(shù)據(jù)庫lx?表user??字段id?username??pwd
//用md5加密,可以自己試試????
if(isset($_POST['user'])$_POST['tijiao']?==?'success'){
$query?=?mysql_query("select?pwd?from?user?where?username?=?'".$_POST['user']."'");
$num?=?mysql_num_rows($query);
if($num??0?){
while($info?=?mysql_fetch_array($query)){
if($info['pwd']?==?md5($_POST['pwd'])){
echo?'登陸成功';
}else{
echo?'登陸失敗';????
}
}
}else{
echo?'登陸失敗';
}
}
?
form?action=""?method="get"/
table?border="0"?cellspacing="0"?cellpadding="0"?
tr
td?class="fieldKey"?width="30%"用戶名:/td
td?class="fieldValue"?width="100%"input?type="text"?name="user"?//td
/tr
trtd?height="10"/td/tr
tr
td?class="fieldKey"密碼:/td
td?class="fieldValue"input?type="password"?name="pwd"?//td
/tr
/table
input?type="hidden"?name="tijiao"?value="success"?/
input?type="submit"?value="登陸"/
/form
$query="select * from reg where username='$username' and pwd='$pwd'";
將該語句這樣寫:$query = sprintf("select * from reg where username=%s and pwd=%s ", $username, $pwd);