php会员注册登入系统开发代码
因為全棋同學的系統需求,雖然會員機制並不難,他一定也很快就學會了,而我還是寫此教學來引導他,幫助他系統擴展的更快,相信他的系統在未來也一定會很有用的! 而這個教學主要是說明session應用在會員機制上,而教學的程式碼還是有些地方有沒有考慮的很周詳,安全上還是會有瑕疵,希望大家見諒。 以下為資料表的欄位資料: 以下為會員系統示意圖: (1) 首頁 - 登入頁面 (index.php) <!- 設定網頁編碼為UTF-8 -> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <form name="form" method="post" action="connect.php"> 帳號:<input type="text" name="id" /> <br> 密碼:<input type="password" name="pw" /> <br> <input type="submit" name="button" value="登入" /> <a href="register.php">申請帳號</a> </form> (2) php連結MySQL資料庫語法(mysql_connect.inc.php) <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php /資料庫設定 /資料庫位置 $db_server = "localhost" /資料庫名稱 $db_name = "mydb" /資料庫管理者帳號 $db_user = "root" /資料庫管理者密碼 $db_passwd = "1234" /對資料庫連線 if(!mysql_connect($db_server, $db_user, $db_passwd) die("無法對資料庫連線"); /資料庫連線採UTF8 mysql_query("SET NAMES utf8"); /選擇資料庫 if(!mysql_select_db($db_name) die("無法使用資料庫"); ?> (3) 會員ID、PW與MySQL資料庫作認證(connect.php) <?php session_start(); ?> <!-上方語法為啟用session,此語法要放在網頁最前方-> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php /連接資料庫 /只要此頁面上有用到連接MySQL就要include它 include("mysql_connect.inc.php"); $id = $_POST'id' $pw = $_POST'pw' /搜尋資料庫資料 $sql = "SELECT * FROM member_table where username = '$id'" $result = mysql_query($sql); $row = mysql_fetch_row($result); /判斷帳號與密碼是否為空白 /以及MySQL資料庫裡是否有這個會員 if($id != null && $pw != null && $row1 = $id && $row2 = $pw) /將帳號寫入session,方便驗證使用者身份 $_SESSION'username' = $id; echo '登入成功!' echo '<meta http-equiv=REFRESH CONTENT=1;url=member.php>' else echo '登入失敗!' echo '<meta http-equiv=REFRESH CONTENT=1;url=index.php>' ?> (4) 會員登入成功後 頁面 - 此頁面有新增、修改、刪除與登出的連結 並且會顯示出所有會員資料(member.php) <?php session_start(); ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php include("mysql_connect.inc.php"); echo '<a href="logout.php">退出</a> <br><br>' /此判斷為判定觀看此頁有沒有權限 /說不定是路人或不相關的使用者 /因此要給予排除 if($_SESSION'username' != null) echo '<a href="register.php">新增</a> ' echo '<a href="update.php">修改</a> ' echo '<a href="delete.php">刪除</a> <br><br>' /將資料庫裡的所有會員資料顯示在畫面上 $sql = "SELECT * FROM member_table" $result = mysql_query($sql); while($row = mysql_fetch_row($result) echo "$row0 - 名字(帳號):$row1, " . "電話:$row3, 地址:$row4, 備註:$row5<br>" else echo '您無權限觀看此頁面!' echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>' ?> (5) 登出 - 洗掉登入使用者之session(logout.php) <?php session_start(); ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php /將session清空 unset($_SESSION'username'); echo '登出中.' echo '<meta http-equiv=REFRESH CONTENT=1;url=index.php>' ?> (6) 加入(註冊)會員 - 填寫會員資料 (register.php) <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <form name="form" method="post" action="register_finish.php"> 帳號:<input type="text" name="id" /> <br> 密碼:<input type="password" name="pw" /> <br> 再一次輸入密碼:<input type="password" name="pw2" /> <br> 電話:<input type="text" name="telephone" /> <br> 地址:<input type="text" name="address" /> <br> 備註:<textarea name="other" cols="45" rows="5"></textarea> <br> <input type="submit" name="button" value="確定" /> </form> (7) 加入(註冊)會員 - 新增會員資料進MySQL資料庫 (register_finish.php) <?php session_start(); ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php include("mysql_connect.inc.php"); $id = $_POST'id' $pw = $_POST'pw' $pw2 = $_POST'pw2' $telephone = $_POST'telephone' $address = $_POST'address' $other = $_POST'other' /判斷帳號密碼是否為空值 /確認密碼輸入的正確性 if($id != null && $pw != null && $pw2 != null && $pw = $pw2) /新增資料進資料庫語法 $sql = "insert into member_table (username, password, telephone, address, other) values ('$id', '$pw', '$telephone', '$address', '$other')" if(mysql_query($sql) echo '新增成功!' echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>' else echo '新增失敗!' echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>' else echo '您無權限觀看此頁面!' echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>' ?> (8) 修改會員資料 - 填寫要修改之會員資料(update.php) <?php session_start(); ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php include("mysql_connect.inc.php"); if($_SESSION'username' != null) /將$_SESSION'username'丟給$id