954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP: Getting details like ip address and time

I want to get details of user when he registers with my website for security reasons. I need ip and date and time I guess. Is there anything else I need to cache while registering? How to get ip,time and date? And time will be in which timezone? I want it in GMT +5:30 means asia - culcutta. Thanks in advance.

And i want to save user log in time. Any help about this will be thankful.

kirtan_thakkar
Junior Poster in Training
79 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
$IP = $_SERVER['REMOTE_ADDR'];
$date_time = date("l j F Y  g:ia", time() - date("Z")) ;
echo "$date_time   IP: $IP";
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

The $_SERVER superglobals provide a load of useful information.

$ip = $_SERVER['REMOTE_ADDR'];     // The IP address from which the user is viewing the current page.
$time = $_SERVER['REQUEST_TIME'];  // The timestamp of the start of the request.     
$port = $_SERVER['REMOTE_PORT'];   // The port being used on the user's machine to communicate with the web server.   
$browser = $_SERVER['HTTP_USER_AGENT']; // Contents of the User-Agent (Web browser): header from the current request

// etc...


See here

nonshatter
Posting Whiz
377 posts since Nov 2009
Reputation Points: 39
Solved Threads: 63
 

Hi

Use this to see what information can be accessed and then decide what you want to use and access it like any other array

foreach ($_SERVER as $key => $item)
 {
  echo "<li><b>$key:</b> $item<br/></li>";
 }


N

nuttyniall
Newbie Poster
19 posts since Apr 2010
Reputation Points: 10
Solved Threads: 3
 

Thank you all. It done my work. But I have a last question. What would be better format to store user's login time? Like 1296143233 or Thursday 27 January 2011 3:47pm? Thank you. And how to convert 1296143233 to Thursday 27 January 2011 3:47pm. I think first format will save the disk space. Please help.

kirtan_thakkar
Junior Poster in Training
79 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

I generally tend to use a Unix timestamp, but I'm not really sure what the 'best' format is. This link explains mysql date formats

You can convert a unix timestamp to a human-readable format using the date() function. You can format the date format to your liking:

$timestamp = 1296143233;
$formattedtime = date("d/m/y", $timestamp)

Or to turn a date back into a timestamp, use strtotime() :

$date = "01/01/1970";
$date2timestamp = strtotime($date);
nonshatter
Posting Whiz
377 posts since Nov 2009
Reputation Points: 39
Solved Threads: 63
 

Hey thanks.... My work will get done by this.. Thank you so much to all of you who helped......

kirtan_thakkar
Junior Poster in Training
79 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: