欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于PHP實(shí)現(xiàn)用戶登錄注冊功能的詳細(xì)教程

 更新時間:2020年08月04日 17:10:36   作者:just for everything  
這篇文章主要介紹了基于PHP實(shí)現(xiàn)用戶登錄注冊功能的詳細(xì)教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

教程前先給大家看看小編的實(shí)現(xiàn)成果吧!

圖1:

在這里插入圖片描述

圖2:

在這里插入圖片描述

圖3:

在這里插入圖片描述

教程:
實(shí)現(xiàn)這個功能我們需要五個php文件:

login.php
(登錄界面,如圖2)

<!DOCTYPE html>
<html><head>
<title>登錄</title>
<meta name="content-type"; charset="UTF-8">
</head><body> 
<div class="content" align="center"> <!--頭部-->
 <div class="header"> <h1>登錄頁面</h1> </div> 
<!--中部--> 
<div class="middle">
 <form id="loginform" action="loginaction.php" method="post"> 
<table border="0"> <tr> 
 <td>用戶名:</td> 
<td> <input type="text" id="name" name="username" 
required="required" value="<?php
echo isset($_COOKIE[""]) ? $_COOKIE[""] : ""; ?>"> </td> </tr> 
<tr> <td>密 碼:</td> <td><input type="password" id="password" name="password"></td> 
</tr> <tr> <td colspan="2"> <input type="checkbox" name="remember"><small>記住我 </td> </tr> <tr> <td 
colspan="2" align="center" style="color:red;font-size:10px;"> <!--提示信息--> <?php
$err = isset($_GET["err"]) ? $_GET["err"] : "";
switch ($err) {
 case 1:
  echo "用戶名或密碼錯誤!";
  break;

 case 2:
  echo "用戶名或密碼不能為空!";
  break;
} ?> </td> </tr> <tr> <td colspan="2" align="center"> 
<input type="submit" id="login" name="login" value="登錄"> <input type="reset" id="reset" 
name="reset" value="重置"> </td> </tr> 
<tr> 
 <td colspan="2" align="center"> 還沒有賬號,快去<a href="register.php" rel="external nofollow" >注冊</a>吧</td>
</tr> 
</table> 
</form> 
</div> 
<!--腳部--> 
<div class="footer"> <small>Copyright &copy; 版權(quán)所有·歡迎翻版 </div> </div>
</body>
</html>  

loginaction.php
(使login.php實(shí)現(xiàn)與數(shù)據(jù)庫的連接,并校正輸入)

<?php
// $Id:$ //聲明變量
$username = isset($_POST['username']) ? $_POST['username'] : "";
$password = isset($_POST['password']) ? $_POST['password'] : "";
$remember = isset($_POST['remember']) ? $_POST['remember'] : ""; //判斷用戶名和密碼是否為空
if (!empty($username) && !empty($password)) { //建立連接
 $conn = mysqli_connect('localhost', '', '', 'user'); //準(zhǔn)備SQL語句
 $sql_select = "SELECT username,password FROM usertext WHERE username = '$username' AND password = '$password'"; //執(zhí)行SQL語句
 $ret = mysqli_query($conn, $sql_select);
 $row = mysqli_fetch_array($ret); //判斷用戶名或密碼是否正確
 if ($username == $row['username'] && $password == $row['password']) 
 { //選中“記住我”
  if ($remember == "on") 
  { //創(chuàng)建cookie
   setcookie("", $username, time() + 7 * 24 * 3600);
  } //開啟session
  session_start(); //創(chuàng)建session
  $_SESSION['user'] = $username; //寫入日志
  $ip = $_SERVER['REMOTE_ADDR'];
  $date = date('Y-m-d H:m:s');
  $info = sprintf("當(dāng)前訪問用戶:%s,IP地址:%s,時間:%s /n", $username, $ip, $date);
  $sql_logs = "INSERT INTO logs(username,ip,date) VALUES('$username','$ip','$date')";
  //日志寫入文件,如實(shí)現(xiàn)此功能,需要創(chuàng)建文件目錄logs
  $f = fopen('./logs/' . date('Ymd') . '.log', 'a+');
  fwrite($f, $info);
  fclose($f); //跳轉(zhuǎn)到loginsucc.php頁面
  header("Location:loginsucc.php"); //關(guān)閉數(shù)據(jù)庫,跳轉(zhuǎn)至loginsucc.php
  mysqli_close($conn);
 }
 else 
 { 
  //用戶名或密碼錯誤,賦值err為1
  header("Location:login.php?err=1");
 }
} else { //用戶名或密碼為空,賦值err為2
 header("Location:login.php?err=2");
} ?>

loginsucc.php
(登錄成功后界面,如圖3)

<!DOCTYPE html>
<html>
<head>
<title>登錄成功</title>
<meta name="content-type";
 charset="UTF-8">
</head>
<body> 
<div> 
<?php
// $Id:$ //開啟session
session_start(); //聲明變量
$username = isset($_SESSION['user']) ? $_SESSION['user'] : ""; //判斷session是否為空
if (!empty($username)) { ?> 
<h1>登錄成功!</h1> 歡迎您!
<?php
 echo $username; ?> 
<br/> <a href="login.php" rel="external nofollow" rel="external nofollow" >退出</a> //跳轉(zhuǎn)至主網(wǎng)頁
<?php
} else { //未登錄,無權(quán)訪問
  ?>
 <h1>你無權(quán)訪問?。?!</h1> 
<?php
} ?> </div>
</body>
</html>

register.php
(注冊界面,如圖1)

<!DOCTYPE html>
<html>
<head><title>注冊</title>
<meta name="content-type"; charset="UTF-8">
</head><body> 
<div class="content" align="center"> <!--頭部--> 
<div class="header"> <h1>注冊頁面</h1> </div> <!--中部--> 
<div class="middle"> 
<form action="registeraction.php" method="post"> <table border="0"> 
<tr> <td>用戶名:</td> 
<td><input type="text" id="id_name" name="username" required="required"></td> 
</tr> <tr>
 <td>密 碼:</td> <td><input type="password" id="password" name="password" 
required="required"></td> 
</tr> <tr>
 <td>重復(fù)密碼:</td> <td><input type="password" id="re_password" 
name="re_password" required="required"></td> </tr> <tr>
 <td>性別:</td> <td> <input type="radio" id="sex" name="sex" value="mam">男 <input type="radio" id="sex" name="sex" value="woman">女 </td> </tr> <tr>
 <td>QQ:</td> <td><input type="text" id="qq" name="qq" required="required"></td> </tr> <tr> 
<td>Email:</td> <td><input type="email" id="email" name="email" required="required"></td> </tr> <tr> 
<td>電話:</td> <td><input type="text" id="phone" name="phone" required="required"></td> </tr> <tr> 
<td>地址:</td> <td><input type="text" id="address" name="address" required="required"></td> </tr> 
<tr> <td colspan="2" align="center" style="color:red;font-size:10px;"> <!--提示信息--> 
<?php
$err = isset($_GET["err"]) ? $_GET["err"] : "";
switch ($err) {
 case 1:
  echo "用戶名已存在!";
  break;

 case 2:
  echo "密碼與重復(fù)密碼不一致!";
  break;

 case 3:
  echo "注冊成功!";
  break;
}
?> 
</td> </tr> <tr> <td colspan="2" align="center"> 
<input type="submit" id="register" name="register" value="注冊">
 <input type="reset" id="reset" name="reset" value="重置"> </td></tr> 
 <tr> <td colspan="2" align="center"> 
如果已有賬號,快去<a href="login.php" rel="external nofollow" rel="external nofollow" >登錄</a>吧! </td> </tr> </table> </form> </div> 
<!--腳部--> 
<div class="footer"> <small>Copyright &copy; 版權(quán)所有·歡迎翻版 </div> </div></body></html>

registeraction.php
(實(shí)現(xiàn)register.php連接數(shù)據(jù)庫,并向指定表單插入數(shù)據(jù))

<?php
// $Id:$ //聲明變量
$username = isset($_POST['username']) ? $_POST['username'] : "";
$password = isset($_POST['password']) ? $_POST['password'] : "";
$re_password = isset($_POST['re_password']) ? $_POST['re_password'] : "";
$sex = isset($_POST['sex']) ? $_POST['sex'] : "";
$qq = isset($_POST['qq']) ? $_POST['qq'] : "";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$phone = isset($_POST['phone']) ? $_POST['phone'] : "";
$address = isset($_POST['address']) ? $_POST['address'] : "";
if ($password == $re_password) { //建立連接
 $conn = mysqli_connect("localhost", "", "", "user"); //準(zhǔn)備SQL語句,查詢用戶名
 $sql_select = "SELECT username FROM usertext WHERE username = '$username'"; //執(zhí)行SQL語句
 $ret = mysqli_query($conn, $sql_select);
 $row = mysqli_fetch_array($ret); //判斷用戶名是否已存在
 if ($username == $row['username']) { //用戶名已存在,顯示提示信息
  header("Location:register.php?err=1");
 } else { //用戶名不存在,插入數(shù)據(jù) //準(zhǔn)備SQL語句
  $sql_insert = "INSERT INTO usertext(username,password,sex,qq,email,phone,address) 
VALUES('$username','$password','$sex','$qq','$email','$phone','$address')"; //執(zhí)行SQL語句
  mysqli_query($conn, $sql_insert);
  header("Location:register.php?err=3");
 } //關(guān)閉數(shù)據(jù)庫
 mysqli_close($conn);
} else {
 header("Location:register.php?err=2");
} ?>

建議將五個文件存于本地,便于更改,當(dāng)然不介意用linux的文本編輯器也可以直接上傳后在服務(wù)器端修改。保存好文件后:

1.創(chuàng)建數(shù)據(jù)庫及表單

小編這里是通過phpmyadmin可視化界面創(chuàng)建的數(shù)據(jù)庫和表單,進(jìn)入ip/phpmyadmin后登陸數(shù)據(jù)庫:

在這里插入圖片描述

登陸myadmin后創(chuàng)建數(shù)據(jù)庫user和表單usertext:

在這里插入圖片描述

在這里插入圖片描述

2.php文件中操作數(shù)據(jù)庫的函數(shù)

這里小編先解釋下列php數(shù)據(jù)庫操作函數(shù)的作用:
(1)mysqli_connect(“l(fā)ocalhost”, “服務(wù)器名”, “數(shù)據(jù)庫密碼”, “連接的數(shù)據(jù)庫名”);
參數(shù)描述:
“l(fā)ocalhost”,不需要更改,新手切忌不要改成服務(wù)器的ip地址,因?yàn)閯?chuàng)建數(shù)據(jù)庫的默認(rèn)的有權(quán)限訪問用戶為:

在這里插入圖片描述

“連接的數(shù)據(jù)庫名”,是數(shù)據(jù)庫不要填表單,第一步的user是數(shù)據(jù)庫,usertext是表單。

(2)mysqli_query()函數(shù)執(zhí)行某個針對數(shù)據(jù)庫的查詢:
mysqli_query(connection,query,resultmode);
參數(shù)描述:
connection必需。規(guī)定要使用的 MySQL 連接。
query必需,規(guī)定查詢字符串。
(這是一個存放mysql命令的字符串,命令內(nèi)容要用該函數(shù)才可實(shí)現(xiàn))
resultmode
可選。一個常量。可以是下列值中的任意一個:
MYSQLI_USE_RESULT(如果需要檢索大量數(shù)據(jù),請使用這個)
MYSQLI_STORE_RESULT(默認(rèn))
eg:

(3)mysqli_fetch_array() 函數(shù)
從結(jié)果集中取得一行作為關(guān)聯(lián)數(shù)組,或數(shù)字?jǐn)?shù)組,或二者兼有。
mysqli_fetch_array(result,resulttype);
參數(shù)描述:
result必需。規(guī)定由 mysqli_query()、mysqli_store_result() 或 mysqli_use_result() 返回的結(jié)果集標(biāo)識符。
resulttype可選。規(guī)定應(yīng)該產(chǎn)生哪種類型的數(shù)組??梢允且韵轮抵械囊粋€:
MYSQLI_ASSOC
MYSQLI_NUM
MYSQLI_BOTH

3修改php文件實(shí)現(xiàn)登錄注冊

下面我們來對loginaction.php和registeraction.php兩個文件進(jìn)行更改
如果數(shù)據(jù)庫創(chuàng)建和第1步時一樣,那只需要對兩個文件中的:
mysqli_connect(“l(fā)ocalhost”, “服務(wù)器名”, “數(shù)據(jù)庫密碼”, “user”);
進(jìn)行用戶信息填寫即可。
如果數(shù)據(jù)庫名稱或表單不一樣,則需要找到下面語句:
mysqli_connect(“l(fā)ocalhost”, “服務(wù)器名”, “數(shù)據(jù)庫密碼”, “user”);
SELECT;
INSERT INTO;(只在registeraction.php有)
將上述語句中的(數(shù)據(jù)庫名)user和(表單名)usertext修改成你的數(shù)據(jù)庫名和表單名。

4進(jìn)一步完善

有興趣的朋友可以試著向小編一樣創(chuàng)建超鏈接,登錄成功后跳轉(zhuǎn)到指定網(wǎng)頁;
在loginsucc.php中找到退出將login.php改為其他網(wǎng)頁(直接寫文件名的話需要放于同一目錄下)

在這里插入圖片描述

點(diǎn)擊鏈接:

在這里插入圖片描述

當(dāng)然有進(jìn)必有出,這里小編設(shè)置了一個退出登錄modal
(bootstrap使用:https://getbootstrap.com/docs/4.3/getting-started/introduction/)

在這里插入圖片描述

到此這篇關(guān)于基于PHP實(shí)現(xiàn)用戶登錄注冊功能的詳細(xì)教程的文章就介紹到這了,更多相關(guān)PHP實(shí)現(xiàn)用戶登錄注冊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • php獲取參數(shù)的幾種方法總結(jié)

    php獲取參數(shù)的幾種方法總結(jié)

    本篇文章要是對php獲取參數(shù)的幾種方法進(jìn)行了總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-02-02
  • php+websocket 實(shí)現(xiàn)的聊天室功能詳解

    php+websocket 實(shí)現(xiàn)的聊天室功能詳解

    這篇文章主要介紹了php+websocket 實(shí)現(xiàn)的聊天室功能,結(jié)合實(shí)例形式詳細(xì)分析了php+websocket 實(shí)現(xiàn)的聊天室功能相關(guān)配置、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-05-05
  • PHP的魔術(shù)常量__METHOD__簡介

    PHP的魔術(shù)常量__METHOD__簡介

    這篇文章主要介紹了PHP的魔術(shù)常量__METHOD__簡介,使用__METHOD__可以獲取類中的方法名稱,PHP5之后新增的魔術(shù)常量,需要的朋友可以參考下
    2014-07-07
  • php判斷表是否存在的方法

    php判斷表是否存在的方法

    這篇文章主要介紹了php判斷表是否存在的方法,實(shí)例分析了三種常見的判斷表的方法,涉及php操作數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • PHP實(shí)現(xiàn)把數(shù)字ID轉(zhuǎn)字母ID

    PHP實(shí)現(xiàn)把數(shù)字ID轉(zhuǎn)字母ID

    以下是對使用PHP把數(shù)字ID轉(zhuǎn)字母ID的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下
    2013-08-08
  • PHP使用mysqli操作MySQL數(shù)據(jù)庫的簡單方法

    PHP使用mysqli操作MySQL數(shù)據(jù)庫的簡單方法

    下面小編就為大家?guī)硪黄狿HP使用mysqli操作MySQL數(shù)據(jù)庫的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • PHP中的use關(guān)鍵字及文件的加載詳解

    PHP中的use關(guān)鍵字及文件的加載詳解

    最近有個朋友問我,php中的use關(guān)鍵字是不是會進(jìn)行文件的自動引入?其實(shí)use關(guān)鍵字與文件加載沒有一點(diǎn)關(guān)系,use關(guān)鍵字的作用就是聲明要使用的類所處那個命名空間之下。下面通過這篇文章跟著小編一起學(xué)習(xí)下PHP中的use關(guān)鍵字及文件的加載。
    2016-11-11
  • PHP的簡單跳轉(zhuǎn)提示的實(shí)現(xiàn)詳解

    PHP的簡單跳轉(zhuǎn)提示的實(shí)現(xiàn)詳解

    這篇文章主要介紹了PHP的簡單跳轉(zhuǎn)提示的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • PHP自定義圖片縮放函數(shù)實(shí)現(xiàn)等比例不失真縮放的方法

    PHP自定義圖片縮放函數(shù)實(shí)現(xiàn)等比例不失真縮放的方法

    這篇文章主要介紹了PHP自定義圖片縮放函數(shù)實(shí)現(xiàn)等比例不失真縮放的方法,結(jié)合實(shí)例形式分析了php圖片縮放函數(shù)的功能實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下
    2016-08-08
  • PHP面向?qū)ο笪宕笤瓌t之開放-封閉原則(OCP)詳解

    PHP面向?qū)ο笪宕笤瓌t之開放-封閉原則(OCP)詳解

    這篇文章主要介紹了PHP面向?qū)ο笪宕笤瓌t之開放-封閉原則(OCP),簡單分析了PHP面向?qū)ο箝_放-封閉原則(OCP)的概念、原理、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2018-04-04

最新評論