Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php storing user id in session?

I am wondering what the risks are of storing the userid in a session?

then simply doing a

if(isset($_SESSION['user_id'])){
  login_user($_SESSION['user_id]);
}

Are sessions encrypted enough that we wont have to worry about hashing them? What are the chances of someone being able to change their ID?

like image 467
Hailwood Avatar asked Apr 20 '11 00:04

Hailwood


People also ask

How to get ID in session in PHP?

Before getting a session id you need to start a session and that is done by using: session_start() function. Now that you have started a session you can get a session id by using: session_id().

How can we store variable in session in PHP?

Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session. The following example starts a session then register a variable called counter that is incremented each time the page is visited during the session.

What is PHPsessid?

PHPSESSID – The PHPSESSID cookie is native to PHP and enables websites to store serialised state data. It is used to establish a user session and to pass state data via a temporary cookie, which is commonly referred to as a session cookie. (

What does$_ session mean in PHP?

Start a PHP Session A session is started with the session_start() function. Session variables are set with the PHP global variable: $_SESSION.


2 Answers

The session is by default stored in /tmp as a file. It is not viewable by the end user unless you have security issues such as directory traversal vulnerabilities.

The only portion the client sees is the unique hash stored in a cookie which maps to the relevant session on the server.

like image 107
alex Avatar answered Sep 30 '22 20:09

alex


Most applications use $_SESSION as you are. If there where a wide spread weakness then major projects would be doing things differently.

like image 20
rook Avatar answered Sep 30 '22 19:09

rook