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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:07:45+00:00 2026-05-13T13:07:45+00:00

My web application uses UserId almost throughout the entire application.. what is the most

  • 0

My web application uses UserId almost throughout the entire application..

  • what is the most efficient way to secure a session variable in php?

  • Is session vulnerable to attacks?

  • Should i keep my encrypted value of UserId in session?

Any suggestion…

  • 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-13T13:07:46+00:00Added an answer on May 13, 2026 at 1:07 pm

    Note: Taken from my previous answer.

    Terminology

    • User: A visitor.
    • Client: A particular web-capable software installed on a particular machine.

    Understanding Sessions

    In order to understand how to make your session secure, you must first understand how sessions work.

    Let’s see this piece of code:

    session_start();
    

    As soon as you call that, PHP will look for a cookie called PHPSESSID (by default). If it is not found, it will create one:

    PHPSESSID=h8p6eoh3djplmnum2f696e4vq3
    

    If it is found, it takes the value of PHPSESSID and then loads the corresponding session. That value is called a session_id.

    That is the only thing the client will know. Whatever you add into the session variable stays on the server, and is never transfered to the client. That variable doesn’t change if you change the content of $_SESSION. It always stays the same until you destroy it or it times out. Therefore, it is useless to try to obfuscate the contents of $_SESSION by hashing it or by other means as the client never receives or sends that information.

    Then, in the case of a new session, you will set the variables:

    $_SESSION['user'] = 'someuser';
    

    The client will never see that information.


    The Problem

    A security issue may arise when a malicious user steals the session_id of an other user. Without some kind of check, he will then be free to impersonate that user. We need to find a way to uniquely identify the client (not the user).

    One strategy (the most effective) involves checking if the IP of the client who started the session is the same as the IP of the person using the session.

    if(logging_in()) {
        $_SESSION['user'] = 'someuser';
        $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
    }
    
    // The Check on subsequent load
    if($_SESSION['ip'] != $_SERVER['REMOTE_ADDR']) {
        die('Session MAY have been hijacked');
    }
    

    The problem with that strategy is that if a client uses a load-balancer, or (on long duration session) the user has a dynamic IP, it will trigger a false alert.

    Another strategy involves checking the user-agent of the client:

    if(logging_in()) {
        $_SESSION['user'] = 'someuser';
        $_SESSION['agent'] = $_SERVER['HTTP_USER_AGENT'];
    }
    
    // The Check on subsequent load
    if($_SESSION['agent'] != $_SERVER['HTTP_USER_AGENT']) {
        die('Session MAY have been hijacked');
    }
    

    The downside of that strategy is that if the client upgrades it’s browser or installs an addon (some adds to the user-agent), the user-agent string will change and it will trigger a false alert.

    Another strategy is to rotate the session_id on each 5 requests. That way, the session_id theoretically doesn’t stay long enough to be hijacked.

    if(logging_in()) {
        $_SESSION['user'] = 'someuser';
        $_SESSION['count'] = 5;
    }
    
    // The Check on subsequent load
    if(($_SESSION['count'] -= 1) == 0) {
        session_regenerate_id();
        $_SESSION['count'] = 5;
    }
    

    You may combine each of these strategies as you wish, but you will also combine the downsides.

    Unfortunately, no solution is fool-proof. If your session_id is compromised, you are pretty much done for. The above strategies are just stop-gap measures.

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

Sidebar

Related Questions

I'm working on a web application that currently uses a Session to hold the
My web application uses a lot of <a href=#>Perform client script action</a> anchor links
I have a web application uses a proxy to invoke an operation on a
In my web application that uses Apache Shiro for AuthC and AuthZ I have
I have a web application which uses the globalization values being passed from sql
I have a web application that uses fixed css width. (1280px). With jquery, I
I have a web application that uses TONS of javascript, and as usual, there
I have a web application that uses AzMan authorization to grant different functionality to
I have a web application that uses two databases. DB1 Users perform their CRUD
I have a web application that uses the CDO Message object to email reports.

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.