How to access php sessions on different computers

I am doing some exercises on using sessions in php. I configured it like this:

$_SESSION['log_users'][] = array(array('Username'=>$username)) 

I tried to experiment. And it turned out that the created session is different when I use a different ip to access it. When using the same browser that is firefox.

Here is what I did:

  • Set up my router so that others can access the exercise I'm working on using my external IP address.
  • Then I opened the local version of the exercise:

    http: //localhost/exercise/sessions.php

  • Then one that uses the external IP address is used:

    http://201.xxx.xxx/exercise/sessions.php

  • Then I populated an array of sessions on each browser tab. And it turned out that each of these two supports a different version of the session. Detected with print_r($_SESSION['log_users'])

Is this really how it should behave? Can I make sure that there is only one version of the session? I am currently using Wampserver 2.1

+4
source share
3 answers

The session is stored on the server side, and a session cookie is created on the client side to identify the current browser session that contains the current session identifier.

The session cookie is based on the domain that you use to access the site.

Since you are using a different domain, this is localhost and the other is ip , which will create two different sessions.

When visiting pages through the localhost domain. It will create a session and save the session cookie in the localhost domain. If you visit another page on the same domain system, check if the session cookie exists, it resumes the old session and does not create a new one.

At the same time, if you access via ip , the session cookie is not saved for that ip , and then the system assumes that there is no active session for this user, and he will start a new session, and the session cookie will be saved for based on this ip .

This is how the session works.

Hope this helps.

+4
source

The session cookie is bound to a domain name. The first time you access it, it will be bound to the localhost domain.

If you then point your browser to 201.xx.xx.xx, the domain name will no longer match. And your browser will no longer send this cookie. This is why a new session will be created. Even if it is actually the same server.

+2
source

In your case, only $ _SESSION will not help you. You should also try with $ _SESSION and Database.

You must synchronize the session and the database session record.

The system will check your entry in the database. If you have write access, it will directly generate a session for your site. Thus, only one login can be available for all browsers.

0
source

All Articles