How to validate php session in after login page using one line code

Validate your after login page for valid user session. In normal process we make condition for check user session set or not. If session not found proper, system take required action.

But using same if condition or login validation code in all pages, will be hard to manage when it required change. It’s better if all after login page session validation code write at one place and manage session validation using one line function call.

For manage session validation to check user logged in or not we need to make some change in regular coding process.

Full process for validate php session in after login page using one line code achieved in below steps.

  1. Create validate session function and add in common function file.
  2. Add function call in after login page before start process.
  3. Change in ajax calls for handle session error.
How to validate php session in after login page using one line code
How to validate php session in after login page using one line code

1) Create validate session function and add in common function file.

function validateUser($redirect = true)
{	
    if(!empty($_SESSION['user_id']))
    {
        return true;
    }
    else if($redirect == false)
    {
        return false;
    }
    else
    {
        //REDIRECT TO HOME PAGE/ERROR PAGE
        //OR
        //PROCESS FOR INVALID VISIT
    }
}

Function process:
First check whether user session for visiting page exist or not. Here it check by $_SESSION[‘user_id’] variable. It may different in your case. If process is different for you, need to write require validate session code before if condition and get final value for session valid or not. After validate process need to take different action based on result.

After validate process need to take different action based on result.

  • Valid session found: Return true
  • Invalid session found: Check redirect function parameter. If $redirect = true redirect page, else return false value.

Redirect parameter used when we need to check only user session exist or not and not want to redirect page. i.e., Pages that do some action using ajax and process require logged in status before continue.

2) Add function call in after login page before start process

//Include your common function file.

//function call
validateUser();

//Your after login page process

By just add one function call line you able to redirect user to decided page and display proper message. No need to add same session validation code in all pages.

3) Change in ajax calls for handle session error.

Change in php ajax handler file (process.php)

//Include your common function file.

//function call
if(!validateUser(false))
{
    $resp = array(
        'success' => false,
        'noAccess' => true,
        'message' => 'Your error message.'
    );
	
    header('Content-Type: application/json');
    echo json_encode($resp);
    exit;
}

//Your after login page process

Change in ajax call

$.ajax({
    url: "process.php",
    data: '{IF ANY}',
    contentType: 'application/json',
}).done(function(data) {
    if(data.success){
        //process on success
    }else if(data.noAccess){
        //process when session expire
        alert(data.message);
    }else{
        //process on request fail
    }
});