Page 1 of 1

How do I track who's viewing my site?

Posted: Fri Feb 02, 2007 11:47 am
by JeremiahTrotter
Okay, first my background. I have some technical knowledge but I know little to nothing about PHP.

I wanted to create a members-only section for my site. I decided to protect it using PHPBB and was able to splice code here and there so you would have to login (via PHPBB) to view my members-only section. I also spliced in an IP logger that also logged the username.

However, I realized the problem was that only the page was protected: not the files, i.e. pictures and whatever.


So now I am starting over. I am willing to use the .htaccess method of protecting my directory BUT

I want to know if there's a PHP solution that will track who is accessing the members-only section by username and IP.

Thanks in advance. Sorry if my post is incomprehensible - English is not my first language. Thanks again!

Posted: Fri Feb 02, 2007 12:52 pm
by marionconsulting
You can write a small piece of code to handle the logging of username and ip address.

If you already have a login page then your user id is being captured by the code now, probably by something like $user_id = $_POST['user_id']
For the basic ip retrieval you can use $ip_address = $_SERVER['REMOTE_ADDR']; There is more advanced code to handle forwarded requests, etc, but this should get you started. Don't rely on the ip address for anything other than an FYI since it can be spoofed.

Once you have the $user_id and $ip_address then you can place them into a db, flat file, or whatever else you want to do with them

Posted: Sat Feb 03, 2007 10:18 am
by JeremiahTrotter
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Here is the code that I am trying to work with.

Code: Select all

//
// Username and IP Logging 
// 
$dt  = date("j-F-Y  H:i:s");
$fp = fopen("IPlog.txt", "a+");
$ip = $_SERVER['REMOTE_ADDR'];
$username = WHAT DO I PUT HERE????
$mg = "$dt $username From IP address $ip\n";
fwrite($fp, "$mg\r\n");
// change the above line to fwrite($fp, "$mg\n\n"); for unix
// or leave as-is for Windows
fclose($fp);
//
The username part is where I am having the problem. Also, there is no special login page, it will just be the traditional popup login from Internet Explorer.

Thanks so much for any help you can give.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]