Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6205559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:17:47+00:00 2026-05-24T05:17:47+00:00

My PHP+JS+Ajax application requires users to log in, then saves login data to $_SESSION.

  • 0

My PHP+JS+Ajax application requires users to log in, then saves login data to $_SESSION.

There are three types of users: User, Moderator, Admin. Each has more rights than previous.

All queries are made to ajax_req.php

Because I want to keep the ajax_req.php secure, I check the $_SESSION data to determine if the user is logged in and in what group He/She belongs.

The code for this is following:

if(isset($_SESSION['logged'])) {
    if(isset($_SESSION['group_id'])) {
        $group_id=(int)$_SESSION['group_id'];

        if($group_id==ADMIN_GROUP) {
            $login_success=1;
        } else if ($group_id==MODERATOR_GROUP) {
            $login_success=1;
        } else if ($group_id==USER_GROUP) {
            $login_success=1;
        } else {
            $login_success=-1;
        }
    } else {
        $login_success=-1;
    }
} 

if($login_success<1) {
    header('HTTP/1.1 401 Unauthorized');
    $ret = array('status' => 'FALSE', 'txt' => 'Unauthorized access. Please check if You are still logged in.' );
    print json_encode($ret); 
    exit();
}

First question: Am I doing this right?

Second question

I want to protect my application from issuing commands meant for admins.

Next, I have following function:

function assert_right($group_id, $needed) {
    if($group_id==ADMIN_GROUP) {
        return true; 
    } else if ($group_id==MODERATOR_GROUP) {
        if($needed==USER_GROUP || $needed==MODERATOR_GROUP) {
            return true; 
        }
    } else if ($group_id==USER_GROUP) {
        if($needed==USER_GROUP) {
            return true; 
        }
    } 

    header('HTTP/1.1 403 Forbidden');
    $ret = array('status' => 'FALSE', 'txt' => 'Unauthorized access. You do not have sufficient rights to do this action.' );
    print json_encode($ret); 
    exit();
}

And before sensitive action I call

assert_right($_SESSION['group_id'], ADMIN_GROUP);

Second question: Is it correct to return 401 in the first case and 403 on the second? Basically, 403 means “Forbidden”, which according to http://en.wikipedia.org/wiki/List_of_HTTP_status_codes means “The request was a legal request, but the server is refusing to respond to it. Unlike a 401 Unauthorized response, authenticating will make no difference.”. I also checked https://dev.twitter.com/docs/error-codes-responses, which was given as answer to another question.

For me, 403 seems correct, because if user is already logged in, then He/She probably does not have admin account. What’s more, user should never see this unless I have made programming error or user hacks data sent through Ajax.

One more thing to notice: user is not shown the 401 or 403 status codes: they are used by Ajax and JavaScript to show appropriate error messages. If 401 is received, JavaScript shows text: “It seems that You are not logged in to server. Please open up new window and log in to continue. After You have logged in, click here to check connection.”. If 403 is received, text is shown: “It seems that You do not have sufficient rights to complete this operation.”.

Am I using those error codes correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:17:47+00:00Added an answer on May 24, 2026 at 5:17 am

    Your return code is not important at all, because it’s AJAX request so you are able to return anything you want. You return error code then process it using JS then return message to user. Example user click on button delete, he/she may do not have permission to do this, so you return json: status: error, type: permission. Then display to user the message belong to this error type.

    You should combine 1 and 2 together. Write a function or class to check their command. Example:

    $check = $session_check(USER_COMMAND);
    if ($check) //has right
    else //no right.
    
    function session_check($command){
         $userCmdList = array(COMMAND_1,2,3,4);
         $modCmdList = array(COMMAND_2,3,4,5,6,7);
         switch ($_SESSION['group_id']){
              case "admin":
                   return true;
                   break;
              case "mod":
                   if !in_array($command,$modCmdList) return false;
                   else return true;
                   break;
              case "user":
                   if !in_array($command,$userCmdList) return false;
                   else return true;
                   break;
              default:
                   break;
        }
    }
    

    Also remember to use a unique hash for each session to prevent session hjhack

    P/S: This is simple answer that follow your current work process. Other improved methods are always available.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best data grid for php based application using ajax? Any reference
I have a small ajax application built with php. Using phpMyAdmin I have set
I have a small AJAX application, written in PHP that I did not secure
I have a basic ajax application, which will not work, instead the php code
I have been working on on an AJAX chat application using php, mysql. It's
i just completed a web based chat application based on ajax/php. But the problem
I am working on a php that application that uses ajax. On all the
I am converting a javascript/php/ajax application to use jQuery to ensure compatibility with browsers
I'm writing a PHP application which uses AJAX to submit forms via POST when
I'm using Mathjax to display equations in a web application done in PHP/Ajax. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.