如何为基于webpack4的网站在php.ini中配置session.save_path? [英] How to configure session.save_path inside php.ini for webpack4 based website?

查看:50
本文介绍了如何为基于webpack4的网站在php.ini中配置session.save_path?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里收到有关我的整体问题的非常有用的信息: session_start()原因错误:没有这样的文件或目录(新鲜的,可移植的XAMPP)

I already received very helpful information on my overall problem here: session_start() causes error: No such file or directory (fresh, portable XAMPP)

我需要用于登录功能的会话变量,并且Ive已经找到了php.ini并配置了以下路径: session.save_path ="D:\ foundationtests \ src \ assets \ Session"

I need the session variables for my login functionality, and Ive already found my php.ini and configured the path to session.save_path = "D:\foundationtests\src\assets\Session"

因此它也位于我的服务器DocumentRoot之外.

It is thereby also located outside my servers DocumentRoot.

到目前为止,我的网站的行为并没有太大变化,我已经通过基础观察(使用基于webpack4,Babel7和Gulp的ZURB基础6.4框架)重建了项目,并且Session文件夹出现在dist中.文件夹.

So far, the behavior of my website hasn't changed much, and I've already rebuilt the project via foundation watch (using ZURB foundation 6.4 Framework based on webpack4, Babel7 and Gulp) and the Session folder appears in the dist folder.

在我设置了会话变量的php文件中,由于可以回显它,因此似乎可以访问它,我将为您提供一段简化的代码:

In the php file where I set the Session Variable, it seems to be accessible since I can echo it, I'll give you a simplified piece of code:

  session_start();

  $_SESSION["loggedUserID"] = 9;

  echo $_SESSION["loggedUserID"];

但是,执行此脚本后,当我想从另一个脚本访问Session变量时,似乎数据丢失了:

However, after executing this script, when I want to access the Session variable from another script, it seems the data was lost:

session_start();

echo $_SESSION["loggedUserID"];

我在浏览器控制台中收到以下错误消息.

I get the following errormsg in my browser console.

<br />
<b>Notice</b>:  Undefined index: loggedUserID in <b>D:\foundationtests\src\assets\php\globallyUsedFunctions\retrieveLoggedUserID.php</b> on line <b>4</b><br />

此外,我在会话文件夹中找不到php创建的任何文件.

Also, I cant find any files created by php inside the Session Folder.

但是,例如,我不确定是否正确配置了路径.毕竟,这是从中执行构建项目的dist文件夹.

However, for example I'm not sure if I'm configuring the path correctly. After all, it is the dist folder from which the built project is executed.

但是由于我对模块化网站项目和php/apache服务器的配置都还很陌生,所以我不知道这可能是什么原因... ^^

But since Im pretty new to both modular website projects and the configuration of a php/apache server, I have no idea what might be the cause of this...^^

我正在Windows 10计算机上从XAMPP运行我的服务器,而没有管理员权限.但是,至少作为用户,我可以在服务器和项目所在的D分区上创建新文件和内容.

I'm running my server from XAMPP, on a windows 10 machine without admin privileges. However, at least I as a user can create new files and stuff on the D partition both my server and my project are located at.

重新启动我的Web服务器之后,在我的会话文件夹中创建了会话文件.但是,当我在phpFile1上设置$ _SESSION ["indexname"]时,然后单击一个调用phpFile2的按钮,该按钮首先执行session_start();.然后尝试访问$ _SESSION ["indexname"]中的值,我从上面得到了错误. phpFile1创建的会话文件仍然存在,并且其中也包含数据,但是随后phpFile2创建了另一个会话文件,这个会话文件为空,因此我的第一个猜测是服务器访问了第二个空文件,当然可以找到没有.但是我该怎么做才能改变这一点.

After restarting my webserver, Session files were created inside my Session Folder. However, when I set $_SESSION["indexname"] on phpFile1, then click a button which calls phpFile2 which first does session_start(); and then tries to access the value inside $_SESSION["indexname"] I get back the error from above. The session file created by phpFile1 is still there and it also contains data, but then theres another session file created by phpFile2 and this one is empty and so my first guess would be that the server accesses this second, empty file and there of course finds nothing. But what can I do to change this.

当我从php文件中调用phpinfo()并将其回显到试图console.log()的前端时,我得到以下响应:

When I call phpinfo() from the php files and echo it to my front-end where I try to console.log() it, I get the following response:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #fff; color: #222; font-family: sans-serif;}
pre {margin: 0; font-family: monospace;}
a:link {color: #009; text-decoration: none; background-color: #fff;}
a:hover {text-decoration: underline;}
table {border-collapse: collapse; border: 0; width: 934px; box-shadow: 1px 2px 3px #ccc;}
.center {text-align: center;}
.center table {margin: 1em auto; text-align: left;}
.center th {text-align: center !important;}
td, th {border: 1px solid #666; font-size: 75%; vertical-align: baseline; padding: 4px 5px;}
h1 {font-size: 150%;}
h2 {font-size: 125%;}
.p {text-align: left;}
.e {background-color: #ccf; width: 300px; font-weight: bold;}
.h {background-color: #99c; font-weight: bold;}
.v {background-color: #ddd; max-width: 300px; overflow-x: auto; word-wrap: break-word;}
.v i {color: #999;}
img {float: right; bo…

我不知道为什么得到这个,这对我来说似乎是错的,因为它与php无关,但是我也不知道如何更改它.

I dont know why I get this, it seems wrong to me because it has little to do with php, but I also dont know how to change this.

session_get_cookie_params()var_dump() 根据misorude的要求.

The var_dump() of session_get_cookie_params() as per request of misorude.

array(6) {
  ["lifetime"]=>
  int(3600)
  ["path"]=>
  string(1) "/"
  ["domain"]=>
  string(14) "localhost:8000"
  ["secure"]=>
  bool(false)
  ["httponly"]=>
  bool(false)
  ["samesite"]=>
  string(0) ""
}

推荐答案

所以我终于做到了,这就是我必须做的全部工作! 我曾在最新的免安装XAMPP和ZURB Foundation 6.4 ZURB模板项目中工作.

So I finally made it, here's what I had to do overall! I worked in the latest, no-install XAMPP and on ZURB Foundation 6.4 ZURB template project.

要通过AJAX在域之间建立会话,必须进行以下配置:

To establish a session across domains via AJAX, one has to make the following configurations:

通过调用的php skripts设置标头,如下所示:

Set your headers through the called php skripts, like so:

Header("Access-Control-Max-Age: 360");
Header("Access-Control-Allow-Credentials: true");
Header("Access-Control-Allow-Methods: *");
Header("Access-Control-Allow-Headers: Origin");
Header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");
Header("Access-Control-Allow-Origin: "http://localhost:PORT FROM WHICH AJAX CALL IS  PERFORMED");

您也可以在目录部分的httpd.conf中执行此操作,如下所示:

You can also do this inside your httpd.conf in your directory section, like so:

标头集Access-Control-Allow-Origin" http://localhost:8000 "

Header set Access-Control-Allow-Origin "http://localhost:8000"

然后,您需要进入php.ini并为会话配置以下内容: session.save_path ="D:\ Path \ To \ src \ Session"

Then, you need to go into your php.ini and configure the following for your session: session.save_path = "D:\Path\To\src\Session"

然后,假设您将其他所有内容保留为默认设置,则需要配置以下cookie参数,如下所示:

Then, assuming you left everything else in default setting, you need to configure the following cookie parameters like so:

session.cookie_lifetime = 3600
session.cookie_domain = localhost 

(如果您使用的是localhost域)

(if your working with localhost domain)

然后,如果您使用AJAX,则必须手动设置要进行提交的对象以提交凭据,否则浏览器不会跨域发送Cookie,就像这样:

Then, if youre using AJAX, you have to manually set the object doing the invokation to submit credentials, otherwise browsers dont send cookies across domains, like so:

   $.ajax({
      type:"POST",
      url: 'http://localhost:8099/test3.php',
      xhrFields: {
     withCredentials: true
     }
  }).then((response) => {
    console.log(response)
  })
}

xhrFields-withCredentials在这里非常重要!如果未将其设置为true,则不会在任何域之间传输Cookie.

The xhrFields - withCredentials is the very important part here! If its not set to true, no cookie will be transmitted across domains.

这篇关于如何为基于webpack4的网站在php.ini中配置session.save_path?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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