这段代码对吗? (使用catpcha的PHP POST方法)

发布于 2024-11-02 21:49:12 字数 3319 浏览 0 评论 0原文

我是一名非常菜鸟的 php 开发人员;我决定创建一个用户登录系统。 对于系统,我想使用验证码系统。我的问题是,当我单击“发送”按钮时,我无法发送表单的数据(两个文本框:用户名和邮件地址)。 为了让它发挥作用;我创建了一个接收表单数据的方法,该数据的发送方式如下: method_name($POST["username"],$POST["mail"]); 我想知道这是否正确,或者会存在一些安全问题或其他问题。

谢谢。

PD:抱歉我的英语……不太好。 这是索引页

    <?php
require_once('recaptchalib.php');
require_once('home.php');

$publickey = "foo";
$privatekey = "bar";

$error = null;

if ($_POST['action'] == "register") {
    $re_ip = $_SERVER["REMOTE_ADDR"];
    $re_challenge = $_POST["recaptcha_challenge_field"];
    $re_response = $_POST["recaptcha_response_field"];

    $resp = recaptcha_check_answer($privatekey, $re_ip, $re_challenge, $re_response);

    if ($resp->is_valid) {
        // procesar registro
            hello($_POST["username"],$_POST["usermail"]);
        exit;
    } else {
        $error = $resp->error;
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>reCAPTCHA Demo</title>
<style type="text/css">

body {
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    font-size: 12px;
    color: #333;
    line-height: 18px;
}
.casilla {
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    font-size: 11px;
    color: #333;
    padding: 2px 4px;
    width: 298px;
    margin-left: 3px;
}
h3 {
    color: #03C;
    font-size: 16px;
}

</style>
<link rel="stylesheet" href="stylesheets/css3buttons.css" media="screen"/>
</head>
<body>
<h3>Registro</h3>
<form method="post">
    <label for="username">Usuario</label><br />
    <input name="username" type="text" class="casilla" id="username" /><br />
    <label for="usermail">Email</label><br />
    <input name="usermail" type="text" class="casilla" id="usermail" /><br />
    <label for="usercheck">Verificaci&oacute;n</label><br />
    <?php echo recaptcha_get_html($publickey, $error); ?>
    <input type="hidden" name="action" value="register" />
    <button type="submit" name="btsend" value="Enviar">Iniciar Sesión</button>
</form>
</body>
</html>
**and this is other page**

    <!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        function hello($var1,$var2)
        {
            $mailFilter = "^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$";
            echo"HELLO WORLD\n".$var1."---".$var2;
            if (preg_match($mailFilter, $var2)) {
            echo "A match was found.";
            } else {
                echo "correo valido.";
            }
        }
        ?>
    </body>
</html>

I am a very rookie php developer; and i decided create a user Login system.
For the system, i want use captcha system. My problem is that when i click the button "Send" i can't send the data of the form (two textbox: username and mail address).
For make it work; i created a method that recive the data of the form, this data is send like: method_name($POST["username"],$POST["mail"]);
i want to know if this is correct or it will have some security problem or maybe something else.

thanks.

pd: sorry for my english...is not good.
this is the index page

    <?php
require_once('recaptchalib.php');
require_once('home.php');

$publickey = "foo";
$privatekey = "bar";

$error = null;

if ($_POST['action'] == "register") {
    $re_ip = $_SERVER["REMOTE_ADDR"];
    $re_challenge = $_POST["recaptcha_challenge_field"];
    $re_response = $_POST["recaptcha_response_field"];

    $resp = recaptcha_check_answer($privatekey, $re_ip, $re_challenge, $re_response);

    if ($resp->is_valid) {
        // procesar registro
            hello($_POST["username"],$_POST["usermail"]);
        exit;
    } else {
        $error = $resp->error;
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>reCAPTCHA Demo</title>
<style type="text/css">

body {
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    font-size: 12px;
    color: #333;
    line-height: 18px;
}
.casilla {
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    font-size: 11px;
    color: #333;
    padding: 2px 4px;
    width: 298px;
    margin-left: 3px;
}
h3 {
    color: #03C;
    font-size: 16px;
}

</style>
<link rel="stylesheet" href="stylesheets/css3buttons.css" media="screen"/>
</head>
<body>
<h3>Registro</h3>
<form method="post">
    <label for="username">Usuario</label><br />
    <input name="username" type="text" class="casilla" id="username" /><br />
    <label for="usermail">Email</label><br />
    <input name="usermail" type="text" class="casilla" id="usermail" /><br />
    <label for="usercheck">Verificación</label><br />
    <?php echo recaptcha_get_html($publickey, $error); ?>
    <input type="hidden" name="action" value="register" />
    <button type="submit" name="btsend" value="Enviar">Iniciar Sesión</button>
</form>
</body>
</html>
**and this is other page**

    <!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        function hello($var1,$var2)
        {
            $mailFilter = "^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$";
            echo"HELLO WORLD\n".$var1."---".$var2;
            if (preg_match($mailFilter, $var2)) {
            echo "A match was found.";
            } else {
                echo "correo valido.";
            }
        }
        ?>
    </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光与爱终年不遇 2024-11-09 21:49:12

如果无法发送表单,那么我会假设是因为表单声明缺少 action= URL。通常你会写:

 <form method="POST" action="index.php">

第二件事是电子邮件正则表达式。它错过了典型的有效字符,但也没有使用正确的 PCRE 分隔符。而是使用它而不是手动 preg_match $mailFilter

 if (filter_var($var2, FILTER_VALIDATE_EMAIL)) {

If the form cannot be sent, then I would assume because the form declaration lacks the action= URL. Normally you would write:

 <form method="POST" action="index.php">

The second thing is the email regex. It misses the typical valid characters, but also doesn't use correct PCRE delimiters. Rather use this instead of a manual preg_match $mailFilter:

 if (filter_var($var2, FILTER_VALIDATE_EMAIL)) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文