Webbased chat in php without using database or file

前端 未结 14 1017
失恋的感觉
失恋的感觉 2021-02-02 14:49

I am trying to implement a realtime chat application using PHP . Is it possible to do it without using a persistent data storage like database or file . Basically what I need is

14条回答
  •  一整个雨季
    2021-02-02 15:24

    Just tried something I had never done before in response to this question. Seemed to work but I only tested it once. Instead of using a Socket I had an idea of using a shared Session variable. Basically I forced the Session_id to be the same value regardless of the user therefore they are all sharing the same data. From a quick test it seems to work. Here is what I did:

    session_id('12345');
    session_start();
    $session_id = session_id();
    $_SESSION['test'] = $_SESSION['test'] + 1;
    echo "session: {$session_id} test: {$_SESSION['test']} 
    ";

    So my thought process was that you could simply store the chat info in a Session variable and force everyone regardless of who they are to use a shared session. Then you can simply use ajax to continually reload the current Session variable, and use ajax to edit the session variable when adding a message. Also you would probably want to set the Session to never expire or have a really long maxlifetime.

    As I said I just played around with this for a few minutes to see if it would work.

提交回复
热议问题