User Session 'user_agent' and 'ip_address' fields are corrupt when using Laravel Echo Server.
Hi there.
I'm working on a project, where administrator can monitor each user session and end them, if he wish. Also, he could see there all info about that session: IP Address, Browser and it's version, last page where that user was on, and last activity.
Sessions are stored in MySQL database in the form as suggested in the documentation.
All of it was working just fine, before I have integrated Laravel Echo Server into this project. It's being used to whisper from user to user and to catch the events, such as Log in or Log out, and to update that session table dynamically or notify user that his session was closed by admin. Now, each IP address in that sessions table is set to server's IP (haven't noticed that until uploaded the project to a live server with a real IP) and 'user_agent' field is set to empty string. As I stop the execution of my server.js file - everything is starting to work as it should.
Here is my server.js:
require('dotenv').config();
const env = process.env;
const Redis = require('ioredis');
const redis = new Redis({
port: env.REDIS_PORT, // Redis port
host: env.REDIS_HOST, // Redis host
family: 4, // 4 (IPv4) or 6 (IPv6)
password: env.REDIS_PASSWORD,
db: 0
});
const echoServer = require('laravel-echo-server');
echoServer.run({
authHost: env.APP_URL,
devMode: env.APP_DEBUG,
database: "redis",
port: env.ECHO_PORT,
databaseConfig: {
redis: {
host: env.REDIS_HOST,
port: env.REDIS_PORT,
password: env.REDIS_PASSWORD,
}
}
});
//...
For parsing user agent field in PHP side I'm using the "donatj/phpuseragentparser": "^0.9.0" package.
In my Session model I have some methods, one of them returns user agent:
public function getUserAgentAttribute($value)
{
return parse_user_agent($value);
}
Where parse_user_agent() is a helper from that package I've mentioned before. What it does is:
function parse_user_agent( $u_agent = null )
{
if( is_null($u_agent) ) {
if( isset($_SERVER['HTTP_USER_AGENT']) ) {
$u_agent = $_SERVER['HTTP_USER_AGENT'];
} else {
throw new \InvalidArgumentException('parse_user_agent requires a user agent');
}
}
/* ... */
And it always returns the browser admin is logged in with.
I don't understand why it is happening so.
Can anyone give me a hand here?
Please or to participate in this conversation.