简体   繁体   English

如何在PHP中取消设置会话数组

[英]how to unset a session array in PHP

I created a session array using 我使用创建了一个会话数组

$_SESSION['create'] = array();

then filled out the array using this, 然后用这个填充数组

$_SESSION['create']['new-campaign-name']    = $_POST['new-campaign-name'];
$_SESSION['create']['new-campaign-type']    = $_POST['new-campaign-type'];
$_SESSION['create']['new-campaign-country'] = $_POST['new-campaign-country'];
$_SESSION['create']['new-campaign-list']    = $_POST['new-campaign-contact'];
$_SESSION['create']['new-campaign-des']     = $_POST['new-campaign-des'];

but i would want to delete all of the content of the session array. 但我想删除会话数组的所有内容。

i tried this 我尝试了这个

unset($_SESSION['create']);

but it seems to be not working and all of the values are still on the session data, is there a way to reset the contents of my session array? 但它似乎无法正常工作,并且所有值仍在会话数据上,是否有办法重置会话数组的内容?

You'll want to use the following built-in function to do so 您将需要使用以下内置函数来执行此操作

session_unset();

session_unset on PHP.net 在PHP.net上的session_unset

Basically you want to set the create part of the session empty again. 基本上,您想再次将会话的create部分设置为空。 You do this by using $_SESSION['create'] = array(); 您可以使用$_SESSION['create'] = array(); again, as you did to initialize it. 再次进行初始化操作。

Here are some tests that I ran using your existing session variables, including some of my own. 这是我使用您现有的会话变量运行的一些测试,包括我自己的一些变量。

Consult the comments throughout the script. 请查阅整个脚本中的注释。

<?php
session_start();
$_SESSION['create'] = array();

// this one can be used also
// $_SESSION=array();

/*
$_SESSION['create']['new-campaign-name']    = $_POST['new-campaign-name'];
$_SESSION['create']['new-campaign-type']    = $_POST['new-campaign-type'];
$_SESSION['create']['new-campaign-country'] = $_POST['new-campaign-country'];
$_SESSION['create']['new-campaign-list']    = $_POST['new-campaign-contact'];
$_SESSION['create']['new-campaign-des']     = $_POST['new-campaign-des'];

$_SESSION['create_2']['new-campaign-des_2']     = $_POST['new-campaign-des_2'];

*/

// my own test without POST variables
$_SESSION['create']['new-campaign-name']    = 'new-campaign-name';
$_SESSION['create']['new-campaign-type']    = 'new-campaign-type';
$_SESSION['create']['new-campaign-country'] = 'new-campaign-country';
$_SESSION['create']['new-campaign-list']    = 'new-campaign-contact';
$_SESSION['create']['new-campaign-des']     = 'new-campaign-des';

$_SESSION['create_2']['new-campaign-des_2']     = 'new-campaign-des_2';

// echo "Session variables above this";

echo $_SESSION['create']; // this will echo Array

echo "<br>";

// this will unset all in $_SESSION['create'] group
unset($_SESSION['create']);

echo "<br>";

var_dump($_SESSION);

echo "<hr>";

// this will echo NULL
var_dump($_SESSION['create']);

echo "<hr>";

// this will echo Array even after unsetting $_SESSION['create']
echo $_SESSION['create_2'];

echo "<hr>";

// this will echo/dump
// array(1) { ["new-campaign-des_2"]=> string(18) "new-campaign-des_2" } 
var_dump($_SESSION['create_2']);

echo "<hr>";

// this will echo/dump
// array(1) { ["create_2"]=> array(1) { ["new-campaign-des_2"]=> string(18) "new-campaign-des_2" } }
// but not anything in the ['create'] group
var_dump($_SESSION);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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