简体   繁体   中英

Clear Cookies and Session Data in PHP

I'm using PHP to clear all cookie data, specifically clear session data, clean session id, and delete the cookies for the session. I have the following code as part of my coding, but when I click the Clear All button, nothing happens. The rest of my buttons in the cases work as intended.

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

if (isset ( $_SESSION ['tasklist'] )) {
    $task_list = $_SESSION ['tasklist'];
} else {
    $task_list = array ();
}
$action = filter_input ( INPUT_POST, 'action' );
$errors = array ();

switch ($action) {
    case 'add' :
        $new_task = filter_input ( INPUT_POST, 'newtask' );
        if (empty ( $new_task )) {
            $errors [] = 'The new task cannot be empty.';
        } else {
            $task_list [] = $new_task;
        }
        break;
    case 'delete' :
        $task_index = filter_input ( INPUT_POST, 'taskid', FILTER_VALIDATE_INT );
        if ($task_index === NULL || $task_index === FALSE) {
            $errors [] = 'The task cannot be deleted.';
        } else {
            unset ( $task_list [$task_index] );
            $task_list = array_values ( $task_list );
        }
        break;
    case 'clear':
        $_SESSION = array();
        session_destroy();
        $name = session_name();
        $expire = strtotime('-1 year');
        $params = session_get_cookie_params();
        $path = $params['path'];
        $domain = $params['domain'];
        $secure = $params['secure'];
        $httponly = $params['httponly'];
        setcookie($name, '', $expire, $path, $domain, $secure, $httponly);
        include('task_list.php');
        break;
}

$_SESSION ['tasklist'] = $task_list;

include ('task_list.php');

?>

And in the main page:

<!DOCTYPE html>
<html>
<head>
<title>Task List Manager</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
    <header>
        <h1>Task List Manager</h1>
    </header>
    <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">
        <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">
        <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; ?>

    <br><input type="hidden" name="action" value="clear"><label>&nbsp;</label><input type="submit" value="Clear All">
    </main>
    <footer>
        <p>Session ID: <?php echo session_id() ?>
    </footer>
</body>
</html>

Put a form tag around the clear-all burton as well as you did for the other values.

Currently, the click doesn't have any action associated, so it will do nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM