Creating a file based login system



by Administrator author list


Step 6.
The next step is to logout the user. To realise this we will implement anew method in the common.php file. The function is function logoutUser()
The function is quite easy. It only removes the user session variables and looks like this:

<?php
function logoutUser(){
    unset(
$_SESSION['validUser']);
    unset(
$_SESSION['userName']);
}
?>

Step 7.
We create a logout.php file which is as simple as the logoutUser function. We just call the function and redirect the user the the test page. The code is this:

<?php
    
require_once('common.php');
    
logoutUser();
    
header('Location: test.php');
?>

Step 8.
Ok, now we can login, logout and register user, but how to check that there is a logged in user? It is not so complicated as well. We need to check whether the session variables are set or not. If the user is not logged in than we redirect to the login page. There he/she can decide to login or register. To implement this functionality we will create a checkUser function in the common.php file with the following content:

<?php
function checkUser(){
    if ((!isset(
$_SESSION['validUser'])) 
         || (
$_SESSION['validUser'] != true)){
        
header('Location: login.php');
    }
}
?>



article index
page 1 : untitled page
page 2 : Part 2
page 3 : Part 3
page 4 - current : Part 4
page 5 : Part 5


Tags:

Php Toys - 2006 - Php resources, scripts and tutorials - Privacy Policy
Insurance Index - Tutorial collection - Forex trading, brokers, reviews - Mortgage payment calculator
{THEMEDISCLAIMER}
Render time: 0.1280 sec, 0.0178 of that for queries. DB queries: 25.