What you have in mind is not a captive portal at all.
Rather, it would seem you are trying to reinstantiate something like Pi-hole's blocking page, which was removed with Pi-hole FTL v5.17, Web v5.14 and Core v5.12 released in September 2022.
You may still try to roll your own - see the suggestions for a custom.php
page at Upcoming Pi-hole changes – Pi-hole, but note that a block page can only be injected with HTTP - it won't work with HTTPS sites.
Nowadays, the vast majority of websites is using HTTPS, which is the major reason why Pi-hole has switched to NULL blocking (unspecified IP blocking) as default and removed that blocking page.