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 647447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:43:24+00:00 2026-05-13T21:43:24+00:00

Original Question: i read that for RESTful websites. it is not good to use

  • 0

Original Question:

i read that for RESTful websites. it is not good to use $_SESSION. Why is it not good? how then do i properly authenticate users without looking up database all the time to check for the user’s roles?


I read that it is not good to use $_SESSION.

http://www.recessframework.org/page/towards-restful-php-5-basic-tips

I am creating a WEBSITE, not web service in PHP. and i am trying to make it more RESTful. at least in spirit.

right now i am rewriting all the action to use Form tags POST and add in a hidden value called _method which would be “delete” for deleting action and “put” for updating action.

however, i am not sure why it is recommended NOT to use $_SESSION. i would like to know why and what can i do to improve.

To allow easy authorization checking, what i did was to after logging in the user, the username is stored in the $_SESSION.

Everytime the user navigates to a page, the page would check if the username is stored inside $_SESSION and then based on the $_SESSION retrieves all the info including privileges from the database and then evaluates the authorization to access the page based on the info retrieved.

Is the way I am implementing bad? not RESTful? how do i improve performance and security?

Thank you.

  • 1 1 Answer
  • 1 View
  • 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-13T21:43:24+00:00Added an answer on May 13, 2026 at 9:43 pm

    As written in the article, this “rule” is utter nonsense. The author is damning $_SESSION, but is perfectly okay with cookies storing authentication information. He goes on to say:

    In the event you need more than a cookie’s worth of data fall back to storing it in a central database with the authentication still in the cookie.

    What’s the difference between storing data in a session with a token in the cookie, and storing data in a database with authentication in the cookie? There is no difference, other than that by using a token, you’re not transmitting authentication data, possibly in the clear, with every request.

    There is no difference between transmitting additional information with each request and transmitting a token that represents additional data, but needs to be resolved through a session on the server. It’s just a matter of security and practicality.

    The argument is often that a server should be “stateless”. Since RESTfulness pertains to the HTTP protocol though, “statelessness” does not mean “the server doesn’t store any state”. Statelessness from a protocol’s perspective means that I can make any number of requests in arbitrary order, and I receive the same resource for the same request.

    GET  /index.html
    POST /someaction
    GET  /index.html  -> should return the same *resource* as before
    

    Contrast that with a real state-keeping protocol such as FTP:

    LS       -> gets list of files in current directory
    CD /dir  -> changes directory, i.e. changes state
    LS       -> same command gets list of files for a different directory
    

    That’s the real difference between a RESTful protocol and a state-keeping protocol. Whether the server stores any data pertaining to the user or not is totally an implementation detail of the server and has nothing to do with RESTfulness. If the server returns the same resource as response to the exact same request, regardless of what other kinds of requests were made in between, it’s stateless and thereby RESTful.

    This doesn’t have anything to do with authentication or storing additional data and also does not preclude that requests or URLs may eventually expire.

    If URLs/requests do expire, there’s a special way to handle that using HTTP: responding with appropriate status codes. If a user sends a request with an expired token/login, the server is not supposed to answer with a login screen at the requested URL. That would violate RESTfulness.

    not RESTful:

    GET /restricted/page
    
    200 OK
    
    Please log in here:
    Name: _____
    Password: _____
    
    ----------------------
    
    POST /restricted/page
    [name, password]
    
    200 OK
    
    Content of restricted page.
    
    ----------------------
    
    GET /restricted/page
    
    200 OK
    
    Content of restricted page.
    

    RESTful:

    GET /restricted/page
    
    401 Unauthorized
    

    or

    GET /restricted/page
    
    307 Temporary Redirect
    Location: /login
    
    ----------------------
    
    GET /login
    
    200 OK
    
    ----------------------
    
    POST /login
    [name, password]
    
    307 Temporary Redirect
    Location: /restricted/page
    
    ----------------------
    
    GET /restricted/page
    
    200 OK
    

    This does not “replace” the resource /restricted/page as the bad example would do, keeping the server RESTful. It does appropriately signal to the client that the request is valid, just not right now. Note that the term resource is always used, not response. It’s okay for the server to respond differently, but it’s not okay to offer a different resource (content*) at the same URL. If that was the case, the client would also need to keep track of the current state of a session (like FTP) to be able to tell what’s going on. Statelessness is much more about the client being stateless than the server. It doesn’t preclude the server from keeping track of what the client is doing.


    *) Note that content is not equivalent to resource. It’s okay for the content of a resource to change and update.

    Further note that there are valid reasons against using $_SESSION, most notably scalability across several servers. Keeping a server RESTful is not a valid reason though. If you need any sort of state, like expiring logins or a shopping basket, you need to keep track of that information somewhere. A server session is as valid a place as a cookie, and is in many cases the better choice. Whether that session is implemented using $_SESSION or a database or pen and paper is an implementation detail.

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

Sidebar

Related Questions

Edit: original question below, but I revise it now that I have some code
Original Question I've read at least a dozen articles and SO threads on events
(please read the update section below, I leave the original question too for clarity)
I have read tutorials and several stack overflow posts that suggest I should use
UPDATED - please read further details below original question I have a select form
I read that importing a module's mutable objects and changing them in-place,affects the original
[Edit #3] - to anyone reading this question: do not under any circumstance use
EDIT Apologies if the original unedited question is misleading. This question is not asking
Original question: Note: Below plugin pattern based on the official jQuery docs . I'm
Original question: The polysemy of a word is the number of senses it has.

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.