Sar*_*raz 74
是的,您可以将数组放入会话中,例如:
$_SESSION['name_here'] = $your_array;
Run Code Online (Sandbox Code Playgroud)
现在你可以使用$_SESSION['name_here']你想要的任何页面,但确保session_start()在使用任何会话函数之前放置行,所以你的代码应该是这样的:
session_start();
$_SESSION['name_here'] = $your_array;
Run Code Online (Sandbox Code Playgroud)
可能的例子:
session_start();
$_SESSION['name_here'] = $_POST;
Run Code Online (Sandbox Code Playgroud)
现在,您可以在任何页面上获取字段值,如下所示:
echo $_SESSION['name_here']['field_name'];
Run Code Online (Sandbox Code Playgroud)
至于问题的第二部分,除非您分配不同的数组数据,否则会话变量将保留在那里:
$_SESSION['name_here'] = $your_array;
Run Code Online (Sandbox Code Playgroud)
会话生存时间设置为php.ini文件.
小智 5
<?php // PHP part
session_start(); // Start the session
$_SESSION['student']=array(); // Makes the session an array
$student_name=$_POST['student_name']; //student_name form field name
$student_city=$_POST['city_id']; //city_id form field name
array_push($_SESSION['student'],$student_name,$student_city);
//print_r($_SESSION['student']);
?>
<table class="table"> <!-- HTML Part (optional) -->
<tr>
<th>Name</th>
<th>City</th>
</tr>
<tr>
<?php for($i = 0 ; $i < count($_SESSION['student']) ; $i++) {
echo '<td>'.$_SESSION['student'][$i].'</td>';
} ?>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)