Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Close session

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Close session

Postby JediBookworm » Thu Jul 03, 2003 3:37 pm

Hi,

I have a login page that creates a session variable.
I want the pages that I want to protect to verify if the session variable is equal to something. If it doesn't, then it should redirect to the login page, which I want to close the session as to 'log out'.

However, I don't seem to close the session properly, OR something is wrong with the verifying statement on the protected page, so that whether there is a session or not, the pages can be accessed.

LOGIN PAGE CODE:

Code: Select all
<?php
session_unregister("auth");
session_unset();
$pass="something";
if ($pw == $pass)
{
   session_start();
   session_register("auth");
   $auth="Yes";
   header ("location:protected.php");
}
elseif ($pw=="")
{
   $errmsg="";
}
elseif ($pw != $pass)
{
   $errmsg="<b>Error! Please try again.</b>";
}
$pw="";
?>


PROTECTED PAGE CODE:

Code: Select all
<?php
session_start();
session_register("auth");
if ($auth != "Yes")
{
   header ("location:login.php");
}
?>


Thanks.
JediBookworm
New php-forum User
New php-forum User
 
Posts: 13
Joined: Mon Jun 30, 2003 3:54 pm
Location: Canada

Postby JediBookworm » Thu Jul 03, 2003 8:26 pm

I tried again and I am 99% sure that the problem is the part of closing the session or session variables on the login (or logout) page. So how could I fix it?
JediBookworm
New php-forum User
New php-forum User
 
Posts: 13
Joined: Mon Jun 30, 2003 3:54 pm
Location: Canada

the login page

Postby ruturajv » Fri Jul 04, 2003 5:57 am

THe below page should check the login
Code: Select all
if ($something == "something") {
  session_register("auth");
  $_SESSION['auth']=true;
  $url = newpage.php . SID
  header(location: url);
} else {
  header(location:firstpage.php);
}


The below page is the restricted page
Code: Select all
session_start();
if ($_SESSION['auth']) {
  good!!!
else {
 go to back page
}


Hope this helps
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India

Session close

Postby ruturajv » Fri Jul 04, 2003 5:59 am

be sure you pass the SID to logout page as well

and then start the session by
session_start();

and then close it
session_close();
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India

Postby JediBookworm » Fri Jul 04, 2003 2:15 pm

Thanks, but somehow I still can't close the session.

session_close() is undefined, session_unset() and session_unregister("auth") don't work

The only way I found is to close the window...grrr
JediBookworm
New php-forum User
New php-forum User
 
Posts: 13
Joined: Mon Jun 30, 2003 3:54 pm
Location: Canada

try this

Postby ruturajv » Sat Jul 05, 2003 6:42 am

Code: Select all
<?php
session_start();

if (session_is_registered('auth')) {
   session_destroy();
   header('location: default.sps');
} else {
   header('location: login.sps?err=login');
}
?>


BE sure that you append the SID through the logout link to the above page
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.