在每个请求上重置PHP会话 [英] PHP session reset on every request

查看:98
本文介绍了在每个请求上重置PHP会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

距离我尝试进行这项工作已经过去了几天.我在使用PHP会话时遇到问题.服务器是 nginx .我有三个子目录(子域):

Its have been few days since I'm trying to get this work. I have problem with PHP sessions. Server is nginx. I have three sub-directories (sub-domains):

  • app.dev/(有前端,客户访问什么)
  • backend.app.dev/(处理AJAX请求并与API通信的php页面)
  • api.app.dev/(API)
  • app.dev/ (there is frontend, what client visit)
  • backend.app.dev/ (php page which proccessing AJAX requests, and communicates with API)
  • api.app.dev/ (API)

app.dev/中,有一个template/文件夹,其中包含PHP文件,例如表单,标题和网站的某些部分,这些文件根据会话数据进行填充.通过jQuery.load()函数将其加载到index.html页面中.

In app.dev/ there is template/ folder which contains PHP files like forms, header, and parts of site which get populated by depending of session data. This is loaded into index.html page via jQuery.load() function.

backend.app.dev/上,有page.php文件用于处理数据并将其发送到API.

On backend.app.dev/ there is page.php file which process data, and send to API.

然后,当我在登录操作中发送AJAX请求时,以及从API获取令牌后,我需要将其存储到page.php上的会话中.在该请求中,它成功存储了该请求,但是,当我发送第二个请求时,该检查中的会话中是否有令牌,它表示该会话为空.我尝试打印整个$_SESSION变量,但它为空. $_COOKIE也.因此,每次请求后会话都将被丢弃.我用谷歌搜索了很多.我尝试了许多解决方案(我将发布已经尝试过的解决方案的日志.)并且我发现在/var/lib/php/sessions中的每个请求之后都创建了文件.

And then, when I send AJAX request on login action, and after I get token from API, I need to store it into session on page.php. At that request, it stores it successful, but, when I send second request, which check is there a token in session, it says that session is empty. I tried print whole $_SESSION variable, and it's empty. $_COOKIE also. So session get dropped after every request. I googled a lot. I tried many solutions ( I will post a log of solutions that I've tried.) And I found that there is files created after every request in /var/lib/php/sessions.

我尝试在每个请求上打印会话ID,但总是不同的...

I tried to print session id on every request, and it's always different...

从日志文件中,多次发送相同的请求:

From log file, send the same request few times:

[28.Sep.2017 04:39:12]: session id: c0556q1s47kger3l53tgrdadhu

[28.Sep.2017 04:39:29]: session id: 83nmqq1l9d27v7jhh7748gq2ru

[28.Sep.2017 04:39:33]: session id: ece5as0eb0egoolun8u9s0duqi

我尝试过的日志是:

//session_name("backend");
//session_set_cookie_params(0, '/', '.dev');
//session_start();

//session_name('backend');
//ini_set('session.cookie_domain', 'hr.dev');
//ini_set('session.save_path', '/var/lib/php/session');
//ini_set('session.cookie_domain', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
//session_start();

//session_name("all");
//$sess_life_time = 21600; //in seconds
//$sess_path = "/";
//$sess_domain = ".dev";
//$sess_secure = true; // if you have secured session
//$sess_httponly = true; // httponly flag

//ini_set('suhosin.session.cryptdocroot', 'off');
//ini_set('suhosin.cookie.cryptdocroot', 'off');

//session_set_cookie_params($sess_life_time, $sess_path, $sess_domain, $sess_secure, $sess_httponly);

//session_name("all");
//ini_set("session.cokie_domain",".dev");
//session_set_cookie_params(0, '/', '.dev');
//session_start();

//  session_name("mysession");
//  session_set_cookie_params(0,"/",".dev",FALSE,FALSE);
//  setcookie(session_name(), session_id(),0,"/",".dev");
//  session_start();

可能是什么问题?服务器配置文件上缺少什么?

What could be the problem? Is something missing on the server conf?

如果我们尽快解决此问题,我将非常感谢.

I will be really, really thankful if we fix this as soon as possible.

我在backend.app.dev/

I set session variables (start session) at the backend.app.dev/

php.ini信息:

session.auto_start => Off => Off
session.cache_expire => 180 => 180
session.cache_limiter => nocache => nocache
session.cookie_domain => no value => no value
session.cookie_httponly => Off => Off
session.cookie_lifetime => 0 => 0
session.cookie_path => / => /
session.cookie_secure => Off => Off
session.gc_divisor => 1000 => 1000
session.gc_maxlifetime => 1440 => 1440
session.gc_probability => 0 => 0
session.lazy_write => On => On
session.name => PHPSESSID => PHPSESSID
session.referer_check => no value => no value
session.save_handler => files => files
session.save_path => /var/lib/php/sessions => /var/lib/php/sessions
session.serialize_handler => php => php
session.sid_bits_per_character => 5 => 5
session.sid_length => 26 => 26
session.upload_progress.cleanup => On => On
session.upload_progress.enabled => On => On
session.upload_progress.freq => 1% => 1%
session.upload_progress.min_freq => 1 => 1
session.upload_progress.name => PHP_SESSION_UPLOAD_PROGRESS => PHP_SESSION_UPLOAD_PROGRESS
session.upload_progress.prefix => upload_progress_ => upload_progress_
session.use_cookies => On => On
session.use_only_cookies => On => On
session.use_strict_mode => Off => Off
session.use_trans_sid => 0 => 0

推荐答案

经过这么长时间,我终于解决了这个问题.我将API保留在单独的域中,但是将前端和后端集成在同一个域中,但位于不同的子文件夹中.所以现在我有:

I've finally solved this problem, after so much time. I kept API on separated domain, but had integrated front-end and back-end at the same domain but in different sub-folders. So now i have:

  • api.app.dev

app.dev (前端)

app.dev/backend

和(本机)会话现在像魅力一样工作.我在服务这两个子目录时也遇到了麻烦,因此我打开了新线程.我使用的nginx设置是:

And (native) sessions now working like charm. I also had troubles with serving these two sub-directories, so i've opened new thread. There is nginx settings whose i used:

在同一域中服务多个站点

谢谢大家!

这篇关于在每个请求上重置PHP会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆