I work with sessions in PHP and I have different applications on the same domain. The problem is that cookies are domain specific, so session IDs are sent to any page in the same domain. (I don't know if there is a way to make cookies work differently). In this way, session variables are visible on every page of this domain. I am trying to implement my own session manager to overcome this behavior, but I'm not sure if I think about it correctly.
I want to completely exclude the PHP session system and create a global object that will store the session data, and at the end of the script store it in the database.
- On first access, I will create a unique session_id and create a cookie
- At the end of the script, save the session data with session_id, timestamps for the start of the session and last access, and data from $ _SERVER, such as REMOTE_ADDR, REMOTE_PORT, HTTP_USER_AGENT.
- In each access chceck database for session_id sent to the cookie from the client, check the IP, port and user agent (for security) and read the data in the session variable (if not expired).
- If session_id has expired, delete from the database.
This session variable will be implemented as singleton (I know that I am getting a tight connection with this class, but I do not know about a better solution).
I am trying to get the following benefits:
- Session variables invisible in other scenarios on the same server and in the same domain
- User session expiration control
- A way to view open sessions (something like a list of online users)
I am not sure that I do not notice any flaws in this solution. Is there a better way?
Thanks!
UPDATE: I did not explain this in sufficient detail and caused a lot of confusion here, so I want to clarify what I mean:
I am creating an SOA server application that will be deployed in many different environments. It will not have its own web server, so there may be other PHP applications in those environments. Employees of these companies will have user accounts in this application, so they will receive a cookie with a session identifier in this application.
As you know, a web server working with PHP when loading session data does not differ (at least by default) that the script from which the directory is created. All he needs is a session identifier. This session identifier is sent with each request from the client to the server. From your answers, I got a way how PHP can limit cookies to a specific directory, but a malicious user can edit cookies because it is stored on his computer. In my case, a malicious user may have write and execute PHP script access in the same environment, although he does not have access to my application and its database. If he creates a script, he can use the session identifier from the cookie of my application, so he has access to read and edit session data in my application and gain access to parts of my application that he should not allow.
I see that when deploying the application in such an environment other security risks will appear, what I'm going to do is the best isolation that I could do, and processing sessions by default seems too dangerous and not intended for such purposes.
So my question is: if you see something that is less secure and less flexible in my design than it would be with session management by default.
Thank you for your responses,..