?
Solved

problem w/ sessions

Posted on 2005-05-17
1
Medium Priority
?
199 Views
Last Modified: 2010-08-05
Hi folks.
I'm kinda new at this, but I'm having trouble retrieving (or setting) Session variables.  They seem to be lost whenever the page reloads.
Aren't they supposed to remain until unset or until the browser is closed?

brief summary of what i'm doing:

at the top of the page:

if(!isset($_SESSION)) {
      session_start();
      header("Cache-control: private");
}

if ($action == "logout") {
      $_SESSION = array();
                session_unregister();
      session_unset();
}


if ($action == "login") {
                $_SESSION['login'] = 1;
}

if ($_SESSION['login'] ==1) {
                stuff;
} else { other stuff;  }


Now, I don't think I'm properly checking whether or not the session is set.  Basically, I don't want it to be destroyed until the user logs out or closes the browser window.

Am I using the Session functions incorrectly?
Is there a better way to check whether a user is logged in or out in this scenario?







0
Comment
Question by:starfailure
1 Comment
 

Accepted Solution

by:
herkules85 earned 400 total points
ID: 14019088
<?PHP //top of page
session_start();

if ($action == "logout") {
     session_unregister();
     session_unset();
}


if ($action == "login") {
                $_SESSION['login'] = 1;
}

if (!isset($_SESSION['login']) && $_SESSION['login'] !=1) { //not logged in
 
  header("Location you_must_login.html");
  exit;
}


//do whatever you want here
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question