Logging in and out

  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

I've got a backend system up and running now. There is one main problem.
On the login page I start a session and if the user is logged in they are redirected to the admin_main.php page. If they arent then they get the login box and auth and logname are registered and sent into the db.
PHP Code: [ Select ]
 
session_register('auth');
 
session_register('logname');
 
 
  1.  
  2. session_register('auth');
  3.  
  4. session_register('logname');
  5.  
  6.  


When they login correctly auth is set to yes and then on each subsequent page this is checked and if auth is no, i.e. no-one has logged in they are redirected. The variable logname is also stored and can be recalled at any time.

Now on every page there is also a session_register(); function at the top. How do I log the user out and throw those variables away so that they are still stored in the database but the user has to log in again to get back into the system?

The logout code is as follows and is the only php in its own page:-
PHP Code: [ Select ]
 
session_destroy();
 
unset($logname,$auth);
 
exit();
 
 
  1.  
  2. session_destroy();
  3.  
  4. unset($logname,$auth);
  5.  
  6. exit();
  7.  
  8.  


I get this error
Warning: session_destroy(): Trying to destroy uninitialized session in /home/animyst/public_html/logout.php on line 2
And also before I was being told that I was trying to unset variables that werent there.
Can anyone tell me how I would log the user out?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Do a test to see if the variables are set first then destroy it if they are

PHP Code: [ Select ]
 
if (isset($varName){
 
   session_destroy(); // no need for unset;
 
}
 
//redirect here
 
 
  1.  
  2. if (isset($varName){
  3.  
  4.    session_destroy(); // no need for unset;
  5.  
  6. }
  7.  
  8. //redirect here
  9.  
  10.  
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

I used that but on its own it will not work. You have to start a session first on the lgoout page for the program to realise there are any variables. This is the code I have for my login page.
PHP Code: [ Select ]
 
<?php
 
   session_start();
 
  if ($auth !== "yes"){
 
      header("Location: <!-- m --><span class="postlink">http://animyst.gracious.me.uk/admin.php</span><!-- m -->");
 
      exit();
 
  }else{
 
      if (isset($auth)){
 
            session_destroy();   
 
            include ("logout.inc.php");
 
      }else{
 
            header("Location: <!-- m --><span class="postlink">http://animyst.gracious.me.uk/admin_main.php</span><!-- m -->");
 
            }
 
      }
 
?>
 
 
  1.  
  2. <?php
  3.  
  4.    session_start();
  5.  
  6.   if ($auth !== "yes"){
  7.  
  8.       header("Location: <!-- m --><span class="postlink">http://animyst.gracious.me.uk/admin.php</span><!-- m -->");
  9.  
  10.       exit();
  11.  
  12.   }else{
  13.  
  14.       if (isset($auth)){
  15.  
  16.             session_destroy();   
  17.  
  18.             include ("logout.inc.php");
  19.  
  20.       }else{
  21.  
  22.             header("Location: <!-- m --><span class="postlink">http://animyst.gracious.me.uk/admin_main.php</span><!-- m -->");
  23.  
  24.             }
  25.  
  26.       }
  27.  
  28. ?>
  29.  
  30.  

It starts the session, checks if the suer is logged in, if they arent, they are redirected, if not, it finds if there is an $auth variable and if there is(whihc there must be if it is set to yes) then the session is destroyed and the user is logged out.
Phew. Thanks for the continued help Rabid Dog. Much appreciated. But i'm not stopping now!
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Sorry I assumed you had started the session on the page

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 73 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.