This could be a kind of strange question. I have a php file in a public web site where I have access to the server. For the moment we do not need to set Cron job for this file execution. However I need to limit the execution of this file only via localhost. I don’t have any precise idea about the way should I do this. Is there any way to detect whether the request is localhost or not? I mean with PHP. Or should I need to handle this via security setting or firewall of the server?
Share
I’m assuming you mean you want to be able to execute the script only from the server, and not via the web from another host. You have two options:
Option 1
move it out of the
public_htmlfolder. There is no reason you should not take this option, unless there is something preventing you from doing so. In that case,Option 2:
Wrap the entire code in the following
ifstatementAlternatively,
This ensures your script will only run if invoked from the command line, and not via the web.