Welcome Guest, Not a member yet? Register   Sign In
Getting user's IP address: input vs session methods
#1

[eluser]leighmarble[/eluser]
I have a website that autoloads the session class (and also saves session data to database), so all visitors to the site get session data stored for them.

So, in order to get any visitor's IP address, I could either do this:

Code:
$ip = $this->session->userdata('ip_address');

Or this:

Code:
$ip = $this->input->ip_address();

Is there an advantage of one over the other? In some brief iterative testing I've done, the input class method sometimes runs a bit faster. And, I suppose, if someone was blocking cookies with their browser settings, then session->userdata would be useless.

So, that would tell me that I'd pretty much always want to use the input class method. Any persuasive arguments in favor of using the session class for this?

Thanks,
Leigh
#2

[eluser]Tpojka[/eluser]
Input library is autoloaded so you can use it rather if you don't need session library.
#3

[eluser]leighmarble[/eluser]
[quote author="Tpojka" date="1394060354"]Input library is autoloaded so you can use it rather if you don't need session library.[/quote]

Yes, the Input library is autoloaded by CodeIgniter no matter what. But as I mentioned, the website I'm working on also has the Session library autoloaded, which makes the Input method and the Session method equally available in this case.

Really, since I only see advantages to using the Input library method, I'm looking for arguments in favor of using the Session library method. When would using the Session library method of grabbing the ip_address key from the stored user data ever be the preferable way of getting a user's IP address?

In fact, in CodeIgniter's Session library, it calls the Input library's ip_address() method and, if the ip_address stored in the session doesn't match the value returned from the Input class, it destroys the session. See the Session.php file, lines 185-189 (in CodeIgniter version 2.1.4):

Code:
if ($this->sess_match_ip == TRUE AND $session['ip_address'] != $this->CI->input->ip_address())
{
    $this->sess_destroy();
    return FALSE;
}

So... as far as I can tell, input->ip_address() always reigns supreme, and using the session method only means extra overhead.

But if there are arguments to the contrary, I would love to hear them, before I abandon the session method of fetching ip addresses forever!
#4

[eluser]InsiteFX[/eluser]
If he is using database sessions then it will also mean an extra database query call.
#5

[eluser]leighmarble[/eluser]
[quote author="InsiteFX" date="1394085851"]If he is using database sessions then it will also mean an extra database query call.[/quote]

Actually, it wouldn't be an extra database call. If you're using the database to store session data (in the "ci_sessions" table), even if you do nothing at all with your script, CodeIgniter still grabs the session data out of the table, and stores it in the session object. And so any calls to $this->session->userdata('ip_address') are therefore just pulling the ip address as a property from the session object - not making additional calls to the database.

THAT SAID... your point would be another argument AGAINST using the Session class method. I'm still looking for an argument FOR ever using it:

Code:
$ip = $this->session->userdata('ip_address');

Cool? Tell me, when does it ever make sense to use that call?
#6

[eluser]CroNiX[/eluser]
The discussion is kind of moot, isn't it? As you pointed out, they are basically aliases of the same thing, so use whichever you want.
#7

[eluser]leighmarble[/eluser]
[quote author="CroNiX" date="1394146816"]The discussion is kind of moot, isn't it? As you pointed out, they are basically aliases of the same thing, so use whichever you want.[/quote]

You tell me. I'm not an expert programmer, and I've been "sure" of things with programming in the past that I later found out I was wrong about, because I was missing the bigger picture on some issue.

Since the Session method depends (of course) on having the Session class loaded, that makes it the weaker of the two methods. And I cannot see a single advantage to using the Session class method. So, I would consider it informally deprecated, as far as my practice goes.

But I posted the issue here, in case there was some aspect I was overlooking. So far, I'm hearing no objections to my informal deprecation of $this->session->userdata('ip_address')

Thanks,
Leigh
#8

[eluser]InsiteFX[/eluser]
Oh and like the input class is not load. It is load on start up.

So either way a class is being loaded.

#9

[eluser]Massaki[/eluser]
I think the best way is using $this->input->ip_address(), because if you reutilize the method in another program, and the session library is not loaded (not needed), you won't get it.
#10

[eluser]boltsabre[/eluser]
Considering that using the session/userdata method is also calling the Input library’s ip_address, it stands to reason that your testing shows that the session way is slower.

So... use input!!!

It's less key strokes (quicker coding) which also results in less chance of typo bugs.

The only time I guess you wouldn't use input is when you want to ensure you have a legitimate session. You mentioned that going via the session way checks that the ip addresses match, and if they don't the session is being killed.

So yeah, if you are doing something where ensuring you have a legitimate session running is important then use session, else use input.

That's what I've taken out of this thread... I personally hadn't checked out the source code of either of these before, it it was all new to me. Learn something new every day eh :-)




Theme © iAndrew 2016 - Forum software by © MyBB