Jump to content

[SOLVED] Trying to unset single session variable unsets entire session


HaLo2FrEeEk

Recommended Posts

Hey, I'm still writing the code for my school's vote server and it's nearly complete, but I'm having a problem.  I have all the data being stored in session variables, so when the student puts in his student ID, it passes it from one page to the next.  After the student votes, I use a function called logout that checks if the vote was counted, then uses session_unset($_SESSION['studentID']) and I also have a place that passes an error through session to let the user know of various problems with their login, such as if they have already voted, if voting is disabled, or if there are no candidates to vote on.  The problem I am having is when I use session_unset, it unsets ALL session variables, not just the ones I want.  php.net says not to use session_unregister if register_globals is disabled, which it is.  What else can I do?  I need to be able to unset only $_SESSION['error'] (the returned error message) and $_SESSION['studentID'], instead of unsetting the whole session.

 

EDIT: Could I use named sessions?  And destroy the named session for students and admins seperately?  Could someone tell me how to do this.

Link to comment
Share on other sites

I had my setup so it was like this...  i had 3 different types of people who would be logged in, and for testing purposes i had to login to all 3 on the same machine and so i setup my sessions like this:

 

$_SESSION['admin'] = logged in admin array

$_SESSION['rater'] = logged in rater array

$_SESSION['user'] = logged in user/student array

 

then to destroy a certain one i would use

 

session_destroy('admin'); or whatever i named the session.

Link to comment
Share on other sites

Can you elaborate a little bit, setting $_SESSION['admin'] would only set the session variable admin, you can't call session_destroy(admin) becuase session_destroy doesn't accept modifiers like that.  Did you mean naming the session?  Could I name the session for student login like this:

 

session_start();

session_name("student");

 

Then the one for admins would be:

 

session_start();

session_name("admin");

 

Then to destroy each one, I could simply do session_unset() on whichever one I am working on?

 

Can someone please help me understand this better.

Link to comment
Share on other sites

As long as you have called session_start() on the page, you should be able to just do unset($_SESSION['sessionname']) to have it both removed from the $_SESSION array but also removed from the temporary session file.

Link to comment
Share on other sites

I call sesssion_start on all my pages, but session_unset doesn't accept variables, php.net's description looks like this:

 

session_unset — Free all session variables

 

void session_unset ( void )

 

You can't only destroy one variable, you have to destroy them all, but it doesn't actually destroy the session.

 

GAH EDIT: Jeez, I feel dumb, I didn't see that there was a difference between session_unset and unset, I thought that unset was just an alias for session_unset.  Thank you for your help, I will test this and get back to you if I need more help.  Thank you so much, you're a life saver, everyone that helped.

 

: )

Link to comment
Share on other sites

I need to be able to unset only $_SESSION['error'] (the returned error message) and $_SESSION['studentID'], instead of unsetting the whole session.

 

<?php

  session_start();
  unset($_SESSION['error']);
  unset($_SESSION['studentID']);

?>

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.