#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Location
    Portugal
    Posts
    19
    Rep Power
    0

    Request for assistance


    Hi,

    I'm having a little proble with sessions in php - I don't work often with these.

    So, here's the "problem": I have a webpage in php for demonstration pourposes. Imagine a user (user A) enters and sees the demo, and makes some changes.

    When he gets out, another one (user B) might appear after an hour let's assume.

    If the new user (B) appears in between, I must run a script - to restore some default data in the script.

    Can I do this with sessions?

    Kind regards,

    JKepler
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,333
    Rep Power
    9645
    No? What "default data" are you talking about? Why doesn't the page know how to show different content (I guess?) for user A and user B?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    92
    Rep Power
    5
    If you want each visitor to have their own independent copy of data, you can use a session to hold this data.

    However, a script/site that allows 'changes' to be made, implies that a database may be involved. If so, and you would store the data in such a way that it is tied to each visitor and then at some point delete old data.

    The most straight-forward way, with the least amount of changes to your existing code, would be to either use the visitor's session id or a unique generated token (which would be stored in a session variable) as either a database name prefix (produce and populate a complete database for each visitor) or as a table name prefix (produce and populate a set of tables for each visitor.) For the database name prefix method, all you would need to do is select the correct database when you make the connection. For the table name prefix method, you would need to modify all your queries to use a variable/defined constant as a prefix for any table names.

    When a visitor arrives on your demo site, if they don't already have a session (the session would be empty) or a unique token, you would set up a session variable to indicate they started a demo, save the session id/token in a database table, along with the datetime (so that you will know how old data is so that you can eventually remove it), and create and populate the database/tables for whatever method you are going to use. You would use the session id/token in the connection code (method #1) or the queries (method #2) so that they reference the correct data for the visitor.
    Last edited by DSmabismad; September 3rd, 2017 at 09:52 AM. Reason: slight clarification in wording
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Location
    Portugal
    Posts
    19
    Rep Power
    0
    Hi DSmabismad

    Thanks for the reply. Even if no database is changed in the demo, your idea of a token is excelent - thank you very much. I'm not an expert on php sessions, but I got the general idea.

    Basically, I can create a session and save the time (and even the email of the visitor) in a regular txt file.

    When a new session is created, I can compare, for instance, the time difference which if its longer than 1 hour will run the reset script

    Kind regards, and thanks.

    JKepler
  8. #5
  9. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,780
    Rep Power
    4536
    There are three cases to consider if you save a "last active" timestamp in the session.

    1. A first time user of the site will not have a value set.
    2. A current user of the site will have a value less than an hour old.
    3. A timed out user will have a value over an hour old.

    Based on what you've said, 1 and 3 would trigger a "reset" of your data. This is assuming, since it's a demo site, that only one user is hitting it at a time.
    -- Cigars, whiskey and wild, wild women. --

IMN logo majestic logo threadwatch logo seochat tools logo