[RESOLVED] Running "require" with a cron job
Results 1 to 7 of 7

Thread: [RESOLVED] Running "require" with a cron job

  1. #1
    Senior Member
    Join Date
    Mar 2010
    Posts
    327

    resolved [RESOLVED] Running "require" with a cron job

    Hi all:

    I have a php script that I am running using s cron job. It contains the following include:

    PHP Code:
    require($_SERVER['DOCUMENT_ROOT'].'/scr/scrSanitize.php'); 
    This is not being recognized when running as a cron job. The following page is a good thread on this subject, but I cannot figure out how to get their answer to work for me.


    http://stackoverflow.com/questions/2100545/serverdocument-root-does-not-work-in-the-php-script-running-through-cron


    So how does one recode require($_SERVER['DOCUMENT_ROOT'].'/scr/scrSanitize.php'); so it works on a cron job?

    Thanks !

  2. #2
    Senior Member
    Join Date
    Oct 2005
    Location
    Chicago, IL
    Posts
    428
    you need the full path starting at '/'.
    Bob Lob Law

  3. #3
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,902
    If you are running the script via the command line (e.g. called from the cron job as something like "php path/to/file.php") then there is no web server involved, so $_SERVER variables won't exist (or at least won't be populated). Therefore you'll need to reference the required file via a relative or absolute file system path.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  4. #4
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,902
    Quote Originally Posted by jeepin81 View Post
    you need the full path starting at '/'.
    Or a relative path, likely using "../", or "../../", etc., to get down to what would be the document root if it were being run as a web page.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  5. #5
    Senior Member
    Join Date
    Oct 2005
    Location
    Chicago, IL
    Posts
    428
    wont the absolute root path start with '/'?
    Bob Lob Law

  6. #6
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,902
    Quote Originally Posted by jeepin81 View Post
    wont the absolute root path start with '/'?
    Yes (unless it's on Windoze). But you do not have to use a full path to require/include a file. It all depends on what the complete story is here and how portable the script needs to be.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  7. #7
    Senior Member
    Join Date
    Mar 2010
    Posts
    327
    Thanks all! The following worked!

    require('/home/myusername/public_html/scr/scrSanitize.php');

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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