2016-11-05 65 views
0

处理一个简单的项目,我可以在浏览器的地址栏中输入192.168.x.xx/?$n,然后Raspberry Pi将发送一个等效于变量$n的pwm信号(使用WiringPi库) LED。我刚刚开始使用PHP,这就是为什么我需要帮助。现在我想到的是:使用PHP获取用户输入

<?php 
exec("gpio mode 1 pwm"); 
//declare $n as a variable (integer maybe) 
//$n = the user input value from browser (I only know $_GET, pretty sure it's not this right?) 
exec("gpio pwm 1 "+$n); 
?> 

将感谢任何帮助和指针。

+0

http://www.w3schools.com/php/php_forms.asp – zwcloud

回答

1

你有过这样的URL输入数据...

192.168.x.xx/?data=n    //n is of type int 


//php script 
<?php 
$n=$_GET['data']; 
exec("gpio mode 1 pwm"); 
//declare $n as a variable (integer maybe) 
//$n = the user input value from browser (I only know $_GET, pretty sure it's not this right?) 
exec("gpio pwm 1 ".$n); 
?> 
+0

感谢您的输入,用你建议的代码时,变量$ n是一个类型的字符串我是否正确?我可以将它转换为整数吗? – daQuincy

+0

这个答案应该引用GET参数'n',而不是'data'。这个价值应该被消毒;绝不信任用户输入。 – Chris

+0

@daQuincy,关于你的变量类型问题,PHP有一些关于打字的有趣想法。请参阅https://secure.php.net/manual/en/language.types.type-juggling.php,它以“PHP在变量声明中不需要(或支持)显式类型定义”开头;变量的类型由使用变量的上下文。“ (在任何情况下,如果你打算使用它'exec()'它应该是一个字符串。) – Chris