tracking a user

Discussion in 'Web Design and Development' started by Cabbit, Jul 6, 2008.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    Rightie ho, i want to make a script with php that will tell me on one page who is accessing another page.

    Variables are $username, and $page. Every page on my site is have a $page = "page" and every username is well $username as far as the page is concerned.
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    I would think that you could have a script that updates a table in a database as the user moves around. On each page access it updates the table saying where they are. Then on your page it simply reads that table and outputs it. I don't have any code, but doesn't sound horribly hard to do, but might be a pain to add code to each page to update the table.
     
  3. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #3
    Im more looking to try to make a solution that doesn't involve the database, perhaps using session variables.
     
  4. Me1000 macrumors 68000

    Me1000

    Joined:
    Jul 15, 2006
    #4
    Not to totally discurrage you from this, but I recall reading something a while back on a forum dedicated to PHP development where someone wanted to do this.

    The end result was much more complicated than he originally thought.

    When I saw this thread I tried to figure out how to do it in my mind, however the only easy way I could think of would be to handel it in a database. If you already had a user table setup it could be very very simple...

    If you really want to do this your best bet would be to go to a forum dedicated to PHP or another server-side technology. :)
     
  5. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #5
    Aye i am on phpbuilder just now, worked out that a database is the best way but i don't know how to set it up so that it can send a request to the database when the user navigates away from the page.
     
  6. Me1000 macrumors 68000

    Me1000

    Joined:
    Jul 15, 2006
    #6
    The easiest way would not be when they navigate away from a page, but when they navigate to a page. When they navigate to the page just query the DB

    Code:
    UPDATE $table (PAGE) VALUES ("$page") WHERE USER=$user
    something along those lines, :)
     
  7. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #7
    Well there's always JavaScript that can detect onunload, but not likely a great solution. I would say just set an expiration, say 20 minutes. Then when you update a user for a page you can also do a check for expired 'sessions.' I do something very similar for a feedback form validation when checking how old a session is.

    If you didn't want to use a database you could use a flat file (plain text), with a user per line that is online,
    Code:
    user#timestamp#page
    It would take a bit of parsing, but doable.
     
  8. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #8
    The problem comes were its only for when the user is in the chat room and change the status the second they leave the chat room page and go onto a different page.
    This is kept in my whos online box that shows all online users and is updated every 5 seconds.
    The plan is to have a little bubble next to the users name if there in the chat room.
     
  9. Malfoy macrumors 6502a

    Malfoy

    Joined:
    Nov 1, 2005
    #9
    Still using a database it isnt a problem.

    on the page that is listing peoples location.
    put
    PHP:
    if ($page =='chatroom') {
    echo <
    img src='chatbubble.jpg'>; }
    echo 
    $User'  ' $page;
    obviously it would give syntax errors but the idea is there. if the page is the chatroom, itll put a bubble by the name. otherwise itll just display the name. the page refreshing is handled near the heading.
     
  10. exabytes18 macrumors 6502

    Joined:
    Jun 14, 2006
    Location:
    Suburb of Chicago
    #10
    I wrote a fairly simple forum from scratch with PHP. For "user's online," I just maintain a separate MySQL "MEMORY" table with all the sessions and their last activity (unix epoch is convenient). You could use some AJAX if you want to provide a more "real-time" chatroom, but that adds a bit more complexity.


    I then do something along the lines of:

    $tooOld = time() + 300;
    $sessionID = session_id();
    [DELETE FROM online_users WHERE last_activity<$tooOld AND session_id!=$sessionID]
    [SELECT * FROM online_users]
     

Share This Page