php註冊和登入介面的實作案例

墨辰丷
發布: 2023-03-28 16:18:01
原創
3445 人瀏覽過

本篇主要介紹php註冊和登入介面的實作案例,有興趣的朋友參考下,希望對大家有幫助。

當初我覺得一個網站上註冊和登入這兩個功能很神奇,後來自己研究一下發現其實道理很簡單,接下來看一下怎麼實現的吧。 。 。 。

我在我的電腦上建立了幾個檔案:

login.html (登入頁面)

#register.html(註冊頁面)

success.html(登入成功跳轉頁面)

return.html(註冊成功頁面)

login.php

#register.php

登入介面和註冊介面以及success.html並沒有

什麼都是些html標記如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录界面</title>
</head>

<body>
<form method="post" action="login.php">
账号:
<input type="text" name="usernamel"><br/><br/>
密码:
<input type="password" name="passwordl">
<input type="submit" value="登录" name="subl">
<a href="http://127.0.0.1:8080/register.html">没有账号,注册</a>
</form>
</body>
</html>
登入後複製

##

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>会员注册</title>
</head>

<body>
<form method="post" action="register.php">
账  户:
<input type="text" name="username"><br/><br/>
密  码:
<input type="password" name="password"><br/><br/>
密码确认:
<input type="password" name="password2">
<input type="submit" value="注册" name="sub">
</form>
</body>
</html>
登入後複製

return.html是註冊成功之後呈現的頁面,裡面有一段js程式碼是用來定時回傳登入介面的

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>

<body>
注册成功!<br/>
5秒后返回登录界面<br/>
你也可以直接点击回到<a href="http://127.0.0.1:8080/login.html">登录页面</a>
<script type="text/javascript">
setTimeout("ren()",5000);
function ren()
{
  window.location="http://127.0.0.1:8080/login.html";
}

</script>

</body>
</html>
登入後複製

register.php這是與註冊頁面相對應後台頁面

<?php
$link=mysql_connect("localhost","root","207207");//链接数据库
header("Content-type:text/html;charset=utf-8");
if($link)
  {  
    //echo"链接数据库成功";
    $select=mysql_select_db("login",$link);//选择数据库
    if($select)
    {
      //echo"选择数据库成功!";
      if(isset($_POST["sub"]))
      {
        $name=$_POST["username"];
        $password1=$_POST["password"];//获取表单数据
        $password2=$_POST["password2"];
        if($name==""||$password1=="")//判断是否填写
        {
          echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."请填写完成!"."\"".")".";"."</script>";
          echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/register.html"."\""."</script>";    
          exit;
        }
        if($password1==$password2)//确认密码是否正确
        {
        $str="select count(*) from register where username="."&#39;"."$name"."&#39;";
        $result=mysql_query($str,$link);
        $pass=mysql_fetch_row($result);
        $pa=$pass[0];
        if($pa==1)//判断数据库表中是否已存在该用户名
        {
        
        echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."该用户名已被注册"."\"".")".";"."</script>";
        echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/register.html"."\""."</script>";   
        exit; 
        }
        
        
        $sql="insert into register values("."\""."$name"."\"".","."\""."$password1"."\"".")";//将注册信息插入数据库表中
        //echo"$sql";
        mysql_query($sql,$link);
        mysql_query(&#39;SET NAMES UTF8&#39;);
        $close=mysql_close($link);
        if($close)
        {
          //echo"数据库关闭";
          //echo"注册成功!";
          echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/return.html"."\""."</script>";    
        }
        }
        else
        {
          echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."密码不一致!"."\"".")".";"."</script>";
          echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/register.html"."\""."</script>";    
        }
      }
    }
  }
?>
登入後複製

login.php登入介面對應後台檔案

<?php
  header("Content-type:text/html;charset=utf-8");
$link=mysql_connect("localhost","root","207207");
if($link)
{
  $select=mysql_select_db("login",$link);
  if($select)
  {
    if(isset($_POST["subl"]))
    {
      $name=$_POST["usernamel"];
      $password=$_POST["passwordl"];
      if($name==""||$password=="")//判断是否为空
      {
        echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."请填写正确的信息!"."\"".")".";"."</script>";
        echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/login.html"."\""."</script>";
        exit;
      }
      $str="select password from register where username="."&#39;"."$name"."&#39;";
      mysql_query(&#39;SET NAMES UTF8&#39;);20       $result=mysql_query($str,$link);
      $pass=mysql_fetch_row($result);
      $pa=$pass[0];
      if($pa==$password)//判断密码与注册时密码是否一致
      {
        echo"登录成功!";
        echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/success.html"."\""."</script>";
      }
      {  
        echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."登录失败!"."\"".")".";"."</script>";
        echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/login.html"."\""."</script>";
      }
    }
    
  }
}
?>
登入後複製

#自己閒來無事做的還有許多要完善的地方,歡迎大家提問討論,提供更簡便的方法!

以上就是本文的全部內容,希望對大家的學習有所幫助。


相關推薦:

php使用redis長連線有哪些步驟

#php應用容器化與部署使用詳解

#php資料匯出如何實作

php簡單的中獎演算法實例

以上是php註冊和登入介面的實作案例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!