Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: cron & $_SERVER

  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Posts
    131
    Thanks
    21
    Thanked 0 Times in 0 Posts

    cron & $_SERVER

    Im trying to find a solution to the problem of cron jobs not liking $_server, because i use that to direct all my scripts to a fixed location.

    I've tried a few things just to see if i could solve it myself, but short of removing every instance of $_server["something"] from these scripts, is there something i can use for the cron file to understand it?

    I tried -
    PHP Code:
    if (!$_SERVER) { $_SERVER["DOCUMENT_ROOT"] = dirname(dirname(__FILE__));} 
    PHP Code:
    $_SERVER["DOCUMENT_ROOT"] = dirname(dirname(__FILE__)); 
    thinking that if there wasnt a $_SERVER array available, then i could basically create the values myself, but im still getting PHP Notice: Undefined index: DOCUMENT_ROOT

    So, what are my options here? I really dont want to have to go through a load of scripts and swap out a $_SERVER and replace it with something else, but im not sure what other choice i have. Is there something im missing which would allow this to work?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Cron will never have $_SERVER available. This is why I have always said that $_SERVER should be avoided for anything functional such as inclusions since it is unreliable.
    As of 5.4, there is an embedded webserver as well, which I would presume populates the $_SERVER as well. That should include a document root which can be specified with the -t.
    Other than that, you should be able to invoke the cli and provide it with an auto_prepend_file directive. That file's job would be to specify the server populations. Don't bother with issets, just place it above the document root and only use auto_prepend_file when invoking it on the cron.
    I haven't a clue if that'll actually work since I have near 0 reliance on the server superglobal, but I can't see why it wouldn't.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    PaulC2K (09-12-2013)

  • #3
    Regular Coder
    Join Date
    Jul 2005
    Posts
    131
    Thanks
    21
    Thanked 0 Times in 0 Posts
    Yeah, i realise i cant use $_SERVER for this, ive just never found a straight forward solution which doesnt involve writing 2 versions.

    I've been trying to figure out how to do the cron with curl, i had absolutely no idea there were different versions, and it seems like that would solve my problems of it running outside the HTTP or whatever its doing, and basically function as it would if i was viewing the page.

    I have to admit, i didnt realise it was bad practice to use $_SERVER, i thought i was doing things properly for once


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •