动态网站流程图:
1. SQL在已有的数据库 test 里创建user表,id,username,password三个字段
2. connect.php写配置文件:
<?php
error_reporting(0);
$server="localhost";//主机
$db_username="test";//你的数据库用户名
$db_password="";//你的数据库密码
$con = mysql_connect($server,$db_username,$db_password);//链接数据库
if(!$con){
die("can't connect".mysql_error());//如果链接失败输出错误
}
mysql_select_db('test',$con);//选择数据库(我的是test)
?>
3. register.html注册表单页面:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户注册页面</title>
</head>
<body>
<form action="register.php" method="post">
<p>用户名:<input type="text" name="name"></p>
<p>密 码:<input type="text" name="password"></p>
<p><input type="submit" name="submit" value="注册"></p>
</form>
</body>
</html>
4. register.php注册程序:
<?php
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST['submit'])){
exit("错误执行");
}
$name=$_POST['name'];
$password=$_POST['password'];
include('connect.php');
$sql="select * from user where username='$name'";//查询是否有相同用户名
$rs= mysql_query($sql,$con);
if(mysql_num_rows($rs)>0)//返回结果集的数目
{
echo "用户名已经存在,请重新注册";
echo "<br />";
echo "<a href='register.html'>用户注册</a>";
}else
{
$q="insert into user(id,username,password) values (null,'$name','$password')";
$reslut=mysql_query($q,$con);
if (!$reslut){
die('Error: ' . mysql_error());
}else{
echo "注册成功";
echo "<br />";
echo "<a href='login.html'>立即登入</a>";
}
}
mysql_close($con);
?>
5. login.html登入表单页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆</title>
</head>
<body>
<form name="login" action="login.php" method="post">
<p>用户名:<input type=text name="name"></p>
<p>密 码:<input type=password name="password"></p>
<p><input type="submit" name="submit" value="登录"></p>
</form>
<p> <a href="register.html"><input type="submit" value="注册"></a> </p>
</body>
</html>
6. login.php 登入程序:
<?PHP
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST["submit"])){
exit("错误执行");
}
include('connect.php');
$name = $_POST['name'];
$passowrd = $_POST['password'];
if ($name && $passowrd){
$sql = "select * from user where username = '$name' and password='$passowrd'";
$result = mysql_query($sql);
$rows=mysql_num_rows($result);
if($rows){//0 false 1 true
header("refresh:0;url=welcome.html");
exit;
}else{
echo "用户名或密码错误";
echo "
<script>
setTimeout(function(){window.location.href='login.html';},1000);
</script>
";
}
}else{
echo "表单填写不完整";
echo "
<script>
setTimeout(function(){window.location.href='login.html';},1000);
</script>";
}
mysql_close();
?>
7. welcome.html 登录成功跳转页面
<!DOCTYPE html>
<html>
<head>
<title>seccess</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>登入成功</div>
</body>
</html>