Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP $_SESSION Variables

Tags:

php

session

I am using PHP $_SESSION variables with the login workflow of my website and I just wanted to make some clarifications. Much like Facebook, I want to store a secret code only known by the server which is used to sign each request that is sent to and from the server. My initial approach was to generate a random string and store that inside of a MySQL table, but then I learned about session variables. I know that session variables by default work by using cookies that store session names and id, correct? None of the actual data is stored on the user's computer? So if I wanted to implement:


# assume that $rand_string is not null and a string
session_start();
$_SESSION['secret'] = $rand_string;

there would not be any way for the user to decode the session cookies and determine the actual value of $rand_string, right? Just want to make sure the data is secure, otherwise I will revert back to the less smooth MySQL technique. I just like the thought of the easily accessed and managed session variables.

like image 722
NoodleOfDeath Avatar asked Aug 24 '26 09:08

NoodleOfDeath


2 Answers

Session data is stored server-side.

Cookie data is stored client-side.

like image 134
Abhishek Kannan Avatar answered Aug 26 '26 22:08

Abhishek Kannan


I would prefer doing the random stuff by generating a guid` function, because it will generate a unique identifier and will be more secure than a simple random:

# assume that $rand_string is not null and a string
session_start();
$_SESSION['secret'] = com_create_guid();

And yes, $_SESSION variables are stored on server side.

like image 41
Skatox Avatar answered Aug 26 '26 21:08

Skatox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!