我想设置$ 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];}
?>
答案 0 :(得分:0)
在打印任何输出之前使用session_start()或在php.ini
中将session auto start设置为1<?php
session_start();
if(!isset($_SESSION['test']))
echo "no test session";
else
echo $_SESSION['test'];
if($sth)
{
$_SESSION['test'] = 'sth...';
}
答案 1 :(得分: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变量存储在外部。所以如果你想 - 例如 - 打印&#34; choise_1&#34;之后,您需要echo $_SESSION['choise_1'];
。