Can we run php in worker?

Hi,

I would like to run the following code in worker. But it is in PHP

<?php
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
$matchCountry = array("AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR", "HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE", "GB");
if (in_array($country_code, $matchCountry) && !($country_code === "IN"))
{
	echo("<script id=\"cookiebanner\" src=\"https://cdnjs.Cloudflare.com/ajax/libs/cookie-banner/1.2.2/cookiebanner.min.js\" data-moreinfo=\"https://trending.fropky.com/privacy-policy/\" data-message=\"We use cookies to improve your browsing experience.\"></script>");
}
?>

Or I dont know how to convert this code in worker. Can somebody help?

Have look here:
https://developers.cloudflare.com/workers/recipes/country-blocking/

1 Like

Do i need to write this here.

  if (countryBlocked){
document.write("run a script here");
  }

Hey @user3011, the code recipe that @adaptive is similar to what you’re looking for, but you’ll need to adjust slightly – I see you’re trying to add a cookie banner to the page for certain countries. I think this should work:

addEventListener('fetch', event => {
  event.respondWith(blockCountries(event.request))
})

const countries = new Set([
  "AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", 
  "FR", "DE", "GR", "HU", "IE", "IT", "LV", "LT",  "LU", 
  "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE", "GB"
])

async function blockCountries(request) {
  // Get country value from request headers
  let country = request.headers.get('cf-ipcountry')

  // Find out if country is on the block list
  let countryBlocked = countries.has(country)

  // If it's on the blocked list, add a cookie banner
  if (countryBlocked){
    let response = await fetch(request)
    let body = await response.text()
    body = body.replace("</body>", "<script id=\"cookiebanner\" src=\"https://cdnjs.Cloudflare.com/ajax/libs/cookie-banner/1.2.2/cookiebanner.min.js\" data-moreinfo=\"https://trending.fropky.com/privacy-policy/\" data-message=\"We use cookies to improve your browsing experience.\"></script></body>")
    return new Response(body, response)
  }

  // Catch-all return of the original response
  return await fetch(request)
}
4 Likes

Thanks a lot signalnerve

signalnerve,

Can it be run only for html/php pages only? So that no of worker requests r not many.

I add this script and it was showing cookie banner. Just all the images on site stopped working.

I tried this script. Traffic falled to 80%. Than I surf website (from non allow IP in firewall), Got 1025 error. My site was rate limited. I removed the script immediately and things are working fine now. Cutting edge technology doesn’t always work.

You can select the route so that the static assets’ path is excluded.

You need to make sure that where you apply the script is actually an HTML page.

The 1025 error is due to the default Workers’ rate limit. Contact support and they’ll lift it for you.

To contact Cloudflare Customer Support, login & go to https://dash.cloudflare.com/?account=support and select get more help. Please give Support the complete details and link to your Community post.

Matteo,

I am on business plan. Is there also a limit?

Regards,

Yes, the rate limit is by default there, on all plans. I don’t exactly know how much that is. Use the live chat you have available to make them lift it for you. It will take 15 minutes tops.

While I see you have a specific use case you are asking about, as for the question in the subject line, Cloudflare Workers are currently JavaScript only, but if you need specific PHP functionality, there are ports in JavaScript at PHP extensions in JavaScript | Locutus

Matteo,

They should mention that on the page. I lost way too many users today just because they failed to mention there is such a thing exist like limiting the use of worker.

I might not try worker in future ever again. I stick to my original idea for not trying cutting edge technology.

Regards,

Were you on the free version of Workers? Because my knowledge was prior to the latest pricing update and I think it has been lifted for the paid version. There could still exist a limit if you do thousands of reqs a minute from a single IP, but that I presume is to prevent a big price surge, can be lifted as well in case.

Number Of Requests Limit

Unlimited (Paid) Workers scripts automatically scale onto thousands of Cloudflare edge servers around the world; there is no general limit to the number of requests per second Workers can handle.

Cloudflare’s abuse protection methods do not affect well-intentioned traffic. However, if you send many thousands of requests per second from a small number of client IP addresses, you can inadvertently trigger Cloudflare’s abuse protection. If you expect to receive 1015 errors in response to traffic or expect your application to incur these errors, contact Cloudflare to increase your limit.

Accounts using the Workers free tier are limited to a maximum of 100,000 requests per day, with a burst rate limit of 1000 requests per 10 minutes. This limit applies at the account level, meaning that requests on your workers.dev subdomain count toward the same limit as your zoned domains. Visitors who run into the rate limit will be served a Cloudflare 1015 error page, however if you are calling your script programmatically, you can detect the rate limit page and handle it yourself by looking for HTTP status code 429. Upgrading to a paid plan will automatically lift this limit.

— https://workers.Cloudflare.com/docs/reference/runtime/limits/#number-of-requests-limit

Matteo,

Even If I am on free version of worker It says first 100000 request are free and after than they are paid. There is no mention of rate limiting if you hit the limit. This is plain gross wrong and stupid at Cloudflare part.

feeling really angry at myself right now That I thought of trying worker at all.

The limit is clearly stated in the limits section of the documentation, as per the quote above. I can concur it should be stated in the details dropdown within the Workers UI.

First I did not used first 100000 request before rate limit. And It is not mentioned on the page that They will rate limit after 100000 request.

It is written

you can now use Workers for free up to 100,000 requests per day across your account.

After than One should assume they are paid after that

Again, as above.

Accounts using the Workers free tier are limited to a maximum of 100,000 requests per day, with a burst rate limit of 1000 requests per 10 minutes. This limit applies at the account level, meaning that requests on your workers.dev subdomain count toward the same limit as your zoned domains. Visitors who run into the rate limit will be served a Cloudflare 1015 error page, however if you are calling your script programmatically, you can detect the rate limit page and handle it yourself by looking for HTTP status code 429. Upgrading to a paid plan will automatically lift this limit.

You can hit the limit with 1001 requests. Depending on the frequency of those executions.

It is the maximum limit. There isn’t a cost after those 100k executions. They stop working. They are working on handling the limit.

There is also this thread which mentions a possible solution, unfortunately as I am on the paid version I can’t test it, try it if you are interested, it will for sure help others. Maybe not on a production link.

1 Like

Matteo,

Should they rate limit whole website after that or should they stop working worker script after 1000 request in 10 minutes?

Regards,

They rate limit the script and obviously all requests passing through Workers. That is why the other thread, which I forgot to link (will do at the bottom here) suggest adding an event. passThroughOnException() to the script. It should work, but can’t test it myself, will try to see what I can do.