我发现了这个关于使用PhP和MySQL创建登录/注册系统的好教程。该论坛已有5年历史(去年编辑),但仍然可以使用。
初学者简单注册登录系统
登录和注册页面似乎都有问题。
<?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>
我用if($register){
代替了function register(){
因此,当按下Register按钮时,它会运行php代码,但这种编辑似乎也不起作用。那么问题出在哪里呢?如果需要,我可以在我的域上重新添加此代码
登录页面也有同样的问题,当按下清空字段旁边的按钮时,什么都不会发生。
是否回显$_REQUEST数据并检查它们是否被正确获取?
<?php
if (!isset($_POST))
register_form();
else
register();
使用上面的代码更换开关零件。
伙计们,我发现了一个不同的视频演示教程。工作起来很有魅力。我的页面添加了登录/注册系统。
教程,如果有人需要的话。谢谢你的回答,我很感激,并将+1他们。