【问题标题】:empty $_POST variables空 $_POST 变量
【发布时间】:2015-07-08 20:17:00
【问题描述】:

我知道大多数人都有这个问题,很多人在这里问过,但似乎没有任何解决方案适合我。我尝试在php.ini 中增加post_max_size 等,我找到了一个解决方案link,即从.htaccess 文件中删除此行RewriteRule ^(.*)$ http://www.whitemagicsoftware.com/$1 [R=301,L],但我在.htaccess 文件中找不到任何.htaccess 文件在我的@987654327 @文件夹在任何地方。 我正在寻找近两天的解决方案,尝试更改代码但对我没有任何帮助。我也尝试使用$_GET$_REQUEST,但结果是一样的。我可以看到使用firebug 发送的变量值,但我的变量是空的。 任何帮助将不胜感激。
这是我的代码

<?php require ("database_connect.php");?>
<!DOCTYPE html>
<html>
<body>  
    <form method="POST" action="<?php echo ($_SERVER["PHP_SELF"])?>">
    Name : <input type="text" name="name"><br/>
    Password : <input type="password" name="password"><br/>
    <input type="submit" name="login" value="Log In">   
    </form>

    <?php
    //$name=$password="" ;
        if($_SERVER["REQUEST_METHOD"]=="POST" and isset($_POST["login"])){

            $name = testInput($_POST['name']);
            $password = testInput($_POST['password']);
            echo $name."<br>";
        }//if ends here

        //testInput function
        function testInput($data){
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
        }//testInput ends here


        if(isset($_POST["login"]) && isset($_POST["name"]) && isset($_POST["password"]) && !empty($_POST["name"]) && !empty($_POST["password"])){
        echo "Name is ".$name ."- Pass is ".$password."<br>";
        if($result = mysqli_query($conn,"SELECT * FROM users" )){           

            //print "rows are ".$result->num_rows."<br>";//number of rows

            if($result && mysqli_affected_rows($conn) >= 1){//If query was successfull and it has 1 or more than 1 result
            //echo "you are logged in<br>";
                ($row = mysqli_fetch_row($result));

                echo "$row[0] is ".$row[0]."- $row[1] is ".$row[1]."<br>";
                if( $row[0]==$name && $row[1]==$password){
                    echo "Welcome";
                } 
            }//if ends here


            /* free result set */
            $result->close();
        }       
        else {
            print "Wrong Credentials "."<br>";
            die(mysqli_error($conn));
        }
        }

        //close connection
        $conn->close();
    ?>
</body>
</html>

【问题讨论】:

  • 在打开&lt;?php标签error_reporting(E_ALL); ini_set('display_errors', 1);后立即将错误报告添加到文件顶部
  • 做过任何基本的调试,比如var_dump($_POST)?
  • 哦,选择 affected_rows 毫无意义。您没有影响(例如更改)任何行。您希望 mysqli_num_rows() 来查看找到/匹配的行数。
  • 尝试让 testInput 返回 $data
  • @Kisaragi 非常感谢你,它成功了:)。

标签: php apache .htaccess mod-rewrite


【解决方案1】:

尝试改变,

<form method="POST" action="<?php echo ($_SERVER["PHP_SELF"])?>">

to 

<form method="POST" action="">

【讨论】:

    【解决方案2】:

    我的问题已经解决,我的 testInput($data) 函数有问题。 这是更新后的代码,加上一个简单的提示给未来的用户,经常仔细查看你的代码,你会发现问题:)

    <?php require ("database_connect.php");
    error_reporting(E_ALL); ini_set('display_errors', 1);
    
    ?>
    <!DOCTYPE html>
    <html>
    <body>  
        <form method="POST" action="<?php echo ($_SERVER["PHP_SELF"])?>">
        Name : <input type="text" name="name"><br/>
        Password : <input type="password" name="password"><br/>
        <input type="submit" name="login" value="Log In">   
        </form>
    
        <?php
        //$name=$password="" ;
            if($_SERVER["REQUEST_METHOD"]=="POST" and isset($_POST["login"])){
    
                $name = testInput($_POST["name"]);
                $password = testInput($_POST["password"]);
    
            }//if ends here
    
            //testInput function
            function testInput($data){
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
    
                return $data;
            }//testInput ends here
    
    
            if(isset($_POST["login"]) && isset($_POST["name"]) && isset($_POST["password"]) && !empty($_POST["name"]) && !empty($_POST["password"])){
    
            if($result = mysqli_query($conn,"SELECT * FROM users WHERE name='$name' and password='$password'" )){
    
                if($result && mysqli_num_rows($result) >= 1){//If query was successfull and it has 1 or more than 1 result
    
                echo "Welcome <br>";
    
                }//if ends here
                else {
                echo "Wrong Credentials <br>";
            }
    
                /* free result set */
                $result->close();
            }       
            else {
                echo "Wrong Credentials <br>";
                die(mysqli_error($conn));
            }
            }
    
            //close connection
            $conn->close();
        ?>
    </body>
    </html>
    

    【讨论】:

      【解决方案3】:

      我猜 htmlspecialchars() 应该在 stripslashes() 之前运行,或者只是删除文件中的 htmlspecialchars()。 :)

          function testInput($data){
              $data = trim($data);
              $data = stripslashes($data);
      
              return $data;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-05
        • 2012-11-11
        相关资源
        最近更新 更多