一、建立数据库。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | MariaDB [(none)]> CREATE DATABASE test; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> USE test; Database changed MariaDB [test]> CREATE TABLE user ( -> id int(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, -> name VARCHAR(24) NOT NULL, -> pw VARCHAR(24) NOT NULL -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.52 sec) MariaDB [test]> desc user; +-------+-----------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------+------+-----+---------+----------------+ | id | int(6) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(24) | NO | | NULL | | | pw | varchar(24) | NO | | NULL | | +-------+-----------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec) |
二、建立注册页面 register.php。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <style type="text/css"> div{ width: 350px; height: 250px; margin: 0 auto; text-align: center; } form{ width: 300px; background-color: #EEE0E5; padding: 30px; } button{ margin-top: 20px; } </style> <body> <div> <form action="" method="post"> <lable>username:<input type="text" name="name"/></lable> <br/> <br/> <lable>password:<input type="password" name="pw" id=""/></lable> <br/> <br/> <lable>password:<input type="password" name="repw" id=""/></lable> <br/> <br/> <button type="submit" name="submit">register</button> </form> </div> </body> <?php $link = mysqli_connect('localhost', 'root', '123456', 'test'); if (!$link) { die('Could not connect: ' . mysqli_error($link)); }else { if (isset($_POST['submit'])) { if ($_POST['pw'] == $_POST['repw']) { $query = "insert into user (name,pw) values('{$_POST['name']}','{$_POST['pw']}')"; $result = mysqli_query($link, $query); header("Location:login.php"); }else { echo "<script>alert('两次输入的密码不一致。')</script>"; } } } ?> |
三、建立登录页面 login.php。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <style type="text/css"> div{ width: 350px; height: 250px; margin: 0 auto; text-align: center; } form{ width: 300px; background-color: #EEE0E5; padding: 30px; } </style> <body> <div> <form action="" method="post"> <label for="">username:<input type="text" name="name" id=""/></label> <br/> <br/> <label for="">password:<input type="text" name="pw" id=""/></label> <br/> <br/> <button type="submit" name="submit">login</button> </form> </div> </body> <?php $link = mysqli_connect('localhost', 'root', '123456', 'test'); if (!$link) { die('Could not connect: ' . mysqli_error($link)); }else { if (isset($_POST['submit'])) { $query = "select * from user where name = '{$_POST['name']}' and pw = '{$_POST['pw']}'"; $result = mysqli_query($link, $query); if (mysqli_num_rows($result) == 1){ header("Location:index.php"); }else { echo "<script>alert('你的用户名或者密码不正确。')</script>"; } } } ?> |
四、建立主页面 index.php。
1 2 3 | <?php echo "login success."; ?> |