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
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
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
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
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
Share on other sites

You can only use $argv to pass in initial arguments to the script. It's not much use if you don't know what function there going to be calling.

 

Also, the shebang needs to be on the first line not within the <?php code block.

Link to comment
Share on other sites

So there's no way to do it w/o sub-prompting. I mean, enter a character for task (this tells it which function to choose), then it will read the characters that follow the task (these are the arguments) If not, I'll do as told.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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