Php 变量不会回声
我有以下代码,当您键入一个值时,var$username不会回显。Php 变量不会回声,php,variables,Php,Variables,我有以下代码,当您键入一个值时,var$username不会回显。 //TODO: SET AUTH TOKEN as random hash, save in session $auth_token = rand(); if (isset($_POST['action']) && $_POST['action'] == 'Login') { $errors = array(); //USED TO BUILD UP ARRAY OF ERRORS WHICH
//TODO: SET AUTH TOKEN as random hash, save in session
$auth_token = rand();
if (isset($_POST['action']) && $_POST['action'] == 'Login')
{
$errors = array(); //USED TO BUILD UP ARRAY OF ERRORS WHICH ARE THEN ECHOED
$username = $_POST['username'];
if ($username = '')
{
$errors['username'] = 'Username is required';
}
echo $username; // var_dump($username) returns string 0
}
require_once 'login_form.html.php';
?>
登录表格如下:
<form method="POST" action="">
<input type="hidden" name="auth_token" value="<?php echo $auth_token ?>">
Username: <input type="text" name="username">
Password: <input type="password" name="password1">
<input type="submit" name="action" value="Login">
</form>
您缺少一个=
,因此为$username
分配了一个空字符串。换成
if ($username == '')
^-- note the 2 equal signs.
愚蠢的错误
if ($username = '') <-- this is an assignment
如果($username='')此行是赋值,而不是比较:
if ($username = '')
你想要:
if ($username == '')
在登录表单中:(操作属性..)
有些人说用左边的常量写比较更安全,例如如果('''=$username)
,如果你只输入一个=
,你会得到一个意外的=
错误。这是有道理的…早上4:43不醒来也会有帮助,我还可以问:如果所有答案都正确,你会打勾给谁?第一个正确答案?是的,这肯定是不必要的,但保持安全有什么害处?读这篇小文章:也要读命令。:-)
if ($username = '')
if ($username == '')
//TODO: SET AUTH TOKEN as random hash
$auth_token = rand();
if (isset($_POST['action']) && $_POST['action'] == 'Login')
{
$errors = array(); //USED TO BUILD UP ARRAY OF ERRORS WHICH ARE THEN ECHOED
$username = $_POST['username'];
if ($username == '') // **You are assiging not comparing**
{
$errors['username'] = 'Username is required';
}
echo $username; // var_dump($username) returns string 0
}
require_once 'login_form.html.php';
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="auth_token" value="<?php echo $auth_token ?>">
Username: <input type="text" name="username">
Password: <input type="password" name="password1">
<input type="submit" name="action" value="Login">
</form>