0

i want to execute a php script every day. i have a bash script containing below lines. and i was doing 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

1
  • 1
    you should take note that script running in CLI is not the same as running in apache due to different environment between CLI and apache Commented Aug 10, 2011 at 6:58

3 Answers 3

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks Karaszı, as i understand, i should use /usr/bin/php myscript.php in my bash script, and fix the problems, but it think it is more than this because i can still call it with xhttps://slmnbr/cron/myscript.php. cron is unaccessible (403 Forbidden) but myscript.php is still accessible. can be permissions or any config to make it unaccesible?
1

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.

2 Comments

It's a good idea to include all the info needed for the answer in case tutsplus does not outlive stackoverflow. It's also friendlier not to force anybody to leave the site to get the essentials of either the question or the answer.
thanks Chris, of course, i m using cron, what i want to learn is how to prevent external access like you said.
0

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

3 Comments

having php.exe in /usr/local/ it's a bit crazy :)
I have only got my Windows XAMP to look at not sure where it goes in the *NIX distributions.
Oh, sorry I don't know the Windows XAMP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.