user2943416
user2943416

Reputation: 1

How can I get php variables into a linux script?

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

Upvotes: 0

Views: 183

Answers (3)

Alexander L. Belikoff
Alexander L. Belikoff

Reputation: 5721

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.

Upvotes: 1

anubhava
anubhava

Reputation: 785551

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

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

Upvotes: 0

Andrew Sledge
Andrew Sledge

Reputation: 10351

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.

Upvotes: 1

Related Questions