1

I was wondering if it was a good practice to store the infos we retrieved from a DB (more specificaly the user infos) inside a session, useful for the login for example :

e.g :

$sql="SELECT * FROM USER";
$result=$myConnexion->query($sql);
$line=$result->fetch_assoc();

$_SESSION['login']=$line['login'];
$_SESSION['name']=$line['name'];
...

It then would be easier to display infos, welcome messages and so on. What do you think ?

Thank you for your time

Mxsky
  • 749
  • 9
  • 27
  • 2
    yes its perfectly fine....!! it also reduces hassel of querying the database again and again for info you want at several places..... – Manoj Salvi Oct 28 '15 at 13:38
  • 1
    yes it´s ok. I would only suggest that you use a multiple array, so you / or other can better maintain the code. `$_SESSION['usertable'][‘login']=$line['login']; $_SESSION['usertable'][‘name']=$line['name'];` – FlorianR. Oct 28 '15 at 13:39

2 Answers2

2

It's perfectly fine. Make sure you only store Public info you might need later on, meaning don't store the password or the user's ID in the db.

Ralph Melhem
  • 767
  • 5
  • 12
1

Yes, it's fine, as long as you don't store sensitive data, like the password, or the user name. You can check this for more Using PHP SESSION Variables to store MySQL query results

Community
  • 1
  • 1
Ash
  • 61
  • 5