how to tell CLI or webserver execution?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawpoop

    how to tell CLI or webserver execution?

    Within a php file, how can you tell if it's being called by the
    webserver, or by the cli?

    I have some pages that I developed to be served, but now we want to
    run some of them as cron jobs. I have to change some of the path
    references ( our cron job scripts reference include files in the
    webserver directory, and relative paths in the include files break
    when they are called by a script in another directory ), and to make
    everything more interoperable, I need to reference things based on
    whether it's being called by Apache or by the cli. Specifically, I'm
    making a reference to $_SERVER['DOCUMENT_ROOT'], which is empty when
    called by the cli.

    So, I need to know if the script is being called by apache or the
    command line, so I can choose how to references certain included
    files. How should I do this?
  • atpunkt@punktat.de

    #2
    Re: how to tell CLI or webserver execution?

    On 24 Okt., 17:29, lawpoop <lawp...@gmail. comwrote:
    Within a php file, how can you tell if it's being called by the
    webserver, or by the cli?
    >
    I have some pages that I developed to be served, but now we want to
    run some of them as cron jobs. I have to change some of the path
    references ( our cron job scripts reference include files in the
    webserver directory, and relative paths in the include files break
    when they are called by a script in another directory ), and to make
    everything more interoperable, I need to reference things based on
    whether it's being called by Apache or by the cli. Specifically, I'm
    making a reference to $_SERVER['DOCUMENT_ROOT'], which is empty when
    called by the cli.
    >
    So, I need to know if the script is being called by apache or the
    command line, so I can choose how to references certain included
    files. How should I do this?
    You gave the answer yourself:

    if (empty($_SERVER['DOCUMENT_ROOT']))
    {
    }

    There certainly are other ways - but why bother?

    greetings,
    Phil

    Comment

    • Jerry Stuckle

      #3
      Re: how to tell CLI or webserver execution?

      lawpoop wrote:
      Within a php file, how can you tell if it's being called by the
      webserver, or by the cli?
      >
      I have some pages that I developed to be served, but now we want to
      run some of them as cron jobs. I have to change some of the path
      references ( our cron job scripts reference include files in the
      webserver directory, and relative paths in the include files break
      when they are called by a script in another directory ), and to make
      everything more interoperable, I need to reference things based on
      whether it's being called by Apache or by the cli. Specifically, I'm
      making a reference to $_SERVER['DOCUMENT_ROOT'], which is empty when
      called by the cli.
      >
      So, I need to know if the script is being called by apache or the
      command line, so I can choose how to references certain included
      files. How should I do this?
      >
      One way would be to see if something webserver specific is set, i.e.

      if (isset($_SERVER['SERVER_ADDR'])) {

      This should be true if running under a webserver, and false if not. Of
      course there are several other values you could use, also.

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • lawpoop

        #4
        Re: how to tell CLI or webserver execution?

        On Oct 24, 11:58 am, "atpu...@punkta t.de" <atpu...@google mail.com>
        wrote:
        >
        You gave the answer yourself:
        >
        if (empty($_SERVER['DOCUMENT_ROOT']))
        {
        >
        }
        >
        There certainly are other ways - but why bother?
        Thanks, Phil. I just didn't know if this was the "for sure" answer.
        Perhaps under certain circumstances or configurations, this might have
        a value when php is called from the cli. I didn't know.



        Comment

        • lawpoop

          #5
          Re: how to tell CLI or webserver execution?

          On Oct 24, 12:03 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
          One way would be to see if something webserver specific is set, i.e.
          >
          if (isset($_SERVER['SERVER_ADDR'])) {
          >
          This should be true if running under a webserver, and false if not.  Of
          course there are several other values you could use, also.
          >
          Thanks, Jerry. I just wasn't sure if certain variables were more
          reliable than others. I suppose SERVER_ADDR couldn't exist in the cli;
          assuming it means "web sever address".

          I just checked out the php manual, and it says: 'SERVER_ADDR' The IP
          address of the server under which the current script is executing ...
          which doesn't necessarily rule out an ip address for the server when
          called from cli... :P

          Comment

          • lawrence k

            #6
            Re: how to tell CLI or webserver execution?

            On Oct 24, 11:29 am, lawpoop <lawp...@gmail. comwrote:
            Within a php file, how can you tell if it's being called by the
            webserver, or by the cli?
            >
            I have some pages that I developed to be served, but now we want to
            run some of them as cron jobs. I have to change some of the path
            references ( our cron job scripts reference include files in the
            webserver directory, and relative paths in the include files break
            when they are called by a script in another directory ), and to make
            everything more interoperable, I need to reference things based on
            whether it's being called by Apache or by the cli. Specifically, I'm
            making a reference to $_SERVER['DOCUMENT_ROOT'], which is empty when
            called by the cli.
            >
            So, I need to know if the script is being called by apache or the
            command line, so I can choose how to references certain included
            files. How should I do this?

            You mean you're creating an if() statement that references one path if
            the script is called in the web environment, but another path if it is
            not? Personally I'd rather move the necessary files to some central
            include file, something in the PHP include path. That way you won't
            have to change your if() statement, should the structure of the
            server, or the site, ever change.



            Comment

            • Jerry Stuckle

              #7
              Re: how to tell CLI or webserver execution?

              lawpoop wrote:
              On Oct 24, 12:03 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
              >
              >One way would be to see if something webserver specific is set, i.e.
              >>
              >if (isset($_SERVER['SERVER_ADDR'])) {
              >>
              >This should be true if running under a webserver, and false if not. Of
              >course there are several other values you could use, also.
              >>
              >
              Thanks, Jerry. I just wasn't sure if certain variables were more
              reliable than others. I suppose SERVER_ADDR couldn't exist in the cli;
              assuming it means "web sever address".
              >
              I just checked out the php manual, and it says: 'SERVER_ADDR' The IP
              address of the server under which the current script is executing ...
              which doesn't necessarily rule out an ip address for the server when
              called from cli... :P
              >
              >
              Actually, it does. You always have at least two ports on any internet
              connected computer - the external IP address and 127.0.0.1. But you
              aren't accessing the script via an IP address, so neither one is valid.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              Working...