一个简单的注册页面(php+mysql)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <!--Form-->
        <form action="#" method="post">
            <h1>Register</h1>
            <p>First Name:<input type="text" name="fn"></p>
               <p>Last Name:<input type="text" name="ln"></p>
                  <p>Eamil Address:<input type="text" name="email"></p>
                     <p>Password:<input type="text" name="ps1"></p>
                        <p>Confirm Password:<input type="text" name="ps2"></p>
                        <input type="submit" value="Register">
        </form>
        <?php
        // require the connection file
        require('connect.php');
        //if posted
        if($_SERVER['REQUEST_METHOD']=='POST'){
            $errors=array();
     //check first name
            if(empty($_POST['fn'])){
                $errors[]='Please input your first name';
            }
            else{
                $fn=$_POST['fn'];
            }
                 //check last name
            if(empty($_POST['ln'])){
                $errors[]='Please input your last name';
            }
            else{
                $ln=$_POST['ln'];
            }
                          //check email address
            if(empty($_POST['email'])){
                $errors[]='Please input your email address';
            }
            else{
                $email=$_POST['email'];
            }
                                      //check password
            if(empty($_POST['ps1'])){
                $errors[]='Please input your password';
            }
            else{
                $ps1=$_POST['ps1'];
            }
     //confirm password
            if(empty($_POST['ps2'])){
                $errors[]='Please input your password again';
            }
            else{
                if($_POST['ps1']!=$_POST['ps2']){
                    $errors[]='Please confirm your password';
                }
                else{
               $p=$_POST['ps1'];         
                }
            
            }
            //Print errors when they exist
            if(!empty($errors)){
                echo"Error(s):";
                foreach($errors as $msg){
                    echo "<p>$msg</p>";
                }
            }
            else{
            //record the registration information
            $q="Insert into users(first_name,last_name,email,pass,registration_date)"
                    . "values('$fn','$ln','$email',MD5('$ps1'),now())";
            $r=mysqli_query($dbc,$q);
            if($r){
     echo"Registraion successed!";           
            }
            else{
                echo"Failed to register. Please check your information";
            }
        }
        }
        ?>
    </body>
</html>
以下是一个简单PHP+MySQL注册登录页面的示例代码注册页面(register.php): ```php <?php // 如果用户已经登录,则重定向到主页 if (isset($_SESSION['user_id'])) { header('Location: index.php'); exit(); } // 处理用户提交的注册信息 if (isset($_POST['submit'])) { // 连接数据库 $conn = mysqli_connect('localhost', 'username', 'password', 'database_name'); // 检查用户名是否已存在 $username = mysqli_real_escape_string($conn, $_POST['username']); $query = "SELECT * FROM users WHERE username = '$username'"; $result = mysqli_query($conn, $query); if (mysqli_num_rows($result) > 0) { $error_message = '该用户名已被注册!'; } else { // 将新用户插入到数据库中 $password = password_hash($_POST['password'], PASSWORD_DEFAULT); $query = "INSERT INTO users (username, password) VALUES ('$username', '$password')"; mysqli_query($conn, $query); $_SESSION['user_id'] = mysqli_insert_id($conn); header('Location: index.php'); exit(); } } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>注册</title> </head> <body> <h1>注册</h1> <?php if (isset($error_message)): ?> <p><?php echo $error_message; ?></p> <?php endif; ?> <form method="post" action="register.php"> <label for="username">用户名:</label> <input type="text" name="username" id="username" required> <br> <label for="password">密码:</label> <input type="password" name="password" id="password" required> <br> <input type="submit" name="submit" value="注册"> </form> </body> </html> ``` 登录页面(login.php): ```php <?php // 如果用户已经登录,则重定向到主页 if (isset($_SESSION['user_id'])) { header('Location: index.php'); exit(); } // 处理用户提交的登录信息 if (isset($_POST['submit'])) { // 连接数据库 $conn = mysqli_connect('localhost', 'username', 'password', 'database_name'); // 检查用户名和密码是否匹配 $username = mysqli_real_escape_string($conn, $_POST['username']); $query = "SELECT * FROM users WHERE username = '$username'"; $result = mysqli_query($conn, $query); if (mysqli_num_rows($result) > 0) { $user = mysqli_fetch_assoc($result); if (password_verify($_POST['password'], $user['password'])) { $_SESSION['user_id'] = $user['id']; header('Location: index.php'); exit(); } else { $error_message = '密码错误!'; } } else { $error_message = '该用户不存在!'; } } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <h1>登录</h1> <?php if (isset($error_message)): ?> <p><?php echo $error_message; ?></p> <?php endif; ?> <form method="post" action="login.php"> <label for="username">用户名:</label> <input type="text" name="username" id="username" required> <br> <label for="password">密码:</label> <input type="password" name="password" id="password" required> <br> <input type="submit" name="submit" value="登录"> </form> </body> </html> ``` 这个示例代码使用了MySQL数据库来存储用户信息。在实际开发中,应该使用加密的连接和预处理语句来避免SQL注入攻击。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值