KBD

Keith Devens .com

Thursday, August 28, 2008 Flag waving
"For a while he didn't think it was possible. Then, once he got over that..." – GameSpy on Will Wright's "Spore" video game
← Entry 1303Entry 1305 →

Daily link icon Saturday, January 26, 2002

Entry 1304

Ok, here's my programming dilemma. I have a task manager on my website programmed in PHP. I want to write a script that will e-mail me my tasks for the day. I can set up a cron job for it that will run it at 12:05am or something. For security, I may put the script that is run by the cron job in a folder outside of my public website, but I'd prefer not to if possible.

Those are the specs, here's my problem. My task manager is private, so it forces you to log in by forwarding you to the login page if you're not logged in. The cron job isn't coming from a browser so it returns no cookie with a session id to show whether you're logged in or not, and it wouldn't be logged in with the cron job anyway. So, how do I detect when the PHP script is running standalone from a cron job so I can make it bypass the login? It's probably not a big deal, I'm just not sure how to do it (what environment variables I should check, etc.).

Ok, here's what I did. Some variables are not available from the command line, like I thought. The manual didn't recommend a specific variable to use, but it did say explicitly that "If PHP is running as a command-line processor, [$PHP_SELF] is not available", so I used $PHP_SELF. I also found out about using PHP from the command line.

If $PHP_SELF isn't defined, I turn off sessions and the web-based authentication, which I don't think is a security hole because no one but me (and root) can run my scripts from the command line. I also set $PHP_SELF to $argv[0] so that my forms will still work in the code generated from the command line, and I set a "base href" for my script so it'll still go to the right place when I click on links in the HTML e-mail.

The cron job runs a notify script which calls my main tasks script from the command line and e-mails the output. So now I get my tasks in the mail every morning. I'll never forget anything again! Smiley

← Entry 1303Entry 1305 →

Comments XML gif


Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

August 2008
SunMonTueWedThuFriSat
 12
3456789
10111213141516
17181920212223
24252627282930
31 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 1 posts

Recent comments XML

new⇒Girls, please don't get breast implants

http://war-11.ucufyod.net​http://jewelry-26.wohiles.net​http://games-52.ruz...

Randolph: Aug 28, 5:16am

Generated in about 0.13s.

(Used 8 db queries)

mobile phone