PHP gets more than one argument on the command line

At the php command line, I can only read the first argument. When I add the site name, the program does not interrupt before entering the password.

echo "Site name: "; $handle = fopen ("php://stdin","r"); $base_name = trim(fgets($handle)); fclose($handle); echo "Password:"; $handle = fopen ("php://stdin","r"); $base_password = trim(fgets($handle)); fclose($handle); 

How can I read these two variables from stdin?

+4
source share
2 answers

Your arguments passed during the command will be in the global $argv , as well as the super global $_SERVER["argv"] with $argv[0] and $_SERVER["argv"][0] , which is called.

Useful parsing function, e.g. calling ./myscript.php --user=root --password=foobar

 function parse_argvs(){ if( $params = $_SERVER["argv"] ){ $file = array_shift( $params ); while( $params ){ $param = array_shift( $params ); switch( strspn( $param, "-" ) ){ case( 1 ): $OPTS[ trim( $param, " -" ) ] = array_shift( $params ); break; case( 2 ): list( $key, $value ) = explode( "=", $param ); $OPTS[ trim( $key, " -" ) ] = $value; break; default: $OPTS[ $param ] = true; break; } } } return $OPTS ?: array(); } 

Something like

 $parsed = parse_argvs(); echo $parsed['user']; //root echo $parsed['password']; //password 

These are the actual command line arguments passed during the call. Hope this helps.

+1
source

Try the following:

 $base_name = readline("Site Name: "); $base_password = readline("Password: "); 

PHP Readline Function

0
source

All Articles