溫馨提示×

溫馨提示×

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

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

使用php怎么編寫一個注冊和登錄界面

發(fā)布時間:2021-01-28 13:52:57 來源:億速云 閱讀:331 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹使用php怎么編寫一個注冊和登錄界面,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

<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">
賬&nbsp;&nbsp;戶:
<input type="text" name="username"><br/><br/>
密&nbsp;&nbsp;碼:
<input type="password" name="password"><br/><br/>
密碼確認(rèn):
<input type="password" name="password2">
<input type="submit" value="注冊" name="sub">
</form>
</body>
</html>

return.html是注冊成功之后呈現(xiàn)的頁面,里面有一段js代碼是用來定時返回登錄界面的

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標(biāo)題文檔</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這是與注冊頁面相對應(yīng)后臺頁面

<?php
$link=mysql_connect("localhost","root","207207");//鏈接數(shù)據(jù)庫
header("Content-type:text/html;charset=utf-8");
if($link)
  {  
    //echo"鏈接數(shù)據(jù)庫成功";
    $select=mysql_select_db("login",$link);//選擇數(shù)據(jù)庫
    if($select)
    {
      //echo"選擇數(shù)據(jù)庫成功!";
      if(isset($_POST["sub"]))
      {
        $name=$_POST["username"];
        $password1=$_POST["password"];//獲取表單數(shù)據(jù)
        $password2=$_POST["password2"];
        if($name==""||$password1=="")//判斷是否填寫
        {
          echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."請?zhí)顚懲瓿桑?quot;."\"".")".";"."</script>";
          echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://127.0.0.1:8080/register.html"."\""."</script>";    
          exit;
        }
        if($password1==$password2)//確認(rèn)密碼是否正確
        {
        $str="select count(*) from register where username="."'"."$name"."'";
        $result=mysql_query($str,$link);
        $pass=mysql_fetch_row($result);
        $pa=$pass[0];
        if($pa==1)//判斷數(shù)據(jù)庫表中是否已存在該用戶名
        {
        
        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"."\"".")";//將注冊信息插入數(shù)據(jù)庫表中
        //echo"$sql";
        mysql_query($sql,$link);
        mysql_query('SET NAMES UTF8');
        $close=mysql_close($link);
        if($close)
        {
          //echo"數(shù)據(jù)庫關(guān)閉";
          //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登錄界面對應(yīng)后臺文件

<?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"."("."\""."請?zhí)顚懻_的信息!"."\"".")".";"."</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="."'"."$name"."'";
      mysql_query('SET NAMES UTF8');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>";
      }
    }
    
  }
}
?>

關(guān)于使用php怎么編寫一個注冊和登錄界面就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

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

php
AI