简体   繁体   English

将PHP会话变量保存为局部变量

[英]Saving PHP Session variables to local variables

Im having a big problem, i am generating content with my php script. 我有一个大问题,我正在用我的PHP脚本生成内容。 I pass some text-input to my generating script. 我将一些text-input传递给生成的脚本。 This script is putting all text-input to the $_SESSION variable. 该脚本将所有text-input放入$_SESSION变量。

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. 在生成的页面中,我将这些$_SESSION变量放入本地变量。

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. 仅HTML标记仍然存在。 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(); 我现在的问题是,如何在session_destroy();之后保存这些会话变量而不丢失它们session_destroy(); ?

Ps: Im using a MySQL-Database but im not so talented with it. 附:我使用的是MySQL数据库,但我不是那么有才。

Session var are destroyed after session_destroy, it is how it works. 会话var在session_destroy之后被销毁,这就是它的工作方式。 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. 您可以将所需的内容保存在mysql DB中,以文件(json格式),mongodb,memcached,redis ... ...您也可以将cookie用于简单和不安全的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. 但是用数据库来做会更好。

A solution could be to store them in the MySQL Database. 一种解决方案是将它们存储在MySQL数据库中。 Use PDO connector to insert rows in your tables : [Doc Here] 使用PDO连接器在你的表中插入行: [文档这里]

And Insert like this : 然后像这样插入:

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

What is the context of the application ? 应用程序的上下文是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM