php實現(xiàn)登陸驗證數(shù)據(jù)庫 php中驗證碼如何實現(xiàn)登錄驗證

用PHP+JS+MYSQL實現(xiàn)用戶登陸驗證,的具體步驟是怎么樣的呢

第一次學(xué)PHP就是做這個驗證..

專注于為中小企業(yè)提供網(wǎng)站建設(shè)、做網(wǎng)站服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)隆子免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

html做個表單,

當(dāng)表單onsubmit=return?check();調(diào)用自寫js來判斷用戶名和密碼是否為空,

如果是空就alert不能為空,然后return?false;相反則return?true;

而接收的PHP也要驗證是否為空,如果嚴(yán)謹(jǐn)點還要對提交的數(shù)據(jù)進(jìn)行過濾,防止sql注入。

然后php再根據(jù)提交的數(shù)據(jù)搜MYSQL,如果用戶名和密碼都相同時,echo?登錄成功,相反則登錄失敗.

html

script

function?check(obj){

with(obj){

if((user.value+"").length?=?0){

alert("用戶名不能為空");

return?false;

}else?if((pwd.value+"").length?=?0){

alert("用戶名不能為空");

return?false;

}else{

return?true;

}

}

}

/script

body

form?action="check.php"?method="post"?onsubmit="return?check(this)"

input?type="text"?name="user"?value=""

input?type="password"?name="pwd"?value=""

input?type="submit"?name="submit"?value="登錄"

input?type="cancel"?name="cancel"?value="取消"

/form

/body

/html

?php

$conn?=?mysql_connect(?"數(shù)據(jù)庫地址",?"數(shù)據(jù)庫用戶名",?"密碼"?);

mysql_query("set?names?utf8");

mysql_select_db(?"數(shù)據(jù)庫名"?);

function?inject_check($sql_str){?????

return?preg_match("/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|%|eval|=|and|'|;|exec|count/i",?$sql_str);????//?進(jìn)行過濾

}

if(!empty($_POST)){

foreach($_POST?as?$key?=?$value){

if(inject_check($value)){

exit?('scriptalert("地址欄輸入發(fā)現(xiàn)有非法字符,請重新輸入!");history.go(-1);/script');

die?();

}

}

}

$res?=?mysql_query("SELECT?count(*)?as?m?from?`表名`?where?用戶名='${_POST['user']}'?AND?密碼='${_POST['pwd']}'");

$row?=?mysql_fetch_object($res);

if($row-m?0){

echo?"登陸成功";

}else{

echo?"用戶名或密碼錯誤";

}

exit;

?

PHP數(shù)據(jù)庫驗證登陸

你記住 所有在 引號里面的 東西都是字符串!?。。?!

if("SELECT username,passwd FROM wen WHERE $username=$user,$passwd=$passwd"){

echo "scriptalert('歡迎登錄!');window.location.href='index1.php';/script";

}

這一句代碼本身就是不通的.

還有就是 你的sql語句也是錯誤的。

參考如下:

$sql = 'SELECT username,passwd FROM wen WHERE username="'.$user.'" AND passwd="'.$passwd.'"';

$result = mysql_query($sql,$conn);

手動回復(fù),不謝

怎么使用PHP PDO 寫一個登錄驗證代碼

span style="font-size:18px;"span style="font-size:18px;"html

head

meta http-equiv="Content-Type" content="text/html; charset="utf-8" /

/head

?php

session_start(); //初始化session變量

$username = $_POST['name']; ? ? ? ? ?//接收表單提交的用戶名

$password=md5($_POST['pwd']); ? //接收表單提交的密碼

class chkinput ? ?//定義類

{ ? ? ? ? ? ? ?

var $name;

var $pwd;

function chkinput($x,$y) //定義一個方法

{

$this-name=$x; //將管理員名稱傳給類對象$this-name

$this-pwd=$y; //將管理員密碼傳給類對象$this-pwd

}

function checkinput()

{

include("conn.php"); ? ?//連接數(shù)據(jù)庫文件

$sql=mysql_query("select username,password from admin where username='".$this-name."' and password='".$this-pwd."'",$conn);

$info=mysql_fetch_array($sql); ? ? ? ?//檢索管理員名稱和密碼是否正確

if($info==false) //如果管理員名稱或密碼不正確,則彈出相關(guān)提示信息

{ ? ? ? ? ? ? ? ? ?

echo "script language='javascript'alert('您輸入的管理員名稱或密碼錯誤,請重新輸入!');history.back();/script";

exit;

}

else //如果管理員名稱或密碼正確,則直接跳轉(zhuǎn)到登陸成功后界面

{ ? ? ? ? ? ? ? ? ? ? ? ? ? ?

echo "scriptwindow.location='home.php';/script";

$_SESSION['admin_name']=$info['username']; ? ?//將管理員名稱存到$_SESSION[admin_name]變量中

$_SESSION['pwd']=$info['password']; ? ? ? ? ? ?////將管理員名稱存到$_SESSION[pwd]變量中

}

}

}

$obj=new chkinput(trim($username),trim($password)); ? ? ?//創(chuàng)建對象

$obj-checkinput(); ? ? ? ? //調(diào)用類

?

/html/span/span

php登陸數(shù)據(jù)庫驗證問題

if($_POST[id]=='$a') $a 不能用單引號括起來,要么用雙引號,要不不用,單引號擴(kuò)住的話,它不會編譯,只會把它堪稱是字符串 $a 來看待。我說一個問題哦,僅僅是我自己理解的,你在頁面?zhèn)鬏數(shù)臅r候接受 id ($_POST[id]==)的值,而你又要和$a(用戶名$lr[luser]; )比較( _POST[id]=='$a' ),你的用戶民 是字符串,而id是數(shù)字類型的,怎么能相等...

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用戶登錄驗證的代碼

$db=mysql_connect(連接你的數(shù)據(jù)庫);

mysql_select_db('login', $db);

$u=$_POST['u'];

$p=$_POST['p'];

if($u $p){

$query=mysql_query('select * from exam where name=\''.$u.'\'',$db);

if($re=mysql_fetch_array($query,MYSQL_ASSOC)$re['password']==$p){

exit('scriptalert(\'登陸成功\');history.back();/script');

}else{

exit('scriptalert(\'用戶名不存在,或密碼錯誤\');history.back();/script');

}

}

網(wǎng)頁題目:php實現(xiàn)登陸驗證數(shù)據(jù)庫 php中驗證碼如何實現(xiàn)登錄驗證
當(dāng)前網(wǎng)址:http://muchs.cn/article14/hjcsde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、手機網(wǎng)站建設(shè)、網(wǎng)站排名、Google、面包屑導(dǎo)航、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司