CREATE DATABASE my_to_do_list;
use my_to_do_list;
CREATE TABLE users(
id int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(16) NOT NULL,
email VARCHAR(100) NOT NULL,
password VARCHAR(40) NOT NULL,
PRIMARY KEY(id)
)CHARSET=utf8;
SHOW tables;
describe users;
CREATE TABLE items(
id int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
title varchar(50) NOT NULL,
description tinytext NOT NULL,
complete_by date,
complete_on date,
user_id int(10) unsigned NOT NULL,
CONSTRAINT FOREIGN KEY (user_id) REFERENCES users(id)
)CHARSET=utf8;
index.php
list.php
signup.php
signout.php
连接MYSQL
加密用户密码
insert into users values(2,’bob’,’bob@qq.com’,sha1(‘1234’));
index.php
<!DOCTYPE html>
<html>
<head>
<title>登入</title>
<meta charset="utf-8">
</head>
<?php function checkBOM($filename) { global $auto; $contents = file_get_contents ( $filename ); $charset [1] = substr ( $contents, 0, 1 ); $charset [2] = substr ( $contents, 1, 1 ); $charset [3] = substr ( $contents, 2, 1 ); if (ord ( $charset [1] ) == 239 && ord ( $charset [2] ) == 187 && ord ( $charset [3] ) == 191) { if ($auto == 1) { $rest = substr ( $contents, 3 ); rewrite ( $filename, $rest ); echo "BOM found, automatically removed."; } else { echo "BOM found."; } } else echo "BOM Not Found."; } ?>
<? @session_start(); if(isset($_POST['username'])&&($_POST['password'])){ $qw=new mysqli('127.0.0.1','root','1234','my_to_do_list',3306); $qw->set_charset('utf8'); if($qw->connect_errno){ echo "problem"; echo exit(); }else{ $password=$_POST['password']; $username=$_POST['username']; $query="select id,username from users where username='".$username."' and password='".sha1($password)."'"; $result=$qw->query($query); if($result->num_rows){ echo '欢迎你'; $row=$result->fetch_assoc(); $_SESSION['userid']=$row['id']; $_SESSION['username']=$row['username']; $result->free(); }else{ echo '不欢迎你'; } $qw->close(); } } if($_SESSION['userid']){ require_once('list.php'); }else{ echo "<form action='index.php' method='post'>"; echo "<label><span>用户名</span><input type='text' name='username'/></label>"; echo "<label><span>密码</span><input type='password' name='password'/></label>"; echo "<input type='submit' value='登入'> "; echo "</form>"; echo "<a href='signup.php'>注册</a>"; } ?>
</html>
signup.php
<!DOCTYPE html>
<html>
<head>
<title>注册</title>
<meta charset="utf-8">
</head>
<?php
if(isset($_POST['username'])&&isset($_POST['password'])&&isset($_POST['passwordd'])&&isset($_POST['email'])){
if($_POST['password']==$_POST['passwordd']){
$qw=new mysqli('127.0.0.1','root','1234','my_to_do_list');
if($qw->connect_errno){
echo 'no';
}else{
$username=$_POST['username'];
$email=$_POST['email'];
$password=$_POST['password'];
$query="INSERT INTO users VALUES(null,'".$username."','".$email."','".sha1($password)."') ";
$result=$qw->query($query);
if($result){
echo '登记成功';
}else{
echo '登记不成功';
}
}
}else{
echo '密码不一致';
}
}else{
echo "<form action='signup.php' method='post'>";
echo "<label><span>用户名</span><input type='text' name='username'/></label>";
echo "<br>";
echo "<label><span>邮箱</span><input type='email' name='email'/></label>";
echo "<br>";
echo "<label><span>密码</span><input type='password' name='password'/></label>";
echo "<br>";
echo "<label><span>重新输入密码</span><input type='password' name='passwordd'/></label>";
echo "<br>";
echo "<input type='submit' value='注册'> ";
echo "</form>";
}
?>
</html>
list.php
<!DOCTYPE html>
<html>
<head>
<title>列表</title>
<meta charset="utf-8">
</head>
<?php @session_start(); $current_user=$_SESSION['username']; $current_userid=$_SESSION['userid']; $query="select *from items where user_id=$current_userid"; $qw=new mysqli('127.0.0.1','root','1234','my_to_do_list',3306); $qw->set_charset('utf8'); if($qw->connect_errno){ echo 'no'; exit(); }else{ $result=$qw->query($query); if($result->num_rows){ echo "<table>"; for($i=1;($row=$result->fetch_assoc());$i++){ echo "<tr>"; echo "<td>$i</td>"; echo "<td>".$row['title']."</td>"; echo "<td>".$row['description']."</td>"; echo "<td>"; if(!$row['complete_by']){ echo '无限期限'; }else{ echo $row['complete_by']; } echo "</td>"; echo "</tr>"; } echo "</table>"; } } ?>