网站适用于本地开发,但托管平台上有错误

时间:2017-10-17 23:18:39

标签: php mysqli registration web-hosting login-script

如果我问一个对你们这些人来说可能很容易的问题,我会提前道歉。我的网站出现了一些错误。它适用于我的本地开发环境,但在我使用的托管平台(000webhost)上,我在以下页面中收到以下错误:

注册页面(pt-5-register.php) - 当我填写页面的所有细节时,我会收到以下错误:Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd4/573/2779573/public_html/pt-5-register.php:1) in /storage/ssd4/573/2779573/public_html/pt-5-register.php on line 123。在“pt-5-register.php”文件中,我已经注意到发生错误的行。请留意第123行。Image of attempted registration - username: 1st.test

但是,当我登录数据库时,可以看到新创建的用户。我甚至为日志创建了一个脚本,它记录了时间和日期。时间和日期对应于我尝试注册的时间。不幸的是,用户名不会出现在数据库中。

然后我进入登录页面查看发生了什么,输入我'注册'的详细信息,并收到以下错误:

Notice: Undefined variable: link in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 47

Warning: mysqli_prepare() expects parameter 1 to be mysqli, null given in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 47

Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, null given in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 105

Notice: Undefined variable: link in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 110

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 110

在“pt-5-login.php”文件中,我注意到发生错误的行。注意第47,105和110行。

Image of failed login attempt

以下代码是发生此网站错误的网页。该网站的链接将根据要求发送。

请帮助,因为我相信我离完成此登录/注册系统并不远。

以下是配置页面的PHP代码“config.php”

<?php   

/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */

define("DB_SERVER", "localhost");
define("DB_USERNAME", "***********");
define("DB_PASSWORD", "***********");
define("DB_DATABASE", "***********");

// Attempt to connect to MySQL database 
$connect = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

// Check connection
if($connect === false)
{
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

?> 

以下是注册页面的PHP / HTML代码“pt-5-register.php”

<!-- ----------------------------------------------------------------------- */
/* Taken from "Tutorial Republic - PHP MySQL Login System" - 
/* ------------------------------------------------------------------------ -->

----PHP CODE----

<?php
// Include config file
require_once 'config.php';

// Define variables and initialize with empty values
$username = $password = $confirm_password = "";
$username_err = $password_err = $confirm_password_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST")
{

    // Validate username
    if(empty(trim($_POST["username"])))
    {
        $username_err = "Please enter a username.";
    } 

    else
    {

    // Prepare a select statement    
    $sql = "SELECT id FROM users WHERE username = ?";

        if($stmt = mysqli_prepare($connect, $sql))
        {

        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_username);

            // Set parameters
            $param_username = trim($_POST["username"]);

            // Attempt to execute the prepared statement           
           if(mysqli_stmt_execute($stmt))
           {

            /* store result */
            mysqli_stmt_store_result($stmt);

                if(mysqli_stmt_num_rows($stmt) == 1)
                {
                    $username_err = "This username is already taken.";
                } 

                else
                {
                    $username = trim($_POST["username"]);
                }

            } 

            else
            {   
            echo "Oops! Something went wrong. Please try again later.";
            }

        }


         // Close statement
         mysqli_stmt_close($stmt);
    }

    // Validate password
    if(empty(trim($_POST['password'])))
    {     
     $password_err = "Please enter a password.";     
    } 

    elseif(strlen(trim($_POST['password'])) < 6)
    {     
     $password_err = "Password must have atleast 6 characters.";
    } 

    else
    {
        $password = trim($_POST['password']);
    }

    // Validate confirm password
    if(empty(trim($_POST["confirm_password"])))
    {      
      $confirm_password_err = 'Please confirm password.';     
    } 

    else
    {    
    $confirm_password = trim($_POST['confirm_password']);     
     if($password != $confirm_password)
     {      
      $confirm_password_err = 'Password did not match.';      
      }
    }

    // Check input errors before inserting in database
    if(empty($username_err) && empty($password_err) && empty($confirm_password_err))
    {

        // Prepare an insert statement
        $sql = "INSERT INTO users (username, password) VALUES (?, ?)";

        if($stmt = mysqli_prepare($connect, $sql))
        {

        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);

            // Set parameters
            $param_username = $username;
            $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash

            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt))

            {
            // Redirect to login page
            header("location: pt-5-login.php"); // LINE 123 - ERROR OCCURRED
            } 

            else
            {
             echo "Something went wrong. Please try again later.";
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);
    }

    // Close connection
    mysqli_close($connect);

}

?>


<!-- ----------------------------------------------------------------------------------- */
/* Temporary suspension of "Tutorial Republic - PHP MySQL Login System" Tutorial
/* ------------------------------------------------------------------------------------ -->




----HTML CODE----

<!doctype html>
<html>
    <head>
        <title>Learning Portal</title> <!-- CHANGE THESE IN ALL FILES RELATING TO THIS PROJECT! -->
        <meta name="description" content="Learning Portal">
        <meta name="keywords" content="Learning Portal EFREI">
        <meta name="author" content="D.Jackson">
        <link rel="stylesheet" type="text/css" href="NHS.css" media="screen">
        <link rel="icon" type="image/ico" href="favicon.ico">
        <link href="https://fonts(dot)googleapis(dot)com/css?family=Jura" rel="stylesheet">
    </head>

    <!-- <div> is to group block elements for CSS formatting -->

    <body>
        <div id="container"> 
            <header><a href="pt-5-home.htm">Register</a>
            </header>
            <!-- The htm. above which would connect to the main page was originally 
            "mars.htm", then "NHS.htm" and is now called "pt-5-home.htm" -->

            <!--
            <div id="main">
                <section>
                    <p>Please enter your details:
                    </p>

                    -->




                    <!-- ----------------------------------------------------------------------- */
                    /* Resuming "Tutorial Republic - PHP MySQL Login System"
                    https://www.tutorialrepublic.com/php-tutorial/php-mysql-login-system.php
                    /* ------------------------------------------------------------------------ -->

                    <!--

                    </head>
                    <body> 

                    -->

                    <div class="wrapper">
                    <h2>Sign Up</h2>
                    <p>Please fill this form to create an account.</p>
                    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">


                    <div class="form-group 
                    <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
                    <label>Username:<sup>*</sup></label>
                    <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
                    <span class="help-block"><?php echo $username_err; ?></span>
                    </div>    

                    <div class="form-group 
                    <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">             
                     <label>Password:<sup>*</sup></label>
                     <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
                     <span class="help-block"><?php echo $password_err; ?></span>
                    </div>

                    <div class="form-group 
                    <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
                    <label>Confirm Password:<sup>*</sup></label>
                    <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
                    <span class="help-block"><?php echo $confirm_password_err; ?></span>
                    </div>

                    <div class="form-group">
                    <input type="submit" class="btn btn-primary" value="Submit">
                    <input type="reset" class="btn btn-default" value="Reset">
                    </div>

            <p>Already have an account? <a href="pt-5-login.php">Login here</a>.</p>
        </form>
    </div>    
</body>
</html>       

以下是登录页面的PHP / HTML代码“pt-5-login.php”

<!-- ----------------------------------------------------------------------- */
/* Taken from "Tutorial Republic - PHP MySQL Login System"
/* ------------------------------------------------------------------------ -->

----PHP CODE----

<?php
// Include config file
require_once 'config.php';

// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST")
{

    // Check if username is empty
    if(empty(trim($_POST["username"])))
    {
        $username_err = 'Please enter username.';
    } 

    else
    {
        $username = trim($_POST["username"]);
    }

    // Check if password is empty
    if(empty(trim($_POST['password'])))
    {
        $password_err = 'Please enter your password.';
    } 

    else
    {
        $password = trim($_POST['password']);
    }

    // Validate credentials
    if(empty($username_err) && empty($password_err))
    {

     // Prepare a select statement
     $sql = "SELECT username, password FROM users WHERE username = ?";

        if($stmt = mysqli_prepare($link, $sql)) // LINE 47 - ERROR OCCURRED
        } 
        {

        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_username);

            // Set parameters
            $param_username = $username;

            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt))
            {

            // Store result
            mysqli_stmt_store_result($stmt);

                // Check if username exists, if yes then verify password
                if(mysqli_stmt_num_rows($stmt) == 1)
                {                    

                // Bind result variables
                mysqli_stmt_bind_result($stmt, $username, $hashed_password);
                if(mysqli_stmt_fetch($stmt))
                    {                    
                    if(password_verify($password, $hashed_password))
                    {

                     /* Password is correct, so start a new session and                    
                    save the username to the session */

                    session_start();
                    $_SESSION['username'] = $username;      
                    header("location: welcome.php");
                    } 

                            else
                            {
                            // Display an error message if password is not valid
                            $password_err = 'The password you entered was not valid.';
                            }
                    }
                } 

                else
                {
                    // Display an error message if username doesn't exist
                    $username_err = 'No account found with that username.';
                }
            } 

            else
            {
                echo "Oops! Something went wrong. Please try again later.";
            }

        }

        // Close statement
        mysqli_stmt_close($stmt); // LINE 105 - ERROR OCCOURED
        } 

    }

    // Close connection
    mysqli_close($link); // LINE 110 - ERROR OCCOURED
        } 

    }

?>




 <!-- ----------------------------------------------------------------------------------- */
/* Temporary suspension of "Tutorial Republic - PHP MySQL Login System" Tutorial
/* ------------------------------------------------------------------------------------ -->

----HTML CODE----

 <!doctype html>
<html>
    <head>
        <title>Learning Portal</title> <!-- CHANGE THESE IN ALL FILES RELATING TO THIS PROJECT! -->
        <meta name="description" content="Learning Portal">
        <meta name="keywords" content="Learning Portal EFREI">
        <meta name="author" content="D.Jackson">
        <link rel="stylesheet" type="text/css" href="NHS.css" media="screen">
        <link rel="icon" type="image/ico" href="favicon.ico">
        <link href="https://fonts(dot)googleapis(dot)com/css?family=Jura" rel="stylesheet">
    </head>

    <!-- <div> is to group block elements for CSS formatting -->

    <body>
        <div id="container"> 
            <header><a href="pt-5-home.htm">Login</a>
            </header>
            <!-- The htm. above which would connect to the main page was originally 
            "mars.htm", then "NHS.htm" and is now called "pt-5-home.htm" -->

            <!--

            <div id="main">
                <section>
                    <p>Please enter your details:
                    </p>

            -->                 




<!-- ----------------------------------------------------------------------- */
/* Resuming "Tutorial Republic - PHP MySQL Login System"
https://www.tutorialrepublic.com/php-tutorial/php-mysql-login-system.php
/* ------------------------------------------------------------------------ -->

<body>
    <div class="wrapper">
        <h2>Login</h2>
        <p>Please fill in your credentials to login.</p>
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
            <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
                <label>Username:<sup>*</sup></label>
                <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
                <span class="help-block"><?php echo $username_err; ?></span>
            </div>    

            <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
                <label>Password:<sup>*</sup></label>
                <input type="password" name="password" class="form-control">
                <span class="help-block"><?php echo $password_err; ?></span>
            </div>

            <div class="form-group">
                <input type="submit" class="btn btn-primary" value="Submit">
            </div>
            <p>Don't have an account? <a href="pt-5-register.php">Sign up now</a>.</p>
        </form>
    </div>    
</body>
</html>

1 个答案:

答案 0 :(得分:1)

在注册脚本中,第一行是:

<!-- ----------------------------------------------------------------------- */
/* Taken from "Tutorial Republic - PHP MySQL Login System" - 
/* ------------------------------------------------------------------------ -->

----PHP CODE----

因此,您已经向浏览器发送了内容,无法发送标题(会话启动,Cookie,重定向等);删除那些行。

在您的配置脚本中,您使用的是 $ connect 变量,因此您必须在登录脚本中使用它而不是 $ link