php 登陸註冊php
註冊代碼:register.phpcss
<style type="text/css"> form{ width:300px; background-color:#EEE0E5; margin-left:300px; margin-top:30px; padding:30px; } button{ margin-top:20px; } </style> <form method="post"> <label>用戶名:<input type="text" name="name"></label> <br/><br/> <label>密碼:<input type="password" name="pw"></label> <br/><br/> <label>再次輸入密碼:<input type="password" name="repw"></label> <button type="submit" name="submit">註冊</button> </form> <?php $link = mysqli_connect('localhost', 'root', 'root', 'test'); if (!$link) { die('Could not connect: ' . mysql_error()); }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.phpmysql
<style type="text/css"> form{ width:300px; background-color:#EEE0E5; margin-left:300px; margin-top:30px; padding:30px; } </style> <form method="post"> <label>用戶名:<input type="text" name="name"></label> <br/><br/> <label>密碼:<input type="password" name="pw"></label> <br/><br/> <button type="submit" name="submit">登陸</button> </form> <?php $link = mysqli_connect('localhost', 'root', 'root', 'test'); if (!$link){ echo"<script>alert('數據庫鏈接失敗!')</script>"; }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"); } } } ?> <?php $link = mysqli_connect('localhost', 'root', 'root', 'test'); if (!$link) { die('Could not connect: ' . mysql_error()); }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>"; } } } ?>