dqm83011 2012-03-23 13:52
浏览 320
已采纳

PhP登录/注册系统

I found this good tutorial on creating a login/register system using PhP and MySQL. The forum is around 5 years old (edited last year) but it can still be usefull.

Beginner Simple Register-Login system

There seems to be an issue with both login and register pages.

<?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; 

} 

?>

Once pressed the register button the page does nothing, fields are erased and no data is added inside the database or error given. I tought that the problem might be the switch($act){ part so I removed it and changed the page using a require

require('connect.php');

where connect.php is

<?php
mysql_connect("localhost","host","password");
mysql_select_db("database");
?>

Removed the function register_form(){ and echo part turning it into an HTML code:

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

And instead of having a function register(){ I replaced it with a if($register){

So when the Register button is pressed it runs the php code, but this edit doesn't seem to work either. So what can the problem be? If needed I can re-add this code on my Domain

The login page has the same issue, nothing happens when the button is pressed beside emptying the fields.

  • 写回答

3条回答 默认 最新

  • douche5961 2012-03-23 22:09
    关注

    Nevermind guys I found a different tutorial with video demonstration. Works like a charm. My Page Added the Login/Register system.

    Tutorial if anyone needs it. Thanks for answering tho I appreciate it and will +1 them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c