PHP Command Line called via exec();

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

    PHP Command Line called via exec();

    Hi there

    If I enter the following at the command line, I
    successfully get an email sent to the correct name/email:


    php /var/www/html/admin/adminscripts/welcome-cli.php
    'Me' 'me@me.net'


    However, if I visit
    "http://www.example.com/example.php?
    vID=Me&vUser=me @me.net"

    which consists of the following:

    <?php

    @$vID = $_REQUEST['vID'];
    @$vUser = $_REQUEST['vUser'];

    exec('php /var/www/html/admin/adminscripts/welcome-
    cli.php $vID $vUser');

    ?>

    the email does not get sent.


    I thought, "hmm, maybe $vID and $vUser are getting passed
    as '$vID' and '$vUser' instead of 'Me' and 'me@me.net' so
    I tried: (please excuse the line breaks)

    <?php

    @$vID = $_REQUEST['vID'];
    @$vUser = $_REQUEST['vUser'];

    $vExecString = "php
    /var/www/html/admin/adminscripts/welcome-cli.php $vID
    $vUser";

    exec('$vExecStr ing');
    ?>


    but still, the e-mail does not get sent. Does anyone
    have any ideas/suggestions on how I can do this properly?
    I've also tried system() and shell_exec() but to no
    avail.

    You might be asking why I am taking this route... the
    answer is that the REAL script (the above is just a test)
    will be sending 4,000+ emails, and I'd like to send the
    website user to a new webpage immediately instead of
    having stare at a blank browser window until the emails
    get sent. Hence, my idea was to call the e-mail script
    via the command line and send the user on their way to
    view other pages while the mail gets sent in the
    background.....

    The system is a Linux box, Apache 2, PHP 5.0.2

    I did not include any source from "welcome-cli.php"
    because it is working properly via the command line
    (telnet).

    Thanks in advance....

  • Chris B

    #2
    Re: PHP Command Line called via exec();

    > <?php[color=blue]
    >
    > @$vID = $_REQUEST['vID'];
    > @$vUser = $_REQUEST['vUser'];
    >
    > $vExecString = "php
    > /var/www/html/admin/adminscripts/welcome-cli.php $vID
    > $vUser";
    >
    > exec('$vExecStr ing');
    > ?>[/color]

    The one thing I'd do is put

    echo $_REQUEST['vID']; to see if the variable is actually there..

    btw, what are the @ for?

    Comment

    • Kenneth Downs

      #3
      Re: PHP Command Line called via exec();

      Good Man wrote:
      [color=blue]
      > Hi there
      >
      > If I enter the following at the command line, I
      > successfully get an email sent to the correct name/email:
      >
      >
      > php /var/www/html/admin/adminscripts/welcome-cli.php
      > 'Me' 'me@me.net'
      >
      >
      > However, if I visit
      > "http://www.example.com/example.php?
      > vID=Me&vUser=me @me.net"
      >
      > which consists of the following:
      >
      > <?php
      >
      > @$vID = $_REQUEST['vID'];
      > @$vUser = $_REQUEST['vUser'];
      >
      > exec('php /var/www/html/admin/adminscripts/welcome-
      > cli.php $vID $vUser');
      >[/color]

      I ran into something like this when I first started with PHP. As a newbie,
      I could not figure out if it was me or PHP, but it seemed that various
      exec() and shell_exec() commands simply did not work. Nothing happened,
      repeatedly. Ultimately I used back-tick, which worked, and I have never
      gone back to figure it out.

      So you can execute something like this:

      $mycommand = "php /var/.../welcome-cli.php $Me $myemail";
      `$mycommand`;

      --
      Kenneth Downs
      Secure Data Software, Inc.
      (Ken)nneth@(Sec )ure(Dat)a(.com )

      Comment

      • Tim Van Wassenhove

        #4
        Re: PHP Command Line called via exec();

        On 2005-05-06, Chris B <zen19389@REMOV Ezen.co.uk> wrote:[color=blue]
        > The one thing I'd do is put
        > echo $_REQUEST['vID']; to see if the variable is actually there..[/color]

        I would use isset, array_key_exist s, empty to test that.

        And i would use
        ini_set('error_ reporting', E_ALL);
        ini_set('displa y_errors', TRUE);

        to make sure PHP warns me about eventual problems...
        [color=blue]
        > btw, what are the @ for?[/color]




        --
        Met vriendelijke groeten,
        Tim Van Wassenhove <http://www.timvw.info>

        Comment

        • Good Man

          #5
          Re: PHP Command Line called via exec();

          Kenneth Downs <knode.wants.th is@see.sigblock > wrote in
          news:mp4sk2-tgm.ln1@pluto.d ownsfam.net:
          [color=blue]
          > I ran into something like this when I first started with PHP. As a
          > newbie, I could not figure out if it was me or PHP, but it seemed that
          > various exec() and shell_exec() commands simply did not work. Nothing
          > happened, repeatedly. Ultimately I used back-tick, which worked, and
          > I have never gone back to figure it out.
          >
          > So you can execute something like this:
          >
          > $mycommand = "php /var/.../welcome-cli.php $Me $myemail";
          > `$mycommand`;[/color]

          Wow.

          Thanks for your post. In a million years I would not have tried that, and
          of course, it worked!

          THANKS!

          Now I'm going to go off and investigate this back-tick business.....

          Comment

          • Kenneth Downs

            #6
            Re: PHP Command Line called via exec();

            Good Man wrote:
            [color=blue]
            > Kenneth Downs <knode.wants.th is@see.sigblock > wrote in
            > news:mp4sk2-tgm.ln1@pluto.d ownsfam.net:
            >[color=green]
            >> I ran into something like this when I first started with PHP. As a
            >> newbie, I could not figure out if it was me or PHP, but it seemed that
            >> various exec() and shell_exec() commands simply did not work. Nothing
            >> happened, repeatedly. Ultimately I used back-tick, which worked, and
            >> I have never gone back to figure it out.
            >>
            >> So you can execute something like this:
            >>
            >> $mycommand = "php /var/.../welcome-cli.php $Me $myemail";
            >> `$mycommand`;[/color]
            >
            > Wow.
            >
            > Thanks for your post. In a million years I would not have tried that, and
            > of course, it worked!
            >
            > THANKS!
            >
            > Now I'm going to go off and investigate this back-tick business.....[/color]

            Glad it helped!

            If you come from a Windows background, you would never have heard of
            backtick, it is a Unix thing that I learned when I moved to Linux about 4
            years ago. You can put it into shell scripts, Perl, and PHP that i know
            of, and probably many more.

            Basically, the three quoting methods are:

            Single Quotes are completely literal, so:

            $variable = 'something';
            $myvar = 'This $variable will not be substituted\n';

            will actually contain the string '$variable' instead of the substituted
            value 'something'. It will also contain a slash followed by an n, the
            string '\n', instead of a newline character.

            Double quotes will evaluate variables and control characters, so:

            $myvar = "This $variable will not be substituted\n";

            will put "This something will...." into $myvar, with a newline at the end.

            Backtick is fascinating because it means *execute* the string and return the
            results. It will also accept interpolation (at least in Bash it does, I'm
            kind of assuming it does in PHP), so you can do this:

            $somedir = "/home/ken/letters/";
            $myfiles = `ls $somedir`;

            and now $myfiles contains a directory listing. Supercool.


            --
            Kenneth Downs
            Secure Data Software, Inc.
            (Ken)nneth@(Sec )ure(Dat)a(.com )

            Comment

            • sandy

              #7
              Re: PHP Command Line called via exec();

              hi,

              just saw this post while killing time. if you would want to pass
              arguments to your php scripts you can use this:

              exec("/path/to/php -f your_file_name. php $argument1 $argument2");

              and when your_file_name. php is executed the arguments are available in
              the $argv array which is filled with the arguments you gave to the
              file.

              so you can make another file say 'example.php' and pass it arguments in
              query string like this
              example.php?arg ument1=me&argum ent2=me@somewhe re.com

              and use the $_GET array to pass the values to the exec command.

              Hope this helps

              Comment

              • Mike Willbanks

                #8
                Re: PHP Command Line called via exec();

                Just to add on to this,
                [color=blue]
                > just saw this post while killing time. if you would want to pass
                > arguments to your php scripts you can use this:
                >
                > exec("/path/to/php -f your_file_name. php $argument1 $argument2");
                >
                > and when your_file_name. php is executed the arguments are available in
                > the $argv array which is filled with the arguments you gave to the
                > file.
                >
                > so you can make another file say 'example.php' and pass it arguments in
                > query string like this
                > example.php?arg ument1=me&argum ent2=me@somewhe re.com
                >
                > and use the $_GET array to pass the values to the exec command.[/color]

                Do not pas the arguments straight in there without using escapeshellarg
                for arguments and commands with escapeshellcmd :)

                Mike

                Comment

                • astro_dave
                  New Member
                  • Jun 2006
                  • 1

                  #9
                  Difference between `$mycommand`; and shell_exec($myc ommand);

                  I ran into something like this when I first started with PHP. As a newbie,
                  I could not figure out if it was me or PHP, but it seemed that various
                  exec() and shell_exec() commands simply did not work. Nothing happened,
                  repeatedly. Ultimately I used back-tick, which worked, and I have never
                  gone back to figure it out.

                  So you can execute something like this:

                  $mycommand = "php /var/.../welcome-cli.php $Me $myemail";
                  `$mycommand`;

                  --
                  Kenneth Downs
                  Secure Data Software, Inc.
                  (Ken)nneth@(Sec )ure(Dat)a(.com )
                  For some reason when doing a shell_exec, you can't pass php variables. The following will not pass the variables $Me and $myemail:
                  shell_exec('php /var/.../welcome-cli.php $Me $myemail');

                  whereas this will :
                  `php /var/.../welcome-cli.php $Me $myemail`;

                  I prefer to use shell_exec than the backticks as it makes code more readable. The following is your example, using shell_exec:
                  $mycommand = "php /var/.../welcome-cli.php $Me $myemail";
                  shell_exec($myc ommand);

                  As you can see it is almost exactly the same. These problems can be diagnosed when using a colourful editor (one that interprets the code and assigns text different colours based on how the code is interpreted).

                  astro_dave.

                  Comment

                  Working...