簡體   English   中英

如何檢查用戶是否已連接並獲取其會話ID(會話由PHP啟動)

[英]How to check if user is connected and get his session id (Session started by PHP)

**我已經看過一些有關此問題或類似問題的帖子,但是很久以前有人問過他們,我想獲得更新的答案,這可能對其他人也有幫助

我一直在使用帶有注冊和登錄系統的PHP創建一個簡單的網站(用於學習)。 現在,有些頁面只能由登錄用戶訪問。 在PHP中,我只需啟動一個會話,並在用戶每次登錄時為他們提供唯一的session_id,如果該頁面僅供登錄用戶查看,則只需檢查該會話是否已設置,並獲取會話內容,它將用於從數據庫中獲取用戶詳細信息(姓名,生日等)

現在,我想使用帶有Node.js的socket.io添加實時聊天功能。 因此,我只需要允許登錄用戶訪問,並獲取他們的會話ID,這樣我就可以從數據庫中獲取他們的姓名以進行顯示。 到目前為止,這是基本的工作聊天,適用於所有登陸localhost:3000 :(包含2個文件: index.htmlindex.js

編輯:

這是我在PHP中用於登錄用戶的內容:

session_start();
$_SESSION['username'] = $username;

並檢查他們是否已登錄:

if (!isset($_SESSION['username'])) { //do stuff

而且我希望保存聊天內容(例如Facebook或WhatsApp聊天),因此我需要sessionJS可以訪問該會話以將聊天內容插入數據庫,並從數據庫中獲取以前的內容,不是嗎? 或者我只能通過PHP做到這一點?

----結束編輯----

index.js:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  console.log('a user connected');
    socket.on('chat message', function(msg){
    io.emit('chat message', msg);
    });
  socket.on('disconnect', function(){
    console.log('user disconnected');   
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

index.html:

<!doctype html>
<html>
  <head>    
    <script src="socket.io/socket.io.js"></script>
    <script
        src="https://code.jquery.com/jquery-3.3.1.min.js"
        integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
        crossorigin="anonymous"></script>
    <script>
    $(function () {
        var socket = io();
        $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
    });
    socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
    });
  });
</script>

    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>

  </head>
  <body>

    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
  </body>
</html>

我需要做什么?

謝謝

您可以將用戶名和聊天消息一起發送。 例如,您的聊天消息包含“ Hello world”,而用戶“ John Doe”發送了該消息。 然后,您可以發送這樣的對象,而不僅僅是消息:

{ "name": "John Doe", "message": "Hello World" }

到您的聊天應用程序。

另一方面,您可以解碼此JSON對象,並僅顯示名稱和消息。

在您的情況下,這意味着替換以下內容:

socket.emit('chat message', $('#m').val());

socket.emit('chat message', {name: "John Doe", message: $('#m').val()});

$('#messages').append($('<li>').text(msg));

$('#messages').append($('<li>').text(msg.name + ": " + msg.message));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM