創(chuàng)建conn.php,連接數(shù)據(jù)庫。
創(chuàng)新互聯(lián)建站是專業(yè)的邳州網(wǎng)站建設(shè)公司,邳州接單;提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行邳州網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
$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ù)庫
欄中,打 把你的數(shù)據(jù)庫導(dǎo)進(jìn)去就好了呀,再把你要測試的網(wǎng)站源碼放在www文件夾下,然后在文件名/index.php打開就行了,還有你要看你源碼中數(shù)據(jù)庫的配置,要改成你本地的,如果是在線安裝的就不要改。
error_reporting(0);
$mysql_servername = "localhost";
$mysql_username = "數(shù)據(jù)庫用戶名";
$mysql_password ="密碼";
$mysql_database ="數(shù)據(jù)庫名";
mysql_connect($mysql_servername , $mysql_username , $mysql_password);
mysql_select_db($mysql_database);
$name=$_POST['user_name'];
$passowrd=$_POST['user_password'];
if ($name $passowrd){
$sql = "SELECT * FROM admin WHERE name = '$name' and password='$passowrd'";
$res = mysql_query($sql);
$rows=mysql_num_rows($res);
if($rows){
header("location='主頁'");
exit;
}
echo "script language=javascriptalert('用戶名密碼錯誤');history.back();/script";
}else {
echo "script language=javascriptalert('用戶名密碼不能為空');history.back();/script";
}
index.php
html
head
title登錄界面/title
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
/head
body
?php
//將用戶提交的用戶名和密碼和數(shù)據(jù)庫中的用戶名和密碼逐一比對,如正確,則進(jìn)入2.php,錯誤則提示錯誤。
$username=$_post['username'];
$password=$_post['password'];
$sql="select * from userinfo where username='$username' and password='$password'";
$re=mysql_query($sql);
$row=mysql_num_rows[$re];
if(!$row){
echo "scriptalert('用戶名或密碼錯誤!');history.back(-1);/script";
}
else {
echo '用戶登錄成功!';
echo 'meta http-equiv=refresh content=2;url=2.php';
}
?
form name="login" method="post" action="index.php"
用戶名:input type="text" name="username" /br/
密碼:input type="password" name="password" /br/
input type="submit" name="submit" value="登錄"
/form
/body
html