/** * Will return location data given a user's ip address. * @param string $ip The ip address to backtrace. * @return array */ public static function lookupIp($ip = null) { // Log whether the ip was null $wasNull = false; // Let's see if we've done this recently for this session if ($ip == null && Session::exists('backtrace_ip')) { return Session::get('backtrace_ip'); } // Choose the ip to use if ($ip == null) { $wasNull = true; $ip = Session::getIpAddress(); } // Format the host string for the api $host = sprintf(self::IP_BACKTRACE_HOST, $ip); // Get the response from the api $response = Utils::curl_get_contents($host); $data = unserialize($response); // Return the info in an array $result = array('ip' => $ip, 'city' => $data['geoplugin_city'], 'state' => $data['geoplugin_region'], 'state_full' => $data['geoplugin_regionName'], 'area_code' => $data['geoplugin_areaCode'], 'dma' => $data['geoplugin_dmaCode'], 'country_code' => $data['geoplugin_countryCode'], 'country_name' => $data['geoplugin_countryName'], 'continent_code' => $data['geoplugin_continentCode'], 'latitude' => $data['geoplugin_latitude'], 'longitude' => $data['geoplugin_longitude'], 'currency_code' => $data['geoplugin_currencyCode'], 'currency_symbol' => $data['geoplugin_currencySymbol']); // Now let's get the zip code $latLongLookup = self::lookupLatLong($result['latitude'], $result['longitude']); $result['zip'] = $latLongLookup['zip']; // Save this for future reference, if this was the current user's ip if ($wasNull) { Session::set('backtrace_ip', $result); } // Now let's return the result return $result; }
public function insert(Session $ses) { echo $ses->getInTime() . ' ' . $ses->getOutTime() . '</br>'; $query = "INSERT INTO `session`(`in_time`, `out_time`, `ip_address`, `device`, `browser`, `user_login_iduser_login`) VALUES ('" . $ses->getInTime() . "', '" . $ses->getOutTime() . "', '" . $ses->getIpAddress() . "', '" . $ses->getDevice() . "','" . $ses->getBrowser() . "','" . $ses->getUserLoginIduserLogin() . "')"; $this->con->openConnection(); $this->con->executeRawQuery($query); $this->con->closeConnection(); }
/** * Updates the visit info such as the current visit time and * the last visit time. * @return type */ public function updateVisitInfo() { // Make sure this user isn't a guest if ($this->isGuest()) { return; } // Update the local info $this->row['last_visit_at'] = $this->row['current_visit_at']; $this->row['last_visit_from'] = $this->row['current_visit_from']; $this->row['current_visit_at'] = time(); $this->row['current_visit_from'] = Session::getIpAddress(); // Update on the database $query = Database::connection()->prepare('UPDATE user SET last_visit_at = ?,' . ' last_visit_from = ?, current_visit_at = ?, current_visit_from = ? WHERE userid = ?'); $query->bindValue(1, $this->getLastVisitTime(), PDO::PARAM_INT); $query->bindValue(2, $this->getLastVisitIP(), PDO::PARAM_STR); $query->bindValue(3, $this->getCurrentVisitTime(), PDO::PARAM_INT); $query->bindValue(4, $this->getCurrentVisitIP(), PDO::PARAM_STR); $query->bindValue(5, $this->getUserId(), PDO::PARAM_INT); // This query isn't really important so we wont complain // if it doesnt execute properly $query->execute(); }