counting visits with session_id

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
staypufinpc
Forum Newbie
Posts: 12
Joined: Thu May 01, 2003 7:59 pm

counting visits with session_id

Post by staypufinpc »

HI all,
I don't think I'm understanding session_id correctly. I have a php/mysql site that requires users to log in. I want to keep track of how many times each person logs in, but I also have given them the "always log me in from this computer" option. Most of our users will always be connecting from their same computer (they're all in the same building, assuming they log in from work), so it's likely they'll choose this option. If this is enabled, I have no way of controlling which page they enter the site at. So, I thought I could use session_id to tell whether or not this is a new log in or if they are accessing the site from another page within the site. I thought I would store session_id as a cookie on their computer (I can assume they'll have cookies enabled, because this should only be the issue if they've already chosen "always log me in from this computer"), compare it to the current session_id and, if different, increase the counter by one. I would then reset the cookie to the new session_id so that they could go anywhere in the site within that session without resetting the counter. I am trying to test this but session_id never seems to change. I've restarted my browser a number of times and the session_id is always the same. How can I track their log-ins and still allow the "always log me in from this computer" option?
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

session_id is a string that identifies a set of variables stored on the server for that particular client. If you want to store data, start a session with session_start() and put variables into the $_SESSION superglobal array, and check them on the other pages.

Regarding storing session ids in cookies, php.ini has a setting, session.use_cookies, which will automatically do this for you.
staypufinpc
Forum Newbie
Posts: 12
Joined: Thu May 01, 2003 7:59 pm

got it

Post by staypufinpc »

Thanks,
ins't it interesting how we never think of the most obvious solutions? Thanks for the suggestion. All I did was first check if a variable $in_session was set, and then set it if not. This is included in the authentication function for each page, so it works anywhere. Thanks.
staypufinpc
Forum Newbie
Posts: 12
Joined: Thu May 01, 2003 7:59 pm

afterthought

Post by staypufinpc »

Here's an afterthought to the session thing, I think it has something to do with this post so I'm putting it here instead of adding a new topic.

if all of my $_SESSION variables are destroyed upon closing the browser, why don't I get a new session_id? I now understand that session_id is not very aptly named. Instead, it should be something like sessions_id or, better yet, client_id, since you can have numerous sessions with one session_id. Does that make sense? I mean, if I do:

Code: Select all

$variable=set;
session_register(variable);
and then quit my browser, start the browser again, start a new session and check for $_SESSION[variable] or session_is_registered(variable), I will get a false return, but, upon checking the PHPSESSID will get the same session id. Hence, I'm in a new session (my old session variables have expired) yet my id is the same. This confusion could be avoided were session_id named something else, since it truly refers to the client and not a single session. Or am I just off my rocker?
Post Reply