我的登录表单中有一些问题,如何解决?

时间:2014-04-16 12:17:46

标签: php login login-script

我输入信息并点击"提交查询"要登录,它会将我重定向到空白页

  • 我已经注册了试图登录的信息。

  • 并在数据库中检查用户是否存在。这是代码:

    <?php
    // ERROR DEBUG THINGY
    $showErrors = (isset($_GET['debug'])) ? true : false;
    
    if(!$showErrors) {
        ini_set('display_errors', 'Off');
    }
    else {
        error_reporting(E_ALL);
        ini_set('display_errors', 'On');
    }
    
    // DATABASE START
    $servername = "localhost";
    $username = "root";
    $conn = mysql_connect($servername, $username) or die (mysql_error());
    mysql_select_db("test", $conn);
    
    // WE START THE SESSION
    session_start();
    
    // START LOGIN PROCESS
    if(isset($_SESSION['logged'])) {
        print_secure_content(); //CONTENT THAT'S ECHOED ONCE YOU LOG IN
    }
    else {
        if(!$_SESSION['logging']) {       //IF LOGGING SESSION ISNT RUNNING
            $_SESSION['logging'] = true; //RETURNS THE USER BACK TO THE LOGIN FORM
            loginform();                //SHOWS THE LOGIN FORM
        }
        else if($_SESSION['logging']) {
            $numberOfRows = checkpass(); //CHECK IF THE USER ALREADY EXISTS
            if($numberOfRows == 1) {    //IF OLD USER LOG HIM IN
                $_SESSION['id'] = $_POST['username'];
                $_SESSION['logged'] = true;
                echo "<h1> You have logged in successfully </h1>";
                print_secure_content();
                print_user_details();
            }
            else {
                echo "You have entered a wrong username or password, please try again.<br />";
                loginform();
            }
        }
    }
    
    // DATABASE CLOSE
    mysql_close();
    
    // FUNCTIONS!!!
    
    function loginform() { //THE LOG IN FORM
        echo "Please login <br />
        <form method=\"POST\" action=\"?\">
                USERNAME<br />
                <input name='username' type='text'><br />
                PASSWORD<br />
                <input name='password' type='password'><br />
        <input type='submit' name='login'></form>
        <p><a href='registerform.php'> Register Here! </a></hr>";
    }
    
    function print_user_details() { //CHECK PASSWORD
        if(isset($_POST['username']) && isset($_POST['password'])) {
            $sql = "SELECT * FROM users WHERE name = '{$_POST['username']}' and password = '{$_POST['password']}'";
            $result = mysql_query($sql, $conn) or die (mysql_error());
            return mysql_num_rows($result);
        }
    }
    
    function checkpass() { //CHECK PASSWORD
        if(isset($_POST['username']) && isset($_POST['password'])) {
            $sql = "SELECT * FROM users WHERE name = '{$_POST['username']}' and password = '{$_POST['password']}'";
            $result = mysql_query($sql, $conn) or die (mysql_error());
            return mysql_num_rows($result);
        }
    }
    
    function print_secure_content() { // ECHO ONCE YOU LOG IN
        echo 'Hello '. $_SESSION['id'] .'!<br />';
        echo 'Only a logged in user can see this. <br /> <a href="logout.php"> Logout </a>';
    }
    

    &GT;

2 个答案:

答案 0 :(得分:3)

可能存在一些语法错误。

转到 php.ini 文件
display_errors = off 更改为上保存。
重新启动服务器
检查前端。您可能会发现错误。纠正它。
再次重启服务器以查看更改。

答案 1 :(得分:0)

$ sql =&#34; SELECT * FROM users WHERE name =&#39;&#34;。$ _ POST [&#39; username&#39;]。&#34;&#39;和密码=&#39;&#34;。$ _ POST [&#39;密码&#39;]。&#34;&#39;&#34 ;;

像这样的用户sql查询