Jump to content

php cli help


mpsn

Recommended Posts

Hi, I'm getting an infinite loop using PHP and CLI.

 

<?php
#!/usr/bin/php -q
$userName = fread(STDIN,10);
$password = fread(STDIN,10);
$isAuthenticated = false;

$realUserId = "aaa";
$realPassword = "pass";

while( $isAuthenticated!=true )
{
echo "Please login to begin: \n";
echo "Enter username: ".$userName."\n";
echo "Enter password: ".$password;

//now check to make sure is registerd user
//$userId = $u->userLogin($userName, $password);
if($userName != $realUserId && $password != $realPassword)
	echo "Invalid username or password. Please try again.\n";
else
{
	echo " You are logged in! You can begin.\n";
	$isAuthenticated = true;
}
}

?>

 

All help appreciated.

Link to comment
https://forums.phpfreaks.com/topic/252358-php-cli-help/
Share on other sites

#!/usr/bin/php -q
<?php

$isAuthenticated = false;

$realUserId = "aaa";
$realPassword = "pass";

while( $isAuthenticated!=true )
{
echo "Please login to begin: \n";
echo "Enter username: \n";
        $userName = fread(STDIN,10);
echo "Enter password: \n";
        $password = fread(STDIN,10);

//now check to make sure is registerd user
//$userId = $u->userLogin($userName, $password);
if($userName != $realUserId && $password != $realPassword)
	echo "Invalid username or password. Please try again.\n";
else
{
	echo " You are logged in! You can begin.\n";
	$isAuthenticated = true;
}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293754
Share on other sites

This is related I guess, why doesn't it exit while loop when i type 'q'?

 

<?php
$task = "f";

function showTasks()//h
{
echo "TASKS:\n";
echo "b - show breakfast menu;"
        echo "l - show lunch menu;"
echo "q - quit: Exit program\n";
echo "h: to view this list of options\n\n\n";
}

while($task != 'q')
{
echo "Please choose a task: ";
$task = fread(STDIN,10);
echo $task;
echo "\n";

if( trim($task) == "h")
	showTasks();
       //other tasks will call other functions, i do later
}
echo "Thanks for using our menu!";
?>

 

All help appreicate.

Link to comment
https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293763
Share on other sites

Is it possible to use both fread(STDIN,10) with $argv in php cli?

 

function foo($a,$b);

function foobar($c);

 

let's say I have these functions(I'm aware it's function headers), so I wanto use fread(STDIN,10) to choose the function, and $argv to pass in arguments to the above functions.

 

Can someone show me quickly how to approach this, i'd appreciate it.

Link to comment
https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293789
Share on other sites

Here is concrete exampel of what i mean, let's say a simple calculator class with add, factorial finding, and government sales tax retrieiving, why doesn't this work?

 

<?php
#!/usr/bin/php -q
class Calculator
{
function factorial($int)
{
	if ($int == 1)
		return 1;
	else 
		return $int*factorial($int-1);
}

function add($num1, $num2)
{
	return $num1 + $num2;
}

function getGSTof($price)
{
	return 0.05*$price;
}
}

//RUNNER
$c = new Calculator();

$task = "";
//TASKS:f for factorial, then 1 arg, a for add, then 2 args, g for gst, then 1 arg
while($task != "q")
{
echo "Please choose a task: ";
$task = trim( fread(STDIN,10)  );

//FACTORIAL
if ( $task == "f" )
{
	$num = $argv[1];
	$factorial = $c->factorial($num);
	echo $factorial;
	echo "\n";
}

//ADDITION
if ( $task == "a" )
        {
	$num = $argv[1];
	$num2 = $argv[2];
	$sum = $c->add($num, $num2);
	echo $sum;
	echo "\n";
        }

//GET GST
if ( $task == "g" )
{
	$price = $argv[1];
	$gst = $c->getGSTof($price);
	echo $gst;
	echo "\n";
}

//QUIT
if($task == "q" )
	break;
}//END WHILE

echo "\n\n\n\nEnd Calculator program";
?>

 

All help apprciated.

Link to comment
https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293799
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.