Bader
Bader

Reputation: 207

retrieve username according to his current ip address SQL

i am trying to Select username based on his login ip address that stored when he logged,

i have two tables

  1. users
  2. sessions

here is my sql statement

Select users.username from users,sessions where user_id = se_user AND se_ip = '" + HttpContext.Current.Request.UserHostAddress + "';

i am using this sql statement inside a DetailsView on the DataSource Configurations, but it seems it doesn't work

what is the problem ?

Upvotes: 0

Views: 419

Answers (2)

Thyamine
Thyamine

Reputation: 1258

I would be careful with using the IP address since most users from home or inside an office have a dynamic IP address assigned. For a period of time it will most likely be the same, but if they happen to get a new address (modem reset, offline for a period of time, etc) then you can no longer trust that IP address.

I'm assuming you are already incorporating that, but just in case anyone comes along and decides to use this idea, they should know that.

Upvotes: 2

Lamak
Lamak

Reputation: 70668

Well, for starters, you should use a proper JOIN instead of the old version. And it looks like you are comparing se_ip with the same string all the time. If you are defining that statement in C#, i think you have a problem with the your quotes. In SQL simple quotes ' define a string, in C# doble quotes " define a string. So in C# yo should define your query like this:

"Select users.username 
from users JOIN [sessions] 
ON users.[USER_ID] = [sessions].se_user 
where se_ip = '" + HttpContext.Current.Request.UserHostAddress + '"

Upvotes: 3

Related Questions