空$ _POST变量

时间:2015-07-08 20:17:01

标签: php apache .htaccess mod-rewrite

我知道大多数人都有这个问题,很多人在这里问过,但似乎没有任何解决方案对我有用。我已尝试增加post_max_size中的php.ini等,我找到了一个解决方案link,即从RewriteRule ^(.*)$ http://www.whitemagicsoftware.com/$1 [R=301,L]文件中删除此行.htaccess,但我不能在我的.htaccess文件夹中找到任何xampp个文件。 我正在寻找一个近两天的解决方案,尝试更改代码,但没有任何对我有用。我也尝试使用$_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>

3 个答案:

答案 0 :(得分:0)

尝试改变,

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

to 

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

答案 1 :(得分:0)

我的问题已经解决,我的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>

答案 2 :(得分:0)

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

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

        return $data;
    }