无法使用 include 命令从另一个 PHP 文件中检索 PHP 值


Unable to retrieve a PHP value from another PHP file using the include command

所以,是的,我是一个新手,目前我花了整个上午的时间试图找到一种方法将一个 php 文件的值获取到另一个文件中。

更具体地说,我正在尝试使用mt_rand命令创建一个简单的验证码系统。例如,我在第一个 php 文件中放置了以下代码:形式.php

<?php
$captcha = mt_rand(10000,99999);?>

并将表单.php包含在另一个.php文件中,该文件旨在成为我的表单页面中的图像。换句话说,$captcha变量需要在form.php文件中使用,以便图像中的代码与表单中要检查的代码相同.php使用if语句:image.php (image.php 是使用 imagepng(( 和其他命令的 IMAGE(

<?php    
include('form.php');?>

所以,整个故事是关于:$captcha包括一个从 10000-99999 的随机数,并且在表单提交后在表单中检查它.php但它也包含在图像中(必须*包含(.php以生成包含代码的图像。

即使使用 include(( 命令对我不起作用。

我愿意在PHP中获得解决方案,但当然也欢迎任何其他人。

注意:想指出的是,我尝试只在图像中生成一个随机数.php看看它是否可以生成并且工作正常,但是当我使用 include 命令时它无法检索值。

PS:我想我也输入了太多的狗屎(太大的文字等(,那是因为我现在真的很困惑。

因此,如果有人有时间彻底检查这两个PHP文件,它们是:

形式.php

       <?php 
    $captcha = mt_rand(10000,99999);

    if(isset($_POST['submit'])){
        if($_POST['response']=$captcha){
            echo "Captcha verified successfully.";
        }else{
            echo "Wrong Captcha Input!!!</br>";
        }
        }
    ?>
    <!DOCTYPE html>
    <head>
    <title>Report Cheater</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
    <form action="" method="post"></textarea><br>
    Captcha(*): <img src="image.php">
    <br><input type="text" name="response"><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html> 

图片.php

<?php
//CAPTCHA verification
header ('Content-Type: image/png');
//Image to be converted into captcha
$im = imagecreatetruecolor(180, 40);
//Captch Background
$im2 = 'capback.png';
//Creates an instance of the captcha background to be added to the captcha
$rsc = imagecreatefrompng($im2);
// Copy and merge
imagecopymerge($im, $rsc, 0, 0, 0, 0, 180, 40, 100);

//Colors to be used
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
//The captcha random number taken directly from report.php
include("form.php"); //works with $captcha = mt_rand(10000,99999); but not with the include("form.php");
// Font
$font = 'arial.ttf';
$x1 = mt_rand(29,41);
$y1 = mt_rand(42,58);
$x2 = $x1 + 1;
$y2 = $y1 + 1;
// Add some shadow to the text
imagettftext($im, 20, 0, $y2, $x2, $grey, $font, $captcha);
// Add the text
imagettftext($im, 20, 0, $y1, $x1, $black, $font, $captcha);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
imagedestroy($rsc);
?>

将两个变量与==进行比较所以改变

 if($_POST['response']=$captcha){

 if($_POST['response']==$captcha){

首先,您必须了解capatcha系统的工作原理

当您生成要在 Capatcha 中使用的哈希或随机数时您必须将其保存在用户会话中,然后在图像中使用它并将其与用户输入匹配。

在您的代码中,您生成两个不同的数字,一个在图像中,然后在检查用户输入时生成另一个数字,这是逻辑错误。

在图像中.php :

生成随机数,您必须以 session_start(( 开始文件;

$capatcha=random(0,1000);
$_SESSION["capatcha"]=$capatcha;

然后使用 $_SESSION["capatcha"] 生成图像,然后使用它来验证用户输入

  if(isset($_POST['submit'])){
        if($_POST['response']==$_SESSION["capatcha"]){
            echo "Captcha verified successfully.";
        }else{
            echo "Wrong Captcha Input!!!</br>";
        }
        }