溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP實現(xiàn)簡易用戶登錄系統(tǒng)

發(fā)布時間:2021-02-04 15:23:28 來源:億速云 閱讀:137 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下PHP實現(xiàn)簡易用戶登錄系統(tǒng),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

PHP簡易用戶登錄系統(tǒng),供大家參考,具體內(nèi)容如下

最近剛剛看到PHP連接數(shù)據(jù)庫的實例,于是做了一個簡易的用戶系統(tǒng)

直接上代碼

連接數(shù)據(jù)庫:connect.php

<?php
$servername = "localhost";
$username = "formbd";
$password = "formbd";
$dbname = "form";
 
// 創(chuàng)建連接
$conn = new mysqli($servername, $username, $password, $dbname);
 
// 檢測連接
if ($conn->connect_error) {
  die("連接失敗: " . $conn->connect_error);
}

?>

用戶注冊前端頁面:reg.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>用戶注冊頁面</title>
  </head>
  <body>
    <form action="reg.php" method="post">
      <p>用戶名:<input type="text" name="name"></p>
      <p>密 碼: <input type="text" name="password"></p>
      <p><input type="submit" name="submit" value="注冊">
        <a href="login.html" ><input type="button" name="login" value="已有賬號,返回登錄"></a>
      </p>
    </form>
  </body>
</html>

注冊后端處理:reg.php

<?php 
  header("Content-Type: text/html; charset=utf8");

  if(!isset($_POST['submit'])){
    exit("錯誤執(zhí)行");
  }//判斷是否有submit操作

  $name=$_POST['name'];//post獲取表單里的name
  $user_password=$_POST['password'];//post獲取表單里的password

  include('connect.php');//鏈接數(shù)據(jù)庫
  $q="insert into user(id,username,password) values (null,'$name','$user_password')";//向數(shù)據(jù)庫插入表單傳來的值的sql
  $sql = "select * from user where username = '$name'";
  
  if (($conn->query($sql))==$name) {
    echo '用戶名已存在';
    $result = $conn->query($sql);
    /*echo "
          <script>
              setTimeout(function(){window.location.href='reg.html';},1000);
          </script>

        ";*/
  }
  else {
  $conn->query($q);
  echo "注冊成功";
  echo "
          <script>
              setTimeout(function(){window.location.href='login.html';},1000);
          </script>

        ";
}
  
  $conn->close();//關(guān)閉數(shù)據(jù)庫

?>

用戶登錄前端頁面:login.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>登陸</title>
  </head>
  <body>
    <form name="login" action="login.php" method="post">
        <p>用戶名<input type=text name="name"></p>
        <p>密 碼<input type=password name="password"></p>
        <p><input type="submit" name="submit" value="登錄">
          <a href="reg.html" ><input type="button" name="reg" value="注冊"></a>
        </p>

      </form>
  </body>
</html>

登錄后端處理:login.php

<?PHP
  header("Content-Type: text/html; charset=utf8");
  if(!isset($_POST["submit"])){
    exit("錯誤執(zhí)行");
  }//檢測是否有submit操作

  include('connect.php');//鏈接數(shù)據(jù)庫
  $name = $_POST['name'];//post獲得用戶名表單值
  $passowrd = $_POST['password'];//post獲得用戶密碼單值

  if ($name && $passowrd){//如果用戶名和密碼都不為空
       $sql = "select * from user where username = '$name' and password='$passowrd'";//檢測數(shù)據(jù)庫是否有對應(yīng)的username和password的sql

       $result = $conn->query($sql);//執(zhí)行sql
       $rows=$result->fetch_assoc();//返回一個數(shù)值
       if($rows){//0 false 1 true
          header("refresh:0;url=success.php");//如果成功跳轉(zhuǎn)至success.php頁面
          exit;
       }else{
        echo "用戶名或密碼錯誤";
        echo "
          <script>
              setTimeout(function(){window.location.href='login.html';},1000);
          </script>

        ";//如果錯誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試;
       }
      

  }else{//如果用戶名或密碼有空
        echo "表單填寫不完整";
        echo "
           <script>
              setTimeout(function(){window.location.href='login.html';},1000);
           </script>";

            //如果錯誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試;
  }

  $conn->close();//關(guān)閉數(shù)據(jù)庫
?>

登錄成功后:success.php

PS:功能未完善

<?php 
include 'connect.php';
session_start(); //聲明變量
$username = isset($_SESSION['nmae']) ? $_SESSION['name'] : "";
?>
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>登陸成功</title>
  </head>
  <body>
    歡迎光臨
    <?php echo $username;?>
    <?php ?>
  </body>
</html>

以上是“PHP實現(xiàn)簡易用戶登錄系統(tǒng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI