【问题标题】:Change session directory PHP更改会话目录 PHP
【发布时间】:2018-11-01 18:20:30
【问题描述】:

我正在运行本地 apache 服务器并具有以下文件夹结构:

localhost
    site 1
        index.php
    site 2
        index.php

现在,当我在 site 1/index.php 中创建会话时,它也会为 site-2 创建它。我怎样才能将它们分开?

【问题讨论】:

  • 使用不同的域将是最简单的 imo。否则,您可能需要在站点 1 和站点 2 的每个页面上使用 php.net/manual/en/function.session-save-path.php 来设置相关路径。
  • 会话默认基于站点,但您已在单个站点中实现文件夹。由于您在 localhost 上运行,因此您需要为 site1.localhostsite2.localhost 等内容添加 Apache vhost 条目

标签: php session


【解决方案1】:

如果您将其发布到网络上,最简单的解决方案是发布到不同的域:

但是如果你想在 localhost 上做:

这可能会有所帮助:

PHP How can I create multiple sessions?

【讨论】:

    【解决方案2】:

    如前所述 - 最简单的方法是尝试将它们设置为具有不同域名的虚拟主机:

    我将使用这两个作为我的示例:

    testsite1.local
    
    testsite2.local 
    

    ( * 注意,把它放在 Apache 的 .conf 文件中。根据你的本地环境进行修改。如果在 linux 上 - /var/www/testsite1/var/www/testsite2 --- 或者你保存文件的任何地方。我的配置来自 Xampp窗户)

    <VirtualHost testsite1.local:80>
        ServerAdmin webmaster@testsite1.local
        DocumentRoot "c:/xampp/htdocs/testsite1"
        ServerName testsite1.local
    </VirtualHost>
    
    <VirtualHost testsite2.local:80>
        ServerAdmin webmaster@testsite2.local
        DocumentRoot "c:/xampp/htdocs/testsite2"
        ServerName testsite2.local
    </VirtualHost>
    

    将本地域添加到本地路由(如果在 Windows 上,则 System32 中的主机文件:/windows/system32/drivers/etc/hosts - 或在 Linux 上 - /etc/hosts

    127.0.0.1 testsite1.local
    
    127.0.0.1 testsite2.local 
    

    重启你的 Apache。

    您的服务器应自动将会话与适当的域相关联,因为与 cookie 不匹配的域不允许使用存储会话的 cookie。

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 2015-05-23
      • 2023-04-08
      • 2014-12-04
      • 2011-09-28
      相关资源
      最近更新 更多