Home

Awesome

<div align="center">

Cloudflare Laravel Request

<img src="https://cdn.snipform.io/pdphilip/cf-request/cf-request.png" alt="Cloudflare Laravel Request" />

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

</div>

Cloudflare Laravel Request inherits the request object from Laravel and parses specific headers from Cloudflare to provide additional information about the request, including:

The User-Agent is also parsed to provide additional information about the device, including:

With this package, you can:

CF Request in action: Test your connection

Highlights

Lean into Cloudflare's Security

public function register(CfRequest $request)
{
    if ($request->isBot()) {
        abort(403, 'Naughty bots');
    }
    if ($request->threatScore() > 50) {
        abort(403, 'Thanks but no thanks');
    }
    $attributes = $request->validate([
        'first_name' => 'required|string',
        'last_name' => 'required|string',
        //... etc
    ]);
   //... etc
}

Set the timezone based on the origin

date_default_timezone_set(CfRequest::timezone());
// Now carbon dates will be parsed for the user's timezone

Apply logic based on the user's country


public function welcome()
{
   if (CfRequest::country() === 'US') {
         return view('welcome_us');
   }
   return view('welcome');
}

Apply logic based on device type

public function welcome()
{
    $loadVideo = true;
    if (CfRequest::deviceType() === 'mobile') {
        $loadVideo = false;
    }
    // etc
}

Requirements

Installation

Add the package via composer:

composer require pdphilip/cf-request

Then install with:

php artisan cf-request:install

Cloudflare Setup

<details> <summary>Option 1: Via Cloudflare API</summary>

Step 1: Copy Zone ID

<img src="https://cdn.snipform.io/pdphilip/cf-request/zoneId.png" alt="Cloudflare Laravel Request - zoneid" />

Step 2: Create an API Token

Token Configuration

<img src="https://cdn.snipform.io/pdphilip/cf-request/token-perms.png" alt="Cloudflare Laravel Request - token perms" />

Run the artisan command:

php artisan cf-request:headers
<img src="https://cdn.snipform.io/pdphilip/cf-request/cf-request-headers.gif" alt="Cloudflare Laravel Request - artisan" />
</details> <details> <summary>Option 2: Manually on Cloudflare</summary>

Navigate to "Modify Request Header"

Creating the rule

Set dynamic
X-AGENT
http.user_agent

Set dynamic
X-IP
ip.src

Set dynamic
X-COUNTRY
ip.src.country

Set dynamic
X-CONTINENT
ip.src.continent

Set dynamic
X-CITY
ip.src.city

Set dynamic
X-POSTAL-CODE
ip.src.postal_code

Set dynamic
X-REGION
ip.src.region

Set dynamic
X-TIMEZONE
ip.src.timezone.name

Set dynamic
X-LAT
ip.src.lat

Set dynamic
X-LON
ip.src.lon

Set dynamic
X-REFERER
http.referer

Set dynamic
X-IS-BOT
cf.client.bot

Set dynamic
X-THREAT-SCORE
cf.threat_score


</details>

Usage

All the standard Laravel request methods are available, with the following additional methods:

CfRequest::country()

CfRequest::city()

CfRequest::region()

CfRequest::postalCode()

CfRequest::lat()

CfRequest::lon()

CfRequest::timezone()

CfRequest::isBot()

CfRequest::threatScore()

CfRequest::isMobile()

CfRequest::isTablet()

CfRequest::isDesktop()

CfRequest::isTv()

CfRequest::deviceType()

CfRequest::deviceBrand()

CfRequest::deviceModel()

CfRequest::os()

CfRequest::osVersion()

CfRequest::osFamily()

CfRequest::browser()

CfRequest::browserVersion()

CfRequest::browserName()

CfRequest::browserFamily()

CfRequest::referer()

CfRequest::refererDomain()

You can use the CfRequest facade or inject the CfRequest $request class into your controller methods.

Testing headers

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.