PHP
1
2<?php
3session_start();
4$name = filter_input(INPUT_POST,'username');
5$title = filter_input(INPUT_POST,'title');
6$email = filter_input(INPUT_POST,'email');
7if(!is_null($name)) $_SESSION['name']= $name;
8if(!is_null($title)) $_SESSION['title']= $title;
9if(!is_null($email)) $_SESSION['email']= $email;
10?>
11[ <a href="<?php echo $_SERVER["SCRIPT_NAME"];?>">リロード</a> ]
12<hr>
13<form action="" method="post">
14名前:<input type="text" name="username" value="<?php echo htmlspecialchars($name);?>"><br>
15タイトル:<input type="text" name="title" value="<?php echo htmlspecialchars($title);?>"><br>
16メールアドレス:<input type="text" name="email" value="<?php echo htmlspecialchars($email);?>"><br>
17<input type="submit" name="exe" value="送信じゃい!">
18</form>
19<?php
20echo '<pre>';
21ob_start();
22var_dump($_SESSION);
23$content=ob_get_contents();
24ob_end_clean();
25print htmlspecialchars($content);
26echo '</pre>';
27?>
28<table border>
29<tr>
30<?php
31foreach($_SESSION as $val){
32 echo '<td>'.htmlspecialchars($val).'</td>';
33}
34?>
35</tr>
36</table>
完全修正版
PHP
1<?php
2session_start();
3$del = filter_input(INPUT_GET,'del');
4if(!is_null($del) and isset($_SESSION["data"]) and isset($_SESSION["data"][$del])){
5 unset($_SESSION["data"][$del]);
6}
7if($del==="all" and isset($_SESSION["data"])){
8 unset($_SESSION["data"]);
9}
10$name = filter_input(INPUT_POST,'username');
11$title = filter_input(INPUT_POST,'title');
12$email = filter_input(INPUT_POST,'email');
13if(!isset($_SESSION["data"])){
14 $_SESSION["data"]=[];
15}
16if(!is_null($name)){
17 $_SESSION["data"][]=["name"=>$name,"title"=>$title,"email"=>$email];
18}
19?>
20[ <a href="<?php echo $_SERVER["SCRIPT_NAME"];?>">リロード</a>
21 | <a href="<?php echo $_SERVER["SCRIPT_NAME"];?>?del=all">全削除</a> ]
22<hr>
23<form action="<?php echo $_SERVER["SCRIPT_NAME"];?>" method="post">
24名前:<input type="text" name="username"><br>
25タイトル:<input type="text" name="title"><br>
26メールアドレス:<input type="text" name="email"><br>
27<input type="submit" name="exe" value="送信じゃい!">
28</form>
29<table border>
30<thead>
31<tr>
32<th>名前</th>
33<th>タイトル</th>
34<th>email</th>
35<th>-</th>
36</tr>
37</thead>
38<tbody>
39<?php
40foreach($_SESSION["data"] as $key=>$row){
41 echo '<tr>';
42 echo '<td>'.htmlspecialchars($row["name"]).'</td>';
43 echo '<td>'.htmlspecialchars($row["title"]).'</td>';
44 echo '<td>'.htmlspecialchars($row["email"]).'</td>';
45 echo '<td><a href="?del='.$key.'">del</a></td>';
46 echo '</tr>';
47}
48?>
49</tr>
50</tbody>
51</table>
52<hr>
53<?php
54echo '<pre>';
55ob_start();
56var_dump($_SESSION);
57$content=ob_get_contents();
58ob_end_clean();
59print htmlspecialchars($content);
60echo '</pre>';
61?>
62