1. 程式人生 > >php 簡單的註冊和登入,帶資料庫。適合新手學習。

php 簡單的註冊和登入,帶資料庫。適合新手學習。

<?PHP
  header("Content-Type: text/html; charset=utf8");//編碼格式
  if(!isset($_POST["submit"])){
    exit("錯誤執行");
  }//檢查登陸操作
    //引入資料庫
  include('conn.php');
  //接收表單傳值
  $name = $_POST['name'];
  $passowrd = $_POST['password'];

  if ($name && $passowrd){//如果存在使用者名稱和密碼
       $sql = "select * from user where username = '$name' and password='$passowrd'";
       //去資料庫比對登陸資訊
       $result = mysql_query($sql);//執行sql
       $rows=mysql_num_rows($result);//返回一個數值
       if($rows){
          header("refresh:0;url=welcome.html");//匹配成功跳轉至welcome.html頁面
          exit;
       }else{
        echo "使用者名稱或密碼錯誤";
        echo "
          <script>
              setTimeout(function(){window.location.href='login.html';},2000);
          </script>

        ";//如果錯誤,返回從新登陸
       }
       }else{//如果使用者名稱或密碼有空
        echo "請填寫使用者名稱和密碼";
        echo "
           <script>
              setTimeout(function(){window.location.href='login.html';},20000);
           </script>";

            //如果錯誤,返回從新登陸
  }

  mysql_close();//關閉資料庫
?>
註冊頁面zhuce.html