Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad using $_SESSION['id'] in queries?

Tags:

php

mysql

When person logins, he gets $_SESSION['id'] and it becomes his id taken from mysql table. Then I do mysql queries like SELECT * FROM members WHERE member_id = {$_SESSION['id']}.

So, is it safe? Can $_SESSION['id'] disappear or could hacker edit it somehow?

Thank you.

like image 821
good_evening Avatar asked Apr 06 '11 14:04

good_evening


People also ask

Is session variable safe?

No. Generally, session data is only stored server-side and should not be readable by an attacker.

Is session id sensitive?

Session IDs are sensitive information that may allow an attacker to steal, modify and/or destroy information once they obtain one. Information sent via URL parameters is: Stored in clear text in the browser history. Sent to external sites via the referrer HTTP header.

Is PHP session reliable?

“Is a PHP session secure? PHP sessions are only as secure as your application makes them. PHP sessions will allow the client a pseudorandom string (“session ID”) for them to distinguish themselves with, but on the off chance that the string is intercepted by an attacker, the aggressor can imagine to be that client.

What can you do with a PHP session id?

PHP allows us to track each visitor via a unique session ID which can be used to correlate data between connections. This id is a random string sent to the user when a session is created and is stored within the user's browser in a cookie (by default called PHPSESSID).


2 Answers

I would argue that it is always bad to create a query by simply inserting or concatenating variables. Instead, you should use a Prepared Statement that will guarantee protection against SQL Injection type attacks. IMHO, they also make code look better.

like image 111
Kevin Avatar answered Oct 20 '22 01:10

Kevin


In theory, there is no way that clients can affect $_SESSION array, beacuse session data is stored on the server. But in practice, never trust this, because hacker can use another security hole and substitude $_SESSION['id'] with something bad.

Do this, before putting id into your query:

$_SESSION['id'] = intval($_SESSION['id']);
like image 37
Silver Light Avatar answered Oct 19 '22 23:10

Silver Light