guest session php | гостевая сессия php - Forum

Forum Navigation
You need to log in to create posts and topics.

guest session php | гостевая сессия php

есть-ли у кого нибудь готовый php файл для получения гостевой сессии.
Я создаю приложение в котором будут временные записи привязанные к гостевой сессии и храниться на сервере.
То есть, пользователь заходит на сайт,  php создает гостевую сессию, и если пользователь сохранить запить в БД то она должна сохраниться по отношению к гостевой сессии.

Does anyone have a ready-made PHP file for creating a guest session?
I'm developing an application that will store temporary records associated with a guest session on the server.
So, a user logs into the site, the PHP file creates a guest session, and if the user saves a record to the database, it should be stored relative to the guest session.

Hi @lolo,

I have prepared a ready-to-use PHP file for anonymous guest sessions and attached it to this post: guest_session.php.

PHP already creates a browser session when session_start() is called. However, it is better not to use the PHP session ID directly as a database identifier. This example generates a separate random guest_id, stores it inside $_SESSION, and associates every temporary database record with that guest.

The file provides these actions:

  • session: creates or returns the current guest session.
  • save: saves a temporary record for the current guest.
  • list: returns only the current guest’s records.
  • delete: deletes one of the current guest’s records.
  • clear: deletes all records belonging to the current guest.
  • renew: starts a new guest identity.

It uses SQLite and automatically creates:

guest-data/guest-records.sqlite

Records expire after 24 hours. You can change this value near the beginning of the PHP file:

const GUEST_RECORD_LIFETIME = 86400;

Upload guest_session.php to the same directory as your VisualNEO Web application.

To initialize or obtain the guest session:

$.ajax({
    type: "POST",
    url: "guest_session.php",
    dataType: "json",
    data: {
        action: "session"
    },
    success: function(result) {
        console.log("Guest ID:", result.guestId);
    }
});

To save a temporary record:

$.ajax({
    type: "POST",
    url: "guest_session.php",
    dataType: "json",
    data: {
        action: "save",
        content: "My temporary record"
    },
    success: function(result) {
        console.log("Record ID:", result.id);
    }
});

To retrieve the current guest’s records:

$.ajax({
    type: "POST",
    url: "guest_session.php",
    dataType: "json",
    data: {
        action: "list"
    },
    success: function(result) {
        console.log(result.records);
    }
});

To delete one record:

$.ajax({
    type: "POST",
    url: "guest_session.php",
    dataType: "json",
    data: {
        action: "delete",
        id: 123
    },
    success: function(result) {
        console.log(result.deleted);
    }
});

Important notes:

  • The browser must accept the PHP session cookie.
  • The PHP file and VisualNEO application should preferably use the same domain.
  • PHP needs write permission in the directory where guest-data is created.
  • The server needs the PDO SQLite extension.
  • If the visitor deletes the browser cookies or uses another browser, a new guest identity will be created.
  • Old records remain inaccessible and are automatically deleted after their expiration time.
  • The server never accepts a guest_id supplied by JavaScript. It always obtains it securely from $_SESSION.
  • For a production application, it is preferable to place the SQLite database outside the public web directory.

The example is ready to use, but the guest_records table can be extended with any additional fields required by your application.

Uploaded files: