如何为不同的页面分配单独的会话?

How can I devote separated session for different pages?

我有以下生成验证码的脚本:

// captcha.php
session_start();
$captcha_token='';
for($i=1; $i<=5; $i++){$captcha_token .= rand(0,9).' ';}
$_SESSION['captcha'] = str_replace(" ","",$captcha_token);

$im = imagecreatetruecolor(110, 34);
$red = imagecolorallocate($im, 245, 245, 245);
imagefill($im, 0, 0, $red);
$text_color = imagecolorallocate($im, 80, 80, 80);
imagestring($im, 8, 15, 9, $captcha_token, $text_color);
header('Content-Type: image/jpeg');
imagejpeg($im);
imagedestroy($im);

我还有多个页面需要验证码。我对所有这些都使用 captcha.php。像这样:

// contact.php
<input name="captcha" type="text" />
<img  src="captcha.php" />

// resend_password.php
<input name="captcha" type="text" />
<img  src="captcha.php" />

// multiple_wrong_login.php
<input name="captcha" type="text" />
<img  src="captcha.php" />

一切顺利。正如您所见,所有脚本只有一个会话 $_SESSION['captcha']。当我打开 contact.php 并紧接着打开 resend_password.php 时,contact.php 中的 captcha 将无效。我该如何解决?我的意思是如何为不同的页面制作一个单独的验证码?

这是我的建议:

像这样将变量解析为验证码脚本:

<img  src="captcha.php?page=resend_password" />

然后在脚本中:

$_SESSION['captcha'][$_GET['page']] = str_replace(" ","",$captcha_token);