首页 » Program » PHP&&Mysql » 正文

php+mysql实现超原始的注册登录

涉及主要知识点:

1.php操作mysql

2.加密函数md5()

3.php用?页面间传递参数

4.$_GET[]和$_POST[]

平台:redhat6+php5.3+mysql14.14+ Apache/2.2.15 (经典LAMP)

注册页面(regtest.php):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
<title>welcome to regtest.php</title>  
</head>  
 
    
<body>  
<h1>reg</h1>  
<form method="post" action="reg_check.php">  
  username<input type="text" name="username" > 
 
  password:<input type="password" name="password">


  password again:<input type="password" name="pwd_again">   
  <input type="submit" value="submit">  
  <input type="reset" value="clean">   
</form>  
</body>  
</html>  

注册验证页面(reg_check.php):

用于验证注册和写入数据库

<?php
  $db = mysql_connect("127.0.0.1","root","root") or die("Fail to connect db");  
  mysql_select_db("userdb",$db) or die ("can't connect to userdb".mysql_error());  
  $username=$_POST['username'];  
  $password=$_POST['password'];  
  $pwd_again=$_POST['pwd_again'];   
  if($username==""||$password=="")  
  {  
      echo"error:username or password empty";  
  }  
  else   
  {  
      if($password!=$pwd_again)  
      {  
          echo"password is different!";  
          echo"<a href='regtest.php'>input again</a>";  
      }  
      else  
      {  
      $md5pass=md5($password);
          $sql="insert into user(username,password) values('$username','$md5pass')";  
          $result=mysql_query($sql);  
          if(!$result)  
          {  
              echo"Fail reg!".mysql_error();  
              echo"<a href='regtest.php'>返回</a>";  
          }  
          else   
          {  
              echo"Success reg!";  
          }  
      }  
  }  
?>  


看下数据库:

登录页面(logintest.php):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
<title>welcome to login</title>  
</head>   
<body>  
<form method="post" action="login_check.php">  

username:<input type="text" name="username" >  
password:<input type="password" name="password">  

<input type="submit" value="login">  
<input type="reset" value="clean">  
<a href="regtest.php" >reg</a>   
</form>  
</body>  
</html>  

登陆验证页面(login_check.php)

<?php  
$username=$_POST['username'];  
$password=$_POST['password'];  
$db = mysql_connect("127.0.0.1","root","root") or die("Fail to connect db!");  
mysql_select_db("userdb",$db) or die ("Can't connect to userdb".mysql_error());  
$sql = "select * from user where username='$username'";  
$result = mysql_query($sql);  
$column = mysql_fetch_array($result);  
if($username == "")  
{  
    echo"<script type='text/javascript'>alert('input username');location='logintest.php';  
            </script>";  
}  
else
{
   if($password == "")  
   {   
      echo"<script type='text/javascript'>alert('input password');location='logintest.php';</script>";  
   }  
   else  
   {    
      if(($column[username] == $username) && ($column[password] == md5($password)))  
      {   
         echo"<script type='text/javascript'>alert('Success login!');location='welcome.php?uname=$username';</script>";  
      }  
      else   
          echo"<script type='text/javascript'>alert('username or password error');location='logintest.php';</script>";
   }  
}
?>

欢迎页面(welcome.php)

<?php

$username = $_GET['username'];
echo "welcome ".$username."!";
 
?>


超原始的注册登录完成!还有很多不足,日后再完善!