Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Logout PHP ideas

  1. #1
    Join Date
    Apr 2008
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Logout PHP ideas

    I have a chat script that i made but i need ideas on removing users who are not on the webpage, right now if they login and close the browser they still stay in the chat, and to logout they need to click the logout link, what is some ideas to make users logout from the server.

  2. #2
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Take a look at this thread and see if it helps you out any.

  3. #3
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    I think you can use AJAX to submit a logout form when the window closes. Not sure though, as I haven't actually tried to do that. But based on my knowledge, Javascript can handle the close condition ("onunload"), and AJAX can submit a form, so I would guess you can do that.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Logout is simple-- destroy the session.
    Automatic logout isn't really needed-- the browser will reset the session on its own with enough time between.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    May 2007
    Location
    By the beach
    Posts
    23
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    First Option:

    Have the session timeout due to inactivity.

    MySQL Example:

    -- log user
    UPDATE `messagetable` SET `lastactive` = NOW WHERE `user` = 'x';

    -- expire inactive users
    DELETE FROM `messagetable` where `lastactive` < DATE_SUB(NOW(), INTERVAL 20 MINUTE);

    Reference: http://dev.mysql.com/doc/refman/5.0/...ction_date-add

    ~~~

    Second Option:

    Use onunload attribute in html body tag to call session destroy event.

    Ruff Example:

    HTML Code:
    <body onunload='logout_user()'>
    <script>
    //JavaScript Function
    function logout_user(){
      //send logout request via ajax to php file
    }
    </script>
    ~~~
    Note: PHP default session management will destory session data when all web browser windows are closed, cookie and session data is cleared or when the session_destroy() module is called.

  6. #6
    Join Date
    Apr 2008
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok this is what i have so far, when users log into my chat it creates an insert qry to onlineusers, with the fields, username and lastactive. last active is a timestamp in mysql with something like this: 2008-05-04 16:06:23

    I have a logout button where you click to logout when you have finished chatting and that deletes the row of your username so your username isnt displayed in the usersonline list, but i need a way to make it automatically do it so if they close the browser it deletes there row from the database.

    How would i do that?

  7. #7
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    . . . Again, like I and phpsales said, use AJAX. It can send the existing form when the user closes the browser. In other words, AJAX will essentially "click" the logout button when the user closes the browser (not litterally-- it will send the same data to the server, though in a slightly different way, thus causing the same effect; as far as the server is concerned, the user clicked the button). Check out a simple ajax tutorial here. (Not the best tutorial on the web, but if you already know JavaScript, it should be a breeze.)

    An example of AJAX taken from the end of the tutorial:
    Code:
    <html>
    <body>
    
    <script type="text/javascript">
    function ajaxFunction()
    {
    var xmlHttp;
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        try
          {
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        catch (e)
          {
          alert("Your browser does not support AJAX!");
          return false;
          }
        }
      }
      xmlHttp.onreadystatechange=function()
        {
        if(xmlHttp.readyState==4)
          {
          document.myForm.time.value=xmlHttp.responseText;
          }
        }
      xmlHttp.open("GET","time.asp",true);
      xmlHttp.send(null);
      }
    </script>
    
    <form name="myForm">
    Name: <input type="text"
    onkeyup="ajaxFunction();" name="username" />
    Time: <input type="text" name="time" />
    </form>
    
    </body>
    </html>
    Last edited by Jas; 05-10-2008 at 03:47 AM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  8. #8
    Join Date
    Apr 2008
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i'd like to avoid ajax and do it another way

  9. #9
    Join Date
    May 2007
    Location
    By the beach
    Posts
    23
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Since you are not using AJAX we can assume you are using a HTML IFRAME that refreshes. AJAX or non-AJAX still means that the client page has to check in to the server (refresh iframe) for new messages every (x) amount of time. E.g. every 2 seconds.

    1. Every 2 seconds the client checks in with the sever
    2. The server receives the check in and updates the last active for client
    3. The server then directly proceeds to remove all rows that are older than 2 seconds (use MySQL SQL I noted in earlier post).

    This method doesn't have to depend on a web browser closing to kill a session. Any time a user (client) fails to check in with in the allowed time they are automatically timed out. E.g. browser close, network connection closes, etc...

  10. #10
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Quote Originally Posted by Mike4x4 View Post
    i'd like to avoid ajax and do it another way
    Can I ask why? It shouldn't ruin anything existing on your website, though if you don't want to learn AJAX I understand.

    If your not going with AJAX, I would go with phpsales (though the user can't possible refresh every 2 seconds, so you'll have to go with >=120 seconds or find another way to go on that bit; since this is a chat, the whole refresh concept might be a little annoying to users). That method is used by many sites for things like Who's Online stats. Not the more effective way to go, but it does work. And it sounds like you are already partially set up for that anyway (although your also set up for AJAX ).
    Last edited by Jas; 05-10-2008 at 11:51 PM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •