0

I'm trying to work out how to get variables from php into a script because I want to create a user registration page so users can register and the php will call a script and send the variables to it.

Specifically this line i'm having trouble with (its in the php page):

PHP: $results = bash /home/*******/public_html/add_user $login '$pwd';

add_user.sh:

#!/bin/bash
adduser -u "$1" -p "$2" -g users -s /bin/bash
1

3 Answers 3

1

exec

$results = exec("bash /home/*******/public_html/add_user $login '$pwd'");

Keep in mind that the user running this process (the web user, probably www-data, www, web, etc.) must have sudo or elevated access to do this. I would also like to caution against doing this unless you've really taken the time to consider all implications and consequences if you are exposing this to the web.

1

If you really want to call an outside program from PHP, you can use a call like system() or exec() to do that. You will have to construct your command line in PHP from PHP variables.

Having said that, this approach is one of the most common sources of security holes in web applications. You will have to go out of your way making sure the data you are about to pass are valid and correct (think what's going to happen if $login contains "; rm -rf /").

Another consideration is the fact that adduser requires superuser privileges. Last time I checked, shell scripts did not honor SUID, so I'm not sure if the straightforward approach would work - you might need to use sudo or rewrite the script in Perl (for example) and use suidperl.

0

You can echo variables from your PHP scripts and read this into shell variables as:

read -r user pass < <(/path/to/php/script.php)

Not the answer you're looking for? Browse other questions tagged or ask your own question.