-2

我想设置 $choice_1 和 $choice_2 但每个会话只设置一次。我正在使用 $choice_1 和 $choice_2 创建图像名称,并且我想避免在会话中绘制不同的 jpeg。我在http://communitychessclub.com/rabren展示了两个棋子,我希望这两个图像用于整个用户会话。

<?php 
if (!isset($_SESSION['choice_1'])) {$chessmen = array("N","Q","R","B","K","P"); 

$random_piece = array_rand($chessmen); 
$choice_1 = $chessmen[$random_piece];
unset($chessmen[$random_piece]);

$random_piece = array_rand($chessmen); 
$choice_2 = $chessmen[$random_piece];} 
?>
4

2 回答 2

0

在打印任何输出之前使用 session_start() 或在 php.ini 中将会话自动启动设置为 1

<?php
session_start();
if(!isset($_SESSION['test']))
echo "no test session";
else
echo $_SESSION['test'];
if($sth)
{
 $_SESSION['test'] = 'sth...';
}
于 2012-11-04T18:32:28.510 回答
0

试试这个方法...

<?php 
session_start();

if(!isset($_SESSION['choice_1'])) {
    $chessmen = array("N","Q","R","B","K","P"); 

    $random_piece = array_rand($chessmen); 
    $_SESSION['choice_1'] = $chessmen[$random_piece];

    unset($chessmen[$random_piece]);

    $random_piece = array_rand($chessmen); 
    $_SESSION['choice_2'] = $chessmen[$random_piece];
} 
?>

您的会话数据存储在 $_SESSION-Array 内部,而不是作为通用 php 变量存储在外部。因此,如果您想稍后打印“choise_1”,则需要echo $_SESSION['choise_1'];.

于 2012-11-04T19:12:22.573 回答