1. html代码:
<html> <head> <script language="javascript" type="text/javascript" > function doCheck(){ if(document.formLogin.userName.value == ""){ alert("ユーザ名を入力してください"); return false; } if(document.formLogin.password.value == ""){ alert("パスワードを入力してください"); return false; } } </script> </head> <body> <form name="formLogin" action="test.php" method="post" onSubmit="return doCheck();"> ユーザ名 :<input type="text" name="userName"><br> パスワード:<input type="text" name="password"><br><br> <input type="submit" value="提交"> </form> </body> </html>
2. php代码(test.php):
<?php $userName = $_POST['userName']; $password = $_POST['password']; if($userName == "wang" && $password == "123456") { echo "ユーザ名: ".$userName; echo "<br>"; echo "パスワード: ".$password; } else { echo "输入错误"; } ?>3. php代码(第二种写法 test.php):
<?php require_once 'dbTest.php'; class Class_A{ function Class_A(){ $userName = $_POST['userName']; $password = $_POST['password']; $class_B = new Class_B(); $array1 = $class_B->runValue(); if($userName == $array1["userName"] && $password == $array1["password"]) { echo "ユーザ名: ".$array1["userName"]; echo "<br>"; echo "パスワード: ".$password; } else { echo "输入错误"; } } } $a = new Class_A(); ?>
3.2(dbTest.php)
<?php class Class_B{ function runValue(){ $valueArray = array(); $valueArray["userName"] = "wang"; $valueArray["password"] = "123456"; return $valueArray; } } ?>4.连接数据库进行用户登录信息验证( test.php)
<?php require_once 'dbTest.php'; class Class_A{ function Class_A(){ $userName = $_POST['userName']; $password = $_POST['password']; $class_B = new Class_B(); $bool = $class_B->Uservalidate($userName,$password); if($bool) { echo "ユーザ名: ".$userName; } else { echo "誤り入力します"; } } } $a = new Class_A(); ?>4.2 (dbTest.php。 如果登录用户存在返回true,否则返回false)
<?php require_once 'connect.php'; class Class_B{ public $sql = "SELECT * FROM luser "; public $result; function userValidate($userName, $password){ $bool; $conn = new ConnectDatabase(); $conn->getConnectDatabase(); $this->result = mysql_query($this->sql." where name='".$userName."'"); //将查询结果放入数组中 $arrays = mysql_fetch_array($this->result); //因为就一条数据,所以就直接从数组拿到用户名和密码 if($arrays["name"] == $userName && $arrays["password"] == $password){ $bool = true; } else { $bool = false; } //关闭数据库连接和释放资源 $conn->mysqlClose($this->result); return $bool; } } ?>4.3(connect.php 。获得数据库的连接)
<?php class ConnectDatabase{ function getConnectDatabase(){ $conn = mysql_connect("localhost","root",null) or die('getconnect:'.mysql_error()); mysql_select_db("sourcecodecheck") or die("sourcecodecheck: ".mysql_error()); } function mysqlClose($result){ mysql_free_result($result); mysql_close(); } } ?>