Php 会话回声&x27;s上一个变量
我正在制作一个带有验证码的表单,我正在将代码存储在一个隐藏的输入框中,但是输入框存储了以前的会话代码。我猜这是要和明星们一起做的事情,我摆弄了摆弄,但不幸的是没有用 有人能给我指一下正确的方向吗 PHP文件:Php 会话回声&x27;s上一个变量,php,Php,我正在制作一个带有验证码的表单,我正在将代码存储在一个隐藏的输入框中,但是输入框存储了以前的会话代码。我猜这是要和明星们一起做的事情,我摆弄了摆弄,但不幸的是没有用 有人能给我指一下正确的方向吗 PHP文件: <body> <?php session_start(); ?> ... <div id="reg-field"> <h1>Human Verification</h1>
<body>
<?php
session_start();
?>
...
<div id="reg-field">
<h1>Human Verification</h1>
<div id="captions">
<img src="PHP/captcha.php" style="float: right;" class="cnr-all" />
</div>
<form name="Form3" method="post">
<input type="text" id="captcha-bar" class="cnr-all" name="answer" placeholder="Enter Code Here" />
<input type="hidden" name="answer2" value="<?php echo $_SESSION['captcha']; ?>"/>
<input type="button" id="captcha-reload" class="cnr-all" onclick="window.location.href = window.location.href">
<span id="captchaConfirm" class="confirmMessage" style="margin: 12.5px 0px"></span>
</form>
</div>
...
</body>
...
人类验证
会话\u start()
必须在任何输出之前。把它作为第一行代码是最安全的。前面有一个
标记,因此它将失败。在将任何HTML发送到客户端之前,将执行第一个文件的PHP
含义:会话中的代码将在客户端请求captcha.php生成新的captcha代码之前输出
您可以通过在第一个文件中生成验证码来解决此问题。
或者更好的是,不要将其打印出来(隐藏的输入是一个安全问题),只需将用户的答案与$\u会话中的任何内容进行比较即可。
这样你就可以刷新验证码,直到用户提交代码为止。然后将POST['captcha']与会话['captcha']进行比较
此外:
文件1:
<?php
session_start();
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < 5; ++$i)
{
$randomString .= $chars[rand(0, strlen($chars)-1)];
}
$_SESSION['captcha'] = strtolower( $randomString );
?>
<body>
...
<div id="reg-field">
<h1>Human Verification</h1>
<div id="captions">
<img src="PHP/captcha.php" style="float: right;" class="cnr-all" />
</div>
<form name="Form3" method="post">
<input type="text" id="captcha-bar" class="cnr-all" name="answer" placeholder="Enter Code Here" />
<input type="hidden" name="answer2" value="<?php echo $_SESSION['captcha']; ?>"/>
<input type="button" id="captcha-reload" class="cnr-all" onclick="window.location.href = window.location.href">
<span id="captchaConfirm" class="confirmMessage" style="margin: 12.5px 0px"></span>
</form>
</div>
...
</body>
...
人类验证
复制页面顶部的PHP代码并删除session_start at Body标记…我正在使用Javascript验证并比较这两个字段,这样是否仍然可行?最好通过AJAX调用与服务器比较验证码。将用户输入发送到服务器,让PHP进行比较,向客户端发送响应,最后根据PHP的结果向用户提供反馈。我必须使用JS(不要问),据我所知,这是最安全的选择(我知道安全风险),虽然对于人工验证来说应该没问题?在这种情况下,请按照答案的第一部分:在正常页面的顶部生成验证码,而不是在渲染图像时。我已将脚本放在顶部,如何在页面的下一步进行回显?(对这样一个努比·奎斯蒂诺感到抱歉)
<?php
session_start();
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < 5; ++$i)
{
$randomString .= $chars[rand(0, strlen($chars)-1)];
}
$_SESSION['captcha'] = strtolower( $randomString );
?>
<body>
...
<div id="reg-field">
<h1>Human Verification</h1>
<div id="captions">
<img src="PHP/captcha.php" style="float: right;" class="cnr-all" />
</div>
<form name="Form3" method="post">
<input type="text" id="captcha-bar" class="cnr-all" name="answer" placeholder="Enter Code Here" />
<input type="hidden" name="answer2" value="<?php echo $_SESSION['captcha']; ?>"/>
<input type="button" id="captcha-reload" class="cnr-all" onclick="window.location.href = window.location.href">
<span id="captchaConfirm" class="confirmMessage" style="margin: 12.5px 0px"></span>
</form>
</div>
...
</body>
<?php
session_start();
header("Expires: Tue, 01 Jan 2013 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$im = @imagecreatefrompng("captcha_bg.png");
imagettftext($im, 30, 0, 10, 38, imagecolorallocate ($im, 0, 0, 0), 'larabiefont.ttf', $_SESSION['captcha']);
header ('Content-type: image/png');
imagepng($im, NULL, 0);
imagedestroy($im);
?>