Skip to content

Commit ee20a94

Browse files
committed
feat: add location and IP docs
1 parent bdfbc46 commit ee20a94

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

src/docs/http/request.md

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,51 @@ app()->post('/example/register', function() {
230230

231231
In the example above, we're validating the data coming into our application. We're checking if the `name` field is a text, if the `email` field is a valid email, and if the `password` field is at least 8 characters long. If any of these validations fail, the `validate()` method will return `false` and you can get the errors using the `errors()` method. You can find the full list of validation rules [here](/docs/data/validation).
232232

233+
## Client IP & Geo Location
234+
235+
You can get the IP address of the client making the request using the `getIp()` method on the request object.
236+
237+
```php:no-line-numbers
238+
request()->getIp();
239+
```
240+
241+
::: danger Frontend IP Address
242+
The IP address returned by `getIp()` may not always be the actual client IP address, especially if your application is behind a proxy or load balancer. In such cases, the IP address returned may be that of the proxy or load balancer. We have added support for the **`X-Forwarded-For`** HTTP header, which is commonly used by proxies and load balancers to pass along the original client IP address, but be sure to validate the IP address returned by `getIp()` before using it in your application for critical tasks like rate limiting or logging.
243+
:::
244+
245+
### Get location from IP
246+
247+
You can get the geographical location of the client based on the current IP address from `getIp()` using the `getUserLocation()` method. This method uses the [ip-api](https://ip-api.com/) service to get the location data.
248+
249+
```php:no-line-numbers
250+
$location = request()->getUserLocation();
251+
252+
// $location = [
253+
// country' => 'United States',
254+
// 'countryCode' => 'US',
255+
// 'region' => 'CA',
256+
// 'regionName' => 'California',
257+
// 'currency' => 'USD',
258+
// 'city' => 'San Francisco',
259+
// 'zip' => '94105',
260+
// 'lat' => 37.7898,
261+
// 'lon' => -122.3942,
262+
// 'timezone' => 'America/Los_Angeles',
263+
// 'ip' => $ip,
264+
// 'continent' => 'North America',
265+
// 'continentCode' => 'NA',
266+
```
267+
268+
Keep in mind that the free tier of the ip-api service has a limit of 45 requests per minute from an IP address, you can check out other implementations for more robust solutions.
269+
270+
### Pass in a custom IP
271+
272+
You can also pass in a custom IP address to the `getLocationFromIp()` method to get the location data for that IP address.
273+
274+
```php:no-line-numbers
275+
$location = request()->getLocationFromIp('xxx.xxx.xxx.xxx');
276+
```
277+
233278
## Request Object Methods
234279

235280
The request object comes with other methods for doing things like interacting with the request headers, cookies, checking request types, and even user data. Here are some of the most common functionality you'll use:
@@ -284,7 +329,7 @@ This section contains methods which allow you to retrieve information about the
284329
Fetch the request’s host (e.g. “leafphp.dev”):
285330

286331
```php:no-line-numbers
287-
$app->request()->getHost();
332+
request()->getHost();
288333
```
289334

290335
### Host with Port
@@ -327,14 +372,6 @@ Fetch the request’s URL (scheme + host [ + port if non-standard ]):
327372
request()->getUrl();
328373
```
329374

330-
### IP Address
331-
332-
Fetch the request’s IP address:
333-
334-
```php:no-line-numbers
335-
request()->getIp();
336-
```
337-
338375
### Referer
339376

340377
Fetch the request’s referrer:

0 commit comments

Comments
 (0)