我是 php 的新手,这个问题从前一天一直困扰着我。我有三页,1. classpage.php ---- 其中包含,
<?php
session_start();
class addStock{
public function storeSessArray($stk_code){
array_push($_SESSION['recentlyView'],$stk_code);
}
}
?>
$_SESSION['recentlyView']=array();
$checkSession = new addStock;
$chkSession = $checkSession->storeSessArray($val);
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
?>
和3. front.php持有
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title Page</title>
</head>
<body>
<?php
$val='5';
?>
<?php
include 'logic.php';
?>
</body>
</html>
$_SESSION['recentlyView']
当有人加载front.php时,我想将一组值存储在数组中。但是每次我为 $val 分配不同的值时,只有存储在 $val 中的最后一个值出现在$_SESSION['recentlyView']
数组中。我希望使用的每个值$val
都存储在$_SESSION['recentlyView']
您可能会不断覆盖会话数组。尝试检查它是否已经设置:
if(!isset($_SESSION['recentlyView']))
$_SESSION['recentlyView'] = array();
这只会设置数组一次,然后您的方法将添加到它。
另外,如另一个答案中所述,请确保将 放在session_start();
最开始的最顶层页面。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句