新聞中心
php注冊和登錄用的是一個數(shù)據(jù)庫嗎
是的,注冊和登陸用的是一個數(shù)據(jù)庫。相當(dāng)于你在一個保險柜放一個東西,在別的保險柜是拿不到的。(中大型項目除外)
站在用戶的角度思考問題,與客戶深入溝通,找到祁門網(wǎng)站設(shè)計與祁門網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋祁門地區(qū)。
php登錄頁面完整代碼連接數(shù)據(jù)庫
創(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對象
$pdo = new PDO($dns,$username,$password);
創(chuàng)建login.html,登陸頁面。
用戶名
密 碼
創(chuàng)建login.php,驗證賬號密碼。
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST["submit"])){
exit("錯誤執(zhí)行");
}//檢測是否有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'";//檢測數(shù)據(jù)庫是否有對應(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 "用戶名或密碼錯誤";
echo "
setTimeout(function(){window.location.href='login.html';},1000);
";//如果錯誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試;
}
}else{//如果用戶名或密碼有空
echo "表單填寫不完整";
echo "
setTimeout(function(){window.location.href='login.html';},1000);
";
//如果錯誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試;
}
$pdo = null;
創(chuàng)建signup.html,注冊頁面
用戶名:
密 碼:
創(chuàng)建signup.php
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST['submit'])){
exit("錯誤執(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í)行失敗輸出錯誤
}else{
echo "注冊成功";//成功輸出注冊成功
}
$pdo = null;//關(guān)閉數(shù)據(jù)庫
php+mysql怎么做登錄注冊
首先得到提交的數(shù)據(jù)
鏈接數(shù)據(jù)庫,查詢數(shù)據(jù)庫,查詢username 和pwd
提交的username 和 pwd? 跟數(shù)據(jù)庫查詢的username 和pwd做對比,
都相等那就是登陸成功
?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
PHP登陸注冊頁在本地測試時,出現(xiàn)連接數(shù)據(jù)庫失敗的情況怎么辦?
1、檢查環(huán)境正常
使用mysql -u root -p 可以進(jìn)入MySQL操作界面
直接使用/usr/local/php5/bin/php /web/test.php執(zhí)行可以連上數(shù)據(jù)庫
2、打開hosts加入
復(fù)制代碼代碼如下:127.0.0.1 qttc
使用qttc當(dāng)主機(jī)連接也正常,唯獨就不認(rèn)localhost。
3、localhost連接方式不同導(dǎo)致
為了了解PHP連接數(shù)據(jù)庫時,主機(jī)填寫localhost與其它的區(qū)別閱讀了大量資料,最后得知:
當(dāng)主機(jī)填寫為localhost時mysql會采用 unix domain socket連接
當(dāng)主機(jī)填寫為127.0.0.1時mysql會采用tcp方式連接
這是linux套接字網(wǎng)絡(luò)的特性,win平臺不會有這個問題
4、解決方法
在my.cnf的[mysql]區(qū)段里添加
復(fù)制代碼代碼如下:
protocol=tcp
保存重啟MySQL,問題解決!
當(dāng)前題目:php登錄注冊數(shù)據(jù)庫 php用戶注冊登錄
文章出自:http://www.dlmjj.cn/article/dojcdop.html