【问题标题】:Variable wont echo变量不会回显
【发布时间】:2010-12-25 04:27:55
【问题描述】:

我有以下代码,当您输入一个值时,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 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';

 ?>

login_form 是这样的:

<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>

身份验证令牌部分并不重要,它只是当我在用户名文本框中输入一个值并按下登录按钮时,用户名不会回显,var_dump 返回字符串(0)并且 print_r 只是空白。

【问题讨论】:

    标签: php variables


    【解决方案1】:

    愚蠢的错误

    if ($username = '')   <-- this is an assignment
    

    应该是这个

    if ($username == '')  <-- this is comparison
    

    【讨论】:

      【解决方案2】:
      if ($username = '')
      

      您缺少=,因此您将一个空字符串分配给$username。改成

      if ($username == '')
                     ^-- note the 2 equal signs.
      

      【讨论】:

      • 有人说用左边的常数写比较更安全,例如if ('' == $username),如果你只输入一个 =,你会得到一个 unexpected = 错误。
      • 说得有道理...凌晨 4 点 43 分不醒也应该有帮助,我还可以问:如果所有答案都正确,你会勾选谁?第一个正确答案?
      【解决方案3】:

      这一行是赋值,而不是比较:

      if ($username = '')
      

      你想要:

      if ($username == '')
      

      【讨论】:

        【解决方案4】:
        //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>
        

        【讨论】:

        • &lt;form method="POST" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt; 这是不必要的......
        • 是的,这肯定是不必要的,但是保持在更安全的一面有什么害处?阅读这篇小文章:thefutureoftheweb.com/blog/… 也请阅读评论。 :-)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-05
        • 2022-08-19
        • 2015-05-27
        • 1970-01-01
        • 2012-05-03
        • 2018-01-14
        • 2022-01-25
        相关资源
        最近更新 更多