**I've seen a few posts regarding this question, or in the style of this question, but they were asked quite a long time ago, and I wanted to get an updated answer, which might help others as well
I've been working on a simple website (for learning) using PHP, with registration and login system. Now there are pages that are accessible only to logged-in users. In PHP I simply start a session, giving the users their unique session_id whenever they log in, and if the page is meant to be viewed only by logged-in users, I simply check if the session is set, and also get the session content, which will be used to fetch the user details from the database (name, birthday, etc)
Now I want to add real time chat capability, using socket.io with Node.js.
So I need to allow access only to logged in users, and get their session id so that I can get their name from the database to display.
This is the basic working chat so far, which is working to anyone who lands on localhost:3000
: (contains 2 files: index.html
, and index.js
)
Edit:
this is what I use in PHP to log in users:
session_start();
$_SESSION['username'] = $username;
And check if they're logged in:
if (!isset($_SESSION['username'])) { //do stuff
And I want the chat content to be saved (like Facebook or WhatsApp chats), so I need the session to be accessible by nodeJS to insert the chat content into the database, and also to fetch previous content from the database, no? Or I can do it only via PHP?
----End Edit----
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>
What do I need to do?
Thanks