Skip to main content
December 21, 2010
Question

session_start(); | session_destroy();

  • December 21, 2010
  • 1 reply
  • 2074 views

Hello, everyone:

I have created in my database that when someone logs in that their "session_start()" gets logged into their user account.  I also made that when a user logs out that their session is destroyed "session_destroy()".  Session_start() produces a UUID such as this computer of numbers and letters "1c57e7291a2de14d0fe08baaee1eba4b".  What I would like to happen is that if the user logs out not only is their session is destroyed but also the UID is terminated so that if the user logs back on they do not still have the same session of "1c57e7291a2de14d0fe08baaee1eba4b" but changes it to another and different UID session.  Is this possible rather than a new session only starts when a Internet browser is closed down and reponed?

Thank you.

This topic has been closed for replies.

1 reply

David_Powers
Inspiring
December 22, 2010

AdonaiEchad wrote:

What I would like to happen is that if the user logs out not only is their session is destroyed but also the UID is terminated so that if the user logs back on they do not still have the same session of "1c57e7291a2de14d0fe08baaee1eba4b" but changes it to another and different UID session. 

The PHP Manual page for session_destroy() shows the code you need to destroy the session ID.

However, this works only if the user actually logs out. To ensure that a new session ID is generated each time a person logs in, you need to use session_regenerate_id(). The Log In User server behavior in Dreamweaver CS5 was updated to regenerate the session ID automatically after a user's credentials have been authenticated.

December 23, 2010

Thank you David, that helps a lot.

David, is there a possibility that lets say a user logs in and in my database under a user account I have setup a tinyint "0" for offline and "1" for online.    Now I setup already that if the user makes the webapge idle to log them out.  However, how can I log someone out if they click on the close button on their web browser?

So my main objective is that if the browser is idel it logs the user out and regenerates and destorys the session UID.  The additional objective is that if a user closes their browser without signing out or logging out that the code in the webpage will update the database that the user is now offline.  Is this possible?

David_Powers
Inspiring
December 23, 2010

AdonaiEchad wrote:

So my main objective is that if the browser is idel it logs the user out and regenerates and destorys the session UID.  The additional objective is that if a user closes their browser without signing out or logging out that the code in the webpage will update the database that the user is now offline.  Is this possible?

Using Ajax, you could probably use the onUnload event on the <body> tag to send a request to update the database and log out the user. However, that assumes that the user has JavaScript enabled in the browser, and that the page is actually closed. So, it's not 100% reliable.

Since you seem to be concerned about a page being idle for a given time, It's probably a lot simpler to create a session variable that records the time when a user logs in. On each page, compare the value of the session variable with the current time. If it's within your time limit, update the session variable to the current time. If it's beyond the time limit, destroy the session, and log out the user.

I'm not sure if you have any of my books, but the code for this is in "PHP Solutions". In the first edition, it's PHP Solution 9-8. In the second edition, it's PHP Solution 9-9.