1

I'm running a local apache server and have this folder structure:

localhost
    site 1
        index.php
    site 2
        index.php

Now, when I create a session in site 1/index.php it also creates it for site-2. How can I separate them?

nick
  • 2,819
  • 5
  • 33
  • 69
  • Use different domains would be the simplest imo. Otherwise you'll probably need to use http://php.net/manual/en/function.session-save-path.php on every page in site 1 and site 2 to set the relevant path. – Jonnix Nov 01 '18 at 18:23
  • Sessions are site based by default but you have implemented folders within a single site. Since you are running on localhost then you need to add an Apache vhost entry for stuff like `site1.localhost` and `site2.localhost` – MonkeyZeus Nov 01 '18 at 18:31

2 Answers2

1

The simplest solution if you publish this on the web is to different domains:

But if you want to do it on localhost:

This may help:

PHP How can I create multiple sessions?

0

As mentioned - the simplest way is to try setting them up as vhosts with different domain names:

I'll use these two for my example:

testsite1.local

testsite2.local 

( * Note, place this in your .conf file for Apache. Modify for your local envrionment. If on linux - /var/www/testsite1, /var/www/testsite2 --- or wherever you keep your files. My config is from Xampp on Windows )

<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>

Add the local domains to your local routing (if on Windows, the hosts file in System32: /windows/system32/drivers/etc/hosts - or on Linux - /etc/hosts )

127.0.0.1 testsite1.local

127.0.0.1 testsite2.local 

Restart your Apache.

Your server should automatically associate the sessions with the appropriate domains, as cookies where the sessions are stored are not allowed to be used by domains that do not match the cookie.

Kasem O
  • 64
  • 6