【问题标题】:How to save information in array for session to recall php如何将信息保存在数组中以供会话调用php
【发布时间】:2013-04-07 22:40:41
【问题描述】:

所以这是来自一本非常糟糕的 PHP 书。它假定您已经知道如何使用 PHP,并针对它提出的问题给出了完全不适用的示例。我需要将任务保存到一个数组中,这样当页面打开备份时,它会显示您之前所做的任务。网络上几乎没有任何 PHP 示例,除了极其简单和极其困难。

<?php
$lifetime = 60 * 60 * 24 * 365 ;    
session_set_cookie_params($lifetime, "/");
session_start();

if (empty($_SESSION['tasks'])) $_SESSION['tasks'] = array();

if (isset($_POST['tasklist'])) {
    $task_list = $_POST['tasklist'];
} else {
    $task_list = array();
}

$errors = array();

switch( $_POST['action'] ) {
    case 'add':
        $new_task = $_POST['newtask'];
        if (empty($new_task)) {
            $errors[] = 'The new task cannot be empty.';
        } else {
            $task_list[] = $new_task;
        }
        break;
    case 'delete':
             $task_index = $_POST['taskid'];
             unset($task_list[$task_index]);
            $task_list = array_values($task_list);
            break;
    }

task_list.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Task List Manager</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
    <div id="page">
    <div id="header">
        <h1>Task List Manager</h1>
    </div>
    <div id="main">

    <!-- part 1: the errors -->
    <?php if (count($errors) > 0) : ?>
    <h2>Errors</h2>
    <ul>
        <?php foreach($errors as $error) : ?>
            <li><?php echo $error; ?></li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>

    <!-- part 2: the tasks -->
    <h2>Tasks</h2>
    <?php if (count($task_list) == 0) : ?>
        <p>There are no tasks in the task list.</p>
    <?php else: ?>
        <ul>
        <?php foreach($task_list as $id => $task) : ?>
            <li><?php echo $id + 1 . '. ' . $task; ?></li>
        <?php endforeach; ?>
        </ul>
    <?php endif; ?>
    <br />

    <!-- part 3: the add form -->
    <h2>Add Task</h2>
    <form action="." method="post" >
        <?php foreach($task_list as $task) : ?>
          <input type="hidden" name="tasklist[]" value="<?php echo $task; ?>"/>
        <?php endforeach; ?>
        <input type="hidden" name="action" value="add"/>
        <label>Task:</label>
        <input type="text" name="newtask" id="newtask" /> <br />
        <label>&nbsp;</label>
        <input type="submit" value="Add Task"/>
    </form>
    <br />


    <!-- part 4: the delete form -->
    <?php if (count($task_list) > 0) : ?>
    <h2>Delete Task</h2>
    <form action="." method="post" >
        <?php foreach($task_list as $task) : ?>
          <input type="hidden" name="tasklist[]" value="<?php echo $task; ?>"/>
        <?php endforeach; ?>
        <input type="hidden" name="action" value="delete"/>
        <label>Task:</label>
        <select name="taskid">
            <?php foreach($task_list as $id => $task) : ?>
                <option value="<?php echo $id; ?>">
                    <?php echo $task; ?>
                </option>
            <?php endforeach; ?>
        </select>
        <br />
        <label>&nbsp;</label>
        <input type="submit" value="Delete Task"/>
    </form>
    <?php endif; ?>

    </div><!-- end main -->
    </div><!-- end page -->
</body>
</html>

【问题讨论】:

  • 我只阅读了标题...为此使用 SESSIONS,而不是 cookie。
  • 因此,将当前信息存储在序列化 cookie 中(此外,出于安全原因,您不应对 cookie 数据使用(取消)序列化)。更好的是,只需在 cookie 中存储一个唯一 ID,然后从具有该 ID 的数据库表中获取数据。
  • 您可以将数组存储在会话变量或 cookie 中,但是当用户决定清除缓存时,他将丢失任务列表。如果您希望遵循此路径,请确保在您的 $_SESSION['tasks']-array 中填写所有任务。
  • 如何将所有任务保存到数组中?
  • 正如 Marty McVly 所说,用户不太可能在数周后回来完成他的提交 - 因此,这部分代码似乎非常不重要。更重要的是让用户留在网站上并完整输入他的数据。

标签: php arrays session cookies


【解决方案1】:

修改后(使用操作切换)后,您没有将新数组存储在 _SESSION 中。这样做:

$_SESSION['tasks'] = $task_list;

查看它们:

foreach($_SESSION['tasks'] as $value) {
    //display task
}

顺便说一句,这是可怕的想法。你应该使用数据库。一些主机会阻止 session_set_cookie_params 并且用户在清除浏览器缓存后会丢失他的数据。

你也应该这样称呼它:

session_set_cookie_params($time);

第二个参数是可选的。

更多数据示例

核心:

//session start etc
if(!isset($_SESSION['tasks'])) $_SESSION['tasks'] = array();
if(isset($_POST['action']) {
    switch($_POST['action']) {
        case 'add':
            if(isset($_POST['newtask']) && !empty($_POST['newtask']))
                $_SESSION['tasks'][] = $_POST['newtask'];
            break;
        case 'remove':
            if(isset($_POST['taskid']))
                unset($_SESSION['tasks'][$_POST['taskid']]);
            break;
    }
}

显示:

echo '<form method=post><input type=hidden name=action value=remove>';
foreach($_SESSION['tasks'] as $k => $v) {
    echo $v.'<br />Remove: <input type=submit name=taskid value='.$k.'>';
}
echo '</form>';

添加:

<form method=post>
<input type=hidden name=action value=add>
<input type=text name=newtask>
<input type=submit>
</form>

【讨论】:

  • 我还是不明白。似乎我尝试的每一种方式都会出错。
  • 我改进了我的答案。检查一下。
猜你喜欢
  • 1970-01-01
  • 2013-06-27
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多