PhP登录/注册系统

时间:2012-03-23 13:52:06

标签: php login system

我找到了关于使用PhP和MySQL创建登录/注册系统的好教程。 论坛大约有5年的历史(去年编辑过),但它仍然有用。

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){

因此,当按下Register按钮时,它会运行php代码,但此编辑似乎也不起作用。那么问题是什么呢?如果需要,我可以在我的域

上重新添加此代码

登录页面有同样的问题,在清空字段旁按下按钮时没有任何反应。

3 个答案:

答案 0 :(得分:1)

您是否回显了$ _REQUEST数据并检查它们是否被正确抓取?

答案 1 :(得分:1)

<?php
    if (!isset($_POST))
        register_form();
    else
        register();

使用上面的代码更改开关部件。

答案 2 :(得分:0)

没关系,我发现了一个不同的视频演示教程。奇迹般有效。 My Page 添加了登录/注册系统。

Tutorial如果有人需要它。感谢您的回答,我很感激,并会给他们+1。