frosty
frosty

Reputation: 69

Saving PHP Session variables to local variables

Im having a big problem, i am generating content with my php script. I pass some text-input to my generating script. This script is putting all text-inputto the $_SESSION variable.

For example :

$_SESSION[text1] = $text1;
$_SESSION[text2] = $text2;

and so on...

In the generated page i take those $_SESSION variables and put them to local variables.

For example :

$text1 = $_SESSION['text1']
$text2 = $_SESSION['text2']

and so on...

But after i destroy the session (Cause of login/logout system) all the content on the generated site is gone. Only the HTML tags are still there. So that means for me, all session variables get empty after destroying the session.

My question now is how do i save those session variables without loosing them after a session_destroy();?

Ps: Im using a MySQL-Database but im not so talented with it.

Upvotes: 1

Views: 1250

Answers (2)

neoteknic
neoteknic

Reputation: 1970

Session var are destroyed after session_destroy, it is how it works. You can save what you want in mysql DB, in a file (json format), mongodb, in memcached, in redis ... You can also use cookies for simple and non secure var. A very simple thing is to save it in a file :

file_puts_content('filename.json',json_encode($_SESSION));

and to get it back

$_SESSION=json_decode(file_gets_content('filename.json'),true);

But it's much better to do it with a database.

Upvotes: 2

Adrien QUINT
Adrien QUINT

Reputation: 43

A solution could be to store them in the MySQL Database. Use PDO connector to insert rows in your tables : [Doc Here]

And Insert like this :

INSERT INTO my_table(`var1`,`var2`,`var3`) VALUES($val1,$val2,$val3);

What is the context of the application ?

Upvotes: 0

Related Questions