I have a script that I need to run with PHP via cron. The original author of the script made a lot of user of certain $_SERVER parameters (like REQUEST_URI).

But it appears that certain variables don't exist when running PHP via command line or via CRON. For example, there is no request uri, so it makes sense that the REQUEST_URI parameter wouldn't be available.

Is there any way around this other than to completely rewrite the script in order to avoid using special $_SERVER parameters that aren't universally available?


When you don't really know the script I would recommend wget in your CRON to trigger the script through your webserver as the browser would do.

Dig into wget http://localhost/yourScript.php (-O and -o might be interesting or not for still receiving error messages&co) When you need "more", like a login using cookies you might check curl

When going to use hack´n´slay as @pQd mentioned and you can not edit the 3rdParty script you should take a look at "auto_prepend_file" in your cli.php.ini and consider starting the job using php -c

  • That's a good idea but we typically keep cron scripts outside of the web root so that it's not accessible to everyone out there. – Jake Wilson Apr 10 '12 at 22:22
  • When you bind the virtualHost in your apache to its fine ;-) – Tabakhase Apr 10 '12 at 22:26
  • <location /cronjobs>order deny,allow\n deny from all\n allow from</location> or you could even start another instance of apache to split it totally... sorry but this should really be the last problem ;-) – Tabakhase Apr 10 '12 at 22:29
  • Yeah normally a lot of apache config is feasible but we are working with a host that doesn't give us any control over it :-( – Jake Wilson Apr 10 '12 at 22:43
  • Then you still can put that access rule into a .htaccess file (and yes that can fail, but your ssh could get bruteforced too) – Tabakhase Apr 10 '12 at 22:47

by trial-and-error [analyzing errors printed by the script] or source analysis try figuring out which parameters are expected and at the beginning of the script add: $_SERVER['someName'] = '';$_SERVER['otherName'] = '';

it's very much script dependent - what if it expects some actual values there? what if for instance referrer is checked?


Because apparently serverfault wants to encourage new users to write answers instead of just making simple comments, I'll +1 Tabakhase's answer and add my incremental improvement here.

If you don't want to expose your script to the web, even with the .htaccess protection, you can write a short bash script to achieve your goal. Something like:


file=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c16)
cp $script /var/www/host/$file.php
wget --output-document=output.txt http://server/path/to/$file.php
rm /var/www/host/$file.php

You'll of course need to fiddle with permissions to make sure whoever is running the cron job can write files to your /var/www, which has its own implications.

Of course you can generate $file using any method you prefer, and can manage the output / headers / etc (even cookies!) by editing the options passed to wget.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.