Jump to content

Initialze a Session


doubledee

Recommended Posts

I am debugging my Log-In module and would like to erase data that was stored in my $_SESSION variable from my prior debugging session.

 

(It will just be easier for me to follow what is going on if all variables in my SESSION are null.)

 

How do I do that?

 

Where do I do that?

 

Do I destroy the SESSION and then start it?

 

Or do I start the SESSION and then destroy it?  :confused:

 

 

 

Debbie

 

Link to comment
Share on other sites

I'd suggest a simple function:

 

function cleanSession() {
  foreach($_SESSION as $sessvar) {
    unset($sessvar);
  }
}

 

Add it where you want to insure that there are no set session variables.

 

 

But I call that function after I start the session in my header?

 

 

Debbie

 

Link to comment
Share on other sites

Because then you would have to reinitialize the session.  You stated you wanted to have something you could drop in for testing purposes that doesn't do black magic with the session handler.  If you really want to use session_destroy then you'd have to have:

 

session_start();

session_destroy();

session_start();

Link to comment
Share on other sites

Because then you would have to reinitialize the session.  You stated you wanted to have something you could drop in for testing purposes that doesn't do black magic with the session handler.  If you really want to use session_destroy then you'd have to have:

 

session_start();

session_destroy();

session_start();

 

Oh, okay.

 

 

Debbie

 

Link to comment
Share on other sites

Well, it wouldn't be that bad lookin'.

 

$session_id = session_id();

if (empty($session_id))
    session_start();

if (isset($_GET['logout']))
{
    session_destroy();
    session_start();
}

 

The session_start func should be there no matter what.  The logout may have more to it as well (assuming that's why you're destroying it), but same thing pretty much no matter what.

Link to comment
Share on other sites

I'd suggest a simple function:

 

function cleanSession() {
  foreach($_SESSION as $sessvar) {
    unset($sessvar);
  }
}

 

By the way, I added your code in like...

 

<?php
// Initialize a session.
session_start();

foreach($_SESSION as $sessvar) {
	unset($sessvar);
}

 

But when I step through NetBeans it isn't re-setting my SESSION variables?!

 

 

Debbie

 

 

Link to comment
Share on other sites

I think gizmola was aiming more for

foreach(array_keys($_SESSION) as $sessvar) {
  unset($_SESSION[$sessvar]);
}

Right now it'll just unset $sessvar and that won't affect what's in $_SESSION.

 

I don't get it?!

 

I thought "un-setting" was like erasing each attribute?

 

So what should it be to erase each value?

 

 

Debbie

 

 

Link to comment
Share on other sites

It should be what I posted.

 

unset() will destroy values in memory according to what variable you give it. If you

$a = 1;
$b = $a;
unset($b);

then you are destroying $b, and that has no bearing on what happens to $a. Similarly

foreach ($array as $value) {
    unset($value);
}

will destroy $value, and that has no bearing on what happens to $array. By using array keys like

foreach (array_keys($array) as $key) {
    unset($array[$key]);
}

then you are destroying $array[$key].

Link to comment
Share on other sites

It should be what I posted.

 

unset() will destroy values in memory according to what variable you give it. If you

$a = 1;
$b = $a;
unset($b);

then you are destroying $b, and that has no bearing on what happens to $a. Similarly

foreach ($array as $value) {
    unset($value);
}

will destroy $value, and that has no bearing on what happens to $array. By using array keys like

foreach (array_keys($array) as $key) {
    unset($array[$key]);
}

then you are destroying $array[$key].

 

But even if I use your original code and just "unset" the Session values, should I see the values go to '' or null in NetBeans as the code runs through the loop?

 

 

Debbie

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.