2

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?

3
  • Why not $_SESSION['create'] = array(); again? Commented Jan 28, 2014 at 23:09
  • 2
    unset($_SESSION['create']) works to unset that particular key, if it doesn't work for you, maybe some code is adding it in again. $_SESSION=array() would reset the whole session. Commented Jan 28, 2014 at 23:12
  • it doesn't reset the session Commented Jan 28, 2014 at 23:24

3 Answers 3

5

You'll want to use the following built-in function to do so

session_unset();

session_unset on PHP.net

Sign up to request clarification or add additional context in comments.

3 Comments

i don't want to unset other sessions only that specific session array
Could you give us a larger piece of code to work with and state which version of PHP you're using?
Just a hunch, but verify that you didn't close the session for writing ( session_write_close() ) prior to unsetting the array key ( which should work ). Doing so will unset the vars for the current execution but will cause all unset session vars to return next session_start()
3

Basically you want to set the create part of the session empty again. You do this by using $_SESSION['create'] = array(); again, as you did to initialize it.

1 Comment

could you please upvote Fred -ii- answer for his effort
2

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.