2017-10-17 263 views
-3

如果我提出的问题可能对您有些容易,我会提前致歉。我的网站有一些错误。它适用于我本地的开发环境,但在我使用的托管平台(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登录。 PHP“文件,我已经注意到错误发生的地方。查找出47行,105和110

Image of failed login attempt

以下代码在哪里本网站上的错误发生的页面。网站链接将根据要求发送。

请大家帮忙,因为我相信我离完成这个登录/注册系统并不遥远。

以下为PHP CODE配置页 “的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> 
+0

可能要修改你的密码。 – Erik

+0

'$ link'应该是'$ connect',不是吗? (我想它是在config.php中定义的?) – Jeff

+0

,我想'从教程中获得的注释......--- php代码 - '在你的真实代码中不是_对吧? – Jeff

回答

1

在记数比脚本的第一行是:

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

----PHP CODE---- 

所以,你已经发送内容到浏览器并不能发送头(会话开始,饼干,重定向等);删除这些行。

在你的配置脚本,你正在使用$连接变量,所以你必须在你的登录脚本使用它,而不是$链接

+0

我有同样的问题,在本地工作,而不是远程工作。不知何故,我本地安装的PHP并不像在主机上那样严格。 @triby是正确的,header()必须是第一个也是唯一一个将输出发送到客户端浏览器的东西。 – Nic3500

+0

干杯,它做到了! –

相关问题