亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2020年07月10日 08:33:39   作者:行云blog  
這篇文章主要為大家詳細(xì)介紹了PHP實現(xiàn)簡易用戶登錄系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論