Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用$\u会话php在页面之间传递变量时出现问题_Php_Session - Fatal编程技术网

使用$\u会话php在页面之间传递变量时出现问题

使用$\u会话php在页面之间传递变量时出现问题,php,session,Php,Session,我正在为学校做一项作业,在那里我们将制作两个php文件。第一个文件调用会话,生成一个随机的5字符字符串,并将该字符串保存到会话数组中。第二个脚本生成一个图像,并从第一个文件中获取字符串,并将其合并到图像上以生成验证码 我在将值传递给第二个脚本时遇到问题。会话变量“captcha_string”在第一个脚本中完全可见,但不会传递到第二个页面。我对这一点是全新的,而且很沮丧。我的理解是,只要启动会话,整个$\u会话数组就应该可用。当我运行第一个脚本时,我得到的是一个破损的图像标签,而不是我所希望的验

我正在为学校做一项作业,在那里我们将制作两个php文件。第一个文件调用会话,生成一个随机的5字符字符串,并将该字符串保存到会话数组中。第二个脚本生成一个图像,并从第一个文件中获取字符串,并将其合并到图像上以生成验证码

我在将值传递给第二个脚本时遇到问题。会话变量“captcha_string”在第一个脚本中完全可见,但不会传递到第二个页面。我对这一点是全新的,而且很沮丧。我的理解是,只要启动会话,整个$\u会话数组就应该可用。当我运行第一个脚本时,我得到的是一个破损的图像标签,而不是我所希望的验证码。希望这能解决我的问题

这是我为第一个文件所做的:

    <?php
   session_start();
   $possible_chars = array_merge(range('A','Z'),range('0','9'));
   shuffle($possible_chars); 
   $string = substr(implode($possible_chars),0,5);
   $_SESSION['captcha_string']=$string;

?>

<img src="captcha_generator.php" alt="Weinerdog!" />

这是第二个文件中的一位,我试图获取$string(captcha_string),它被命名为“captcha_generator.php:

<?php
session_start();

putenv('GDFONTPATH=' . realpath('.')); 
header("Content-type: image/png");

//import string for the captcha from $_SESSION
$string = $_SESSION['captcha_string'];

// Build an image resource using an existing image as a starting point.
$backgroundimage = "captcha_wiener.jpg"; 
$im=imagecreatefromjpeg($backgroundimage); 
$colour = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));

// Output the string of characters using a true type font. 
// Above we set the font path to the current directory, this
// means that arial.ttf font file must be in this directory.
$font = 'arial.ttf'; 
$angle = rand(-5,5); 
imagettftext($im, 120, $angle, 50, 250, $colour, $font, $string);

// Draw some annoying lines across the image. 
imagesetthickness($im, 10); 
for ($i = 0; $i <3; $i++) { 
    imageline($im, rand(100,50), rand(150,200), rand(450,550), rand(200,250), $colour);
} 

// Output the image as a PNG and the free the used memory. 
imagejpeg($im); 
imagedestroy($im); 

?>


当然,严格来说,这是一个练习,以确保我们可以使用会话传递值。生成captcha的其余代码没有问题,它已经过测试并且可以正常工作。

您在回显一些值时,将内容类型设置为image/png,因此要么您已经发送了标题错误,要么,如果没有发送文本,则返回t(因为由PHP缓存),您将有一个损坏的图像,并且无法看到文本


别担心,包括我在内的每个人都遇到过这种情况:-)

“我有一个问题”无论如何都不是一个合理的问题描述。你有什么问题?到底是什么问题?验证码字符串在生成器脚本中是空的?你用image/png(image)的内容类型回显一些值(文本)。你不能把两者混为一谈。明白了,我只是想知道在$\u会议上是否通过了什么。谢谢,现在一切似乎都正常了。把它写在答案里,我会投票的。现在我觉得自己像个十足的傻瓜。。。。。