首页 开发 Typecho WordPress

废话不多说,代码为:

< form action="<?php $this->options->loginAction()?>" method="post" name="login" rold="form">
    < input type="hidden" name="referer" value="<?php $this->options->siteUrl(); ?>">
    < input type="text" name="name" autocomplete="username" placeholder="请输入用户名" required/>
    < input type="password" name="password" autocomplete="current-password" placeholder="请输入密码" required/>
    < button type="submit">登录< /button >
< /form >
< form action="" method="post" name="register" role="form">
    < input type="text" name="name" placeholder="用户名(必填)" required />
    < input type="email" name="mail" placeholder="邮箱地址(必填)" required />
    < input type="password" name="password" placeholder="密码(必填)" required />
    < button type="submit" name="doRegister">注册< /button >
< /form >
    // 处理注册请求
    if (isset($_POST['doRegister'])) {
        $db = Typecho_Db::get();
        $prefix = $db->getPrefix();
        
        // 获取输入数据
        $name = htmlspecialchars(trim($_POST['name']));
        $mail = htmlspecialchars(trim($_POST['mail']));
        $password = htmlspecialchars(trim($_POST['password']));

        // 检查数据有效性
        if (empty($name) || empty($mail) || empty($password)) {
            echo '<p style="color: red;">请填写所有必填项。</p>';
        } elseif (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
            echo '<p style="color: red;">请输入有效的邮箱地址。</p>';
        } else {
            // 检查用户名是否已存在
            $exists = $db->fetchRow($db->select()->from($prefix . 'users')->where('name = ?', $name)->limit(1));
            if ($exists) {
                echo '<p style="color: red;">用户名已存在。</p>';
            } else {
                // 创建新用户
                $hashedPassword = Typecho_Common::hash($password);
                $db->query($db->insert($prefix . 'users')->rows([
                    'name' => $name,
                    'mail' => $mail,
                    'screenName' => $name,
                    'password' => $hashedPassword,
                    'created' => time(),
                    'group' => 'subscriber', // 默认注册用户组
                    'authCode' => Typecho_Common::randString(32)
                ]));
                echo '<p style="color: green;">注册成功!您现在可以登录。</p>';
            }
        }
    }

感谢支持

© 2025 鼠子Blog 版权所有鲁ICP备2023052135号-3 翻译