我發現這個使用PhP和MySQL創建登錄/註冊系統的好教程。 論壇大約5歲(去年編輯),但它仍然可以是有用的。PhP登錄/註冊系統
Beginner Simple Register-Login system
似乎能夠同時與登錄的問題和註冊頁面。
<?php
function register_form(){
$date = date('D, M, Y');
echo "<form action='?act=register' method='post'>"
."Username: <input type='text' name='username' size='30'><br>"
."Password: <input type='password' name='password' size='30'><br>"
."Confirm your password: <input type='password' name='password_conf' size='30'><br>"
."Email: <input type='text' name='email' size='30'><br>"
."<input type='hidden' name='date' value='$date'>"
."<input type='submit' value='Register'>"
."</form>";
}
function register(){
$connect = mysql_connect("host", "username", "password");
if(!$connect){
die(mysql_error());
}
$select_db = mysql_select_db("database", $connect);
if(!$select_db){
die(mysql_error());
}
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$pass_conf = $_REQUEST['password_conf'];
$email = $_REQUEST['email'];
$date = $_REQUEST['date'];
if(empty($username)){
die("Please enter your username!<br>");
}
if(empty($password)){
die("Please enter your password!<br>");
}
if(empty($pass_conf)){
die("Please confirm your password!<br>");
}
if(empty($email)){
die("Please enter your email!");
}
$user_check = mysql_query("SELECT username FROM users WHERE username='$username'");
$do_user_check = mysql_num_rows($user_check);
$email_check = mysql_query("SELECT email FROM users WHERE email='$email'");
$do_email_check = mysql_num_rows($email_check);
if($do_user_check > 0){
die("Username is already in use!<br>");
}
if($do_email_check > 0){
die("Email is already in use!");
}
if($password != $pass_conf){
die("Passwords don't match!");
}
$insert = mysql_query("INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')");
if(!$insert){
die("There's little problem: ".mysql_error());
}
echo $username.", you are now registered. Thank you!<br><a href=login.php>Login</a> | <a href=index.php>Index</a>";
}
switch($act){
default;
register_form();
break;
case "register";
register();
break;
}
?>
一旦按下注冊按鈕的頁面不執行任何操作,字段被刪除,並沒有數據被授予數據庫或錯誤在裏面加了。 我因子評分那個,所以我刪除它的問題可能是switch($act){
部分,並使用改變了頁面require
require('connect.php');
其中connect.php是
<?php
mysql_connect("localhost","host","password");
mysql_select_db("database");
?>
刪除了function register_form(){
和echo
部分把它變成一個HTML代碼:
<form action='register' method='post'>
Username: <input type='text' name='username' size='30'><br>
Password: <input type='password' name='password' size='30'><br>
Confirm your password: <input type='password' name='password_conf' size='30'><br>
Email: <input type='text' name='email' size='30'><br>
<input type='hidden' name='date' value='$date'>
<input type='submit' name="register" value='Register'>
</form>
而不是有一個function register(){
我用if($register){
替換它所以當按下注冊按鈕時,它運行的是PHP代碼,但這個編輯似乎也不工作。那麼問題是什麼?如果需要,我可以在我的域上重新添加此代碼。
登錄頁面具有相同的問題,當清空字段時按下按鈕時沒有任何反應。
不應該表單動作是register.php而不是隻是註冊?
注意任何尋找登錄系統的人:發佈的代碼有很多問題;不要打擾它。 – outis 2014-04-01 13:21:57