Paste_Image.png
Paste_Image.png
mysql> create table user(
-> id smallint unsigned auto_increment key,
-> username varchar(50) not null,
-> password varchar(50) not null
-> );
reg.html
<html>
<head>
<title>reg</title>
</head>
<body>
<form action="doAction.php?act=reg" method="post">
<table cellspacing="0" cellpadding="0" width="80%" border="0">
<tr>
<td>
用戶名:
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="注冊(cè)"></td>
</tr>
</table>
</form>
</body>
</html>
login.html
<html>
<head>
<title></title>
</head>
<body>
<h1>慕課網(wǎng)登陸界面</h1>
<form action="doAction.php?act=login" method="post">
用戶名:<input type="text" name="username">
<br/>
密碼:<input type="password" name="password">
<br/>
<input type="submit" value="登陸">
</form>
</body>
</html>
doAction.php
<?php
header("content-type:text/html;charset=utf-8");
$act = $_GET['act'];
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? md5($_POST['password']) : "";
mysql_connect('localhost', 'root', '');
mysql_select_db('test');
mysql_set_charset("utf8");
if ($act == 'reg') {
$sql = "insert user(username,password) values('{$username}','{$password}')";
$result = mysql_query($sql);
if ($result) {
echo "注冊(cè)成功成洗,3秒后跳轉(zhuǎn)到登陸界面";
echo "<meta http-equiv='refresh' content = '3;url=login.html' />";
} else {
echo "注冊(cè)失敗灵奖,請(qǐng)重新注冊(cè)";
echo "<meta http-equiv='refresh' content = '3;url=reg.html' />";
}
} else if ($act == 'login') {
$sql = "select * from user where username='{$username}' and password='{$password}'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if ($row) {
echo "登陸成功,3秒鐘后跳轉(zhuǎn)到首頁(yè)";
echo "<meta http-equiv='refresh' content='3;url=http://www.imooc.com'/>";
} else {
echo "登陸失敗版扩,請(qǐng)重新登陸";
echo "<meta http-equiv='refresh' content = '3;url=reg.html' />";
}
}