admin管理员组文章数量:1122846
I would like to make a call to the external API before the page is loaded on my WordPress website. It looks somewhat like this:
$si = session_id();
$res = @file_get_contents("={$si}", false);
if ($res != 'ok')
exit('not ok');
It doesn't have to be php
but what matters is it should send the session id to the api and if the api responds with anything but "ok"
it should stop the page from loading (or maybe redirect somewhere).
I have tried adding this code to functions.php
, index.php
and wp-config.php
based on what other people were recommending, but only functions.php
runs consistently every time the page is loaded, but it fails when I open a page not in the browser, but in code, for example:
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0'}
res = requests.get('', headers=headers)
print(res.text)
It is especially important since obviously this code is meant to keep some users out of the website, and bots are a primary target, but the code above completely bypasses the api call and gets the whole html page. Which file can I add this check to so that It will run 100% of the time?
I would like to make a call to the external API before the page is loaded on my WordPress website. It looks somewhat like this:
$si = session_id();
$res = @file_get_contents("https://my-api.com/si_check?si={$si}", false);
if ($res != 'ok')
exit('not ok');
It doesn't have to be php
but what matters is it should send the session id to the api and if the api responds with anything but "ok"
it should stop the page from loading (or maybe redirect somewhere).
I have tried adding this code to functions.php
, index.php
and wp-config.php
based on what other people were recommending, but only functions.php
runs consistently every time the page is loaded, but it fails when I open a page not in the browser, but in code, for example:
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0'}
res = requests.get('https://my-website.com', headers=headers)
print(res.text)
It is especially important since obviously this code is meant to keep some users out of the website, and bots are a primary target, but the code above completely bypasses the api call and gets the whole html page. Which file can I add this check to so that It will run 100% of the time?
Share Improve this question asked Jul 27, 2024 at 22:04 user9102437user9102437 1135 bronze badges1 Answer
Reset to default 0The correct way to do this would be to wrap your custom functionality in a function and hook it to init.
本文标签: phpAdding code before any page loads
版权声明:本文标题:php - Adding code before any page loads 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736299765a1930575.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论