使用
PHP實現使用者登入和註冊的功能,相信很多同學都可以寫的出來用
PHP實現登入註冊的功能了,給一些剛入門的
PHP程式員,給你們指路了。
登陸介面 login.php
<form action="logincheck.php" method="post"> 使用者名稱:<input type="text" name="username" /> <br /> 密碼:<input type="password" name="password" /> <br /> <input type="submit" name="submit" value="登陸" /> <a href="register.php">註冊</a> </form>
登入處理介面 logincheck.php
<?php if(isset($_POST["submit"]) && $_POST["submit"] == "登陸") { $user = $_POST["username"]; $psw = $_POST["password"]; if($user == "" || $psw == "") { echo "<script>alert('請輸入使用者名稱或密碼!'); history.go(-1);</script>"; } else { mysql_connect("localhost","root","sixx"); mysql_select_db("vt"); mysql_query("set names 'gbk'"); $sql = "select username,password from user where username = '$_POST[username]' and password = '$_POST[password]'"; $result = mysql_query($sql); $num = mysql_num_rows($result); if($num) { $row = mysql_fetch_array($result); //將資料以索引方式儲存在數組中 echo $row[0]; } else { echo "<script>alert('使用者名稱或密碼不正確!');history.go(-1);</script>"; } } } else { echo "<script>alert('提交未成功!'); history.go(-1);</script>"; } ?>
註冊介面 register.php
<form action="regcheck.php" method="post"> 使用者名稱:<input type="text" name="username"/> <br/> 密 碼:<input type="password" name="password"/> <br/> 確認密碼:<input type="password" name="confirm"/> <br/> <input type="Submit" name="Submit" value="註冊"/> </form>
註冊處理介面 regcheck.php
<?php if(isset($_POST["Submit"]) && $_POST["Submit"] == "註冊") { $user = $_POST["username"]; $psw = $_POST["password"]; $psw_confirm = $_POST["confirm"]; if($user == "" || $psw == "" || $psw_confirm == "") { echo "<script>alert('請確認資訊完整性!'); history.go(-1);</script>"; } else { if($psw == $psw_confirm) { mysql_connect("localhost","root","sixx"); //串連資料庫 mysql_select_db("vt"); //選擇資料庫 mysql_query("set names 'gdk'"); //設定字元集 $sql = "select username from user where username = '$_POST[username]'"; //SQL語句 $result = mysql_query($sql); //執行SQL語句 $num = mysql_num_rows($result); //統計執行結果影響的行數 if($num) //如果已經存在該使用者 { echo "<script>alert('使用者名稱已存在'); history.go(-1);</script>"; } else //不存在當前註冊使用者名稱稱 { $sql_insert = "insert into user (username,password,phone,address) values('$_POST[username]','$_POST[password]','','')"; $res_insert = mysql_query($sql_insert); //$num_insert = mysql_num_rows($res_insert); if($res_insert) { echo "<script>alert('註冊成功!'); history.go(-1);</script>"; } else { echo "<script>alert('系統繁忙,請稍候!'); history.go(-1);</script>"; } } } else { echo "<script>alert('密碼不一致!'); history.go(-1);</script>"; } } } else { echo "<script>alert('提交未成功!'); history.go(-1);</script>"; } ?>