开发者

how to call php script from just server by bash?

开发者 https://www.devze.com 2023-03-27 14:17 出处:网络
i want to execute a php script every day. i have a bash script containing below lines. and i was do开发者_如何学Cing it with that script. but by this way, it can be executed from another computer. let

i want to execute a php script every day. i have a bash script containing below lines. and i was do开发者_如何学Cing it with that script. but by this way, it can be executed from another computer. lets say webservername="slmnbr" any body can call myscript with

xhttp://slmnbr/myscript.php.

i want to call it from just server.

BASE_URL=http://`get-webservername`
/usr/bin/wget --no-check-certificate --timeout=0 -O - "$BASE_URL/myscript.php"

thanks in advance


You need to have PHP CLI (command line interface) to do this. It often comes with the PHP installation package (or a separate package).

So as @James mentioned it you need to precede you script name with the CLI PHP executable which in windows is php.exe somewhere in your install directory.

In UN*X systems it's somewhere in /usr/bin or /usr/local/bin or /opt/local/bin. If it is in the PATH you can simply execute it like this:

php your_script.php

But keep in mind that running your script from server will be not the same as running from a webserver. For e.g. you won't have the POST or GET variables. So your script may not work.

Update

You can make the cron ran scripts unreachable for your webserver by making it unreadable by the user who is running the PHP scripts by the http daemon (usually www-data).

I recommend to make it readable only by the user who is running it from cron:

chown cronuser your_script.php
chmod 0400 your_script.php

Update2

If you're running your script in cron with the same user as the webserver does, then at the top of your script stop it from being executed:

if (!(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']))) {
  header('HTTP/1.1 403 Forbidden');
  exit;
}


To execute a task(or a php script) every day use cron.

The link has all the info you need, including how to prevent external access.


With any recent version of "php" you should get a standalone executable called: "php.exe"

Just execute this like so:

/usr/local/php.exe your_script.php
0

精彩评论

暂无评论...
验证码 换一张
取 消