Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8001313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:03:48+00:00 2026-06-04T16:03:48+00:00

I am using connect-mysql-session to store sessions in db. Now my question is how

  • 0

I am using connect-mysql-session to store sessions in db. Now my question is how do i add user data containing browser agent and ip-adress to check if the session is valid? How do i obtain that information? And how do i check if it matches?

    users.login(credentials,function(err, results) {

  //On errors
  if (err) {
    res.render(routes.index, {
      title: 'Login'
    });

  //On success
  } else if (results[0]) {
    //Set session data and redirect to start page
    req.session.userdata = results[0];
    req.session.userdata.email = req.body.email_login;
    req.session.is_logged_in = true;
    res.redirect('/start');

  //Wrong credentials
  } else {
    req.flash('warning','Wrong password or login');
    res.render('index', {
      title: 'Login'
    });
  }
});

Update:

I now got this added to the session:

req.session.ip = req.connection.remoteAddress;
req.session.useragent = req.headers['user-agent'];

and check for it in my auth middleware:

  if(req.session.userdata && req.session.is_logged_in === true && req.session.ip === req.connection.remoteAddress && req.session.useragent === req.headers['user-agent']) {
    next();
  } else {
    res.redirect('/');
  }

Is this secure or do you see any risks with this? Should i go about it another way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:03:51+00:00Added an answer on June 4, 2026 at 4:03 pm

    Your implementation looks good, and will give you some – very – basic protection against session hijacking.

    However, I’m not sure I understand your middleware. What prevents an user from requesting /start directly? And, more importantly, as the middleware intercepts all requests, even those for /start, doesn’t this look like some infinite redirect loop?

    My suggestion would simply be to consider a user logged out at all time when ip or user agent mismatches.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue with storing sessions in a MySQL database using CGI::Session. Here
I'm trying to connect to remote MySQL server with SSL from PHP using mysql_connect:
Using SQLAlchemy to connect to MySQL and I've gotten tired of writing things like
I am learning PHP and tried to connect to MySQL. Altough I am using
I'm trying to connect to a MySQL 5 database using the MySQL ODBC 5.1
I am using C# and I am trying to connect to the MySQL database
I am trying to connect to a MySQL database from a jsp page using
I'm using MySQL C API with C++ to connect to a database. I wold
I am using sessions to pass user information from one page to another. However,
I have been using mysql in a new rails application, but now I wanted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.