2013-12-18 54 views
1

我正在创建一个带有验证码的表单,并且我将这些代码存储在隐藏的输入框中,但输入框存储以前的会话代码。我正在猜测它与session_stars有什么共同之处,我一直在摆弄它,但无济于事。会话回显的前一个变量

请问有人能指点我正确的方向!

php文件:

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

验证码PHP创建者:

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

$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
$randomString = ''; 

for ($i = 0; $i < 5; $i++) 
{ 
    $randomString .= $chars[rand(0, strlen($chars)-1)]; 
} 

$_SESSION['captcha'] = strtolower($randomString); 


$im = @imagecreatefrompng("captcha_bg.png"); 


imagettftext($im, 30, 0, 10, 38, imagecolorallocate ($im, 0, 0, 0), 'larabiefont.ttf', $randomString); 

header ('Content-type: image/png'); 
imagepng($im, NULL, 0); 
imagedestroy($im); 

?> 

回答

1

第一个文件的PHP将在任何HTML发送到客户端之前执行。

含义:来自SESSION的代码将在客户端请求captcha.php之前输出,生成一个新的验证码。

您可以通过在第一个文件中生成验证码来解决此问题。 甚至更​​好,不要将它打印出来HTML(隐藏输入是一个安全问题),只是比较用户的答案,无论你在$ _SESSION。 这样你就可以刷新captcha的所有你喜欢的,直到用户提交它的代码。你那么POST [ '验证码']比作SESSION [ '验证码']

另外:http://www.w3.org/TR/turingtest/

文件1:

<?php 
    session_start(); 
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    $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> 

文件2:

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

?> 
+0

我使用Javascript验证和比较两个字段,它仍然有可能这样吗? – horHAY

+0

更好地通过与服务器的AJAX呼叫比较验证码。将用户输入发送到服务器,PHP会对其进行比较,向客户端发送响应,并根据PHP的结果最终向用户提供反馈。 –

+0

我必须使用JS(不要问),据我所知这是最安全的选择(我知道安全风险),虽然对于人类验证它应该没问题? – horHAY

1

session_start()必须在任何输出之前。把它作为第一行代码是最安全的。您之前有一个<body>标签,因此它会失败。

+0

复制在页面顶部的PHP代码,并删除正文标签session_start ... – Kishore