By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,719 Members | 1,445 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,719 IT Pros & Developers. It's quick & easy.

PHP Command Line called via exec();

P: n/a
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' 'm*@me.net'
However, if I visit
"http://www.example.com/example.php?
vI*************@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 'm*@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('$vExecString');
?>
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....

Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
> <?php

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

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

exec('$vExecString');
?>


The one thing I'd do is put

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

btw, what are the @ for?
Jul 17 '05 #2

P: n/a
Good Man wrote:
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' 'm*@me.net'
However, if I visit
"http://www.example.com/example.php?
vI*************@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');


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)
Jul 17 '05 #3

P: n/a
On 2005-05-06, Chris B <ze******@REMOVEzen.co.uk> wrote:
The one thing I'd do is put
echo $_REQUEST['vID']; to see if the variable is actually there..
I would use isset, array_key_exists, empty to test that.

And i would use
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

to make sure PHP warns me about eventual problems...
btw, what are the @ for?


http://www.php.net/manual/en/languag...rorcontrol.php
--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #4

P: n/a
Kenneth Downs <kn**************@see.sigblock> wrote in
news:mp************@pluto.downsfam.net:
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`;


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.....
Jul 17 '05 #5

P: n/a
Good Man wrote:
Kenneth Downs <kn**************@see.sigblock> wrote in
news:mp************@pluto.downsfam.net:
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`;


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.....


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)
Jul 17 '05 #6

P: n/a
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
ex***********************************@somewhere.co m

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

Hope this helps

Jul 17 '05 #7

P: n/a
Just to add on to this,
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
ex***********************************@somewhere.co m

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


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

Mike
Jul 17 '05 #8

P: 1
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($mycommand);

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.
Jun 6 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.