0

I'm new to web dev and this is my first website using HTML,CSS, PHP and MySQL, so sorry about the mess. I have created a simple login system with PHP, however, when attempting to create an account and pressing 'register' I am greeted with the following error msg. (Note: It still creates the account but it doesn't redirect to the welcome.php page.)

Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd2/939/18193939/public_html/config.php:17) in /storage/ssd2/939/18193939/public_html/register.php on line 83

I have tried removing all white space I can see before but no luck. I can't find any other solutions. Any help would be appreciated.

Here is my code for register.php

<?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.";
    } elseif(!preg_match('/^[a-zA-Z0-9_]+$/', trim($_POST["username"]))){
        $username_err = "Username can only contain letters, numbers, and underscores.";
    } else{
        // Prepare a select statement
        $sql = "SELECT id FROM users WHERE username = ?";
        
        if($stmt = mysqli_prepare($link, $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(empty($password_err) && ($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($link, $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: login.php");
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }

            // Close statement
            mysqli_stmt_close($stmt);
        }
    }
    
    // Close connection
    mysqli_close($link);
}
?>
 
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="style.css" media="screen"/>
<link rel="icon" type="image/x-icon" href="LogoWhite.webp">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<title>Join</title>
</head>
<header>
<div>
<a href="index.html">
<img class="logo" src="LogoWhite.webp" alt="Average_Joe's_Logo" width="250" height="250" id="coverphoto"> 
</div>
</header>
<nav id="navbar">
<ul>
   <li class="dropdown">
   <a href="AboutUs.html" class="class1" class="dropbtn">About Us</a>
      <div class="dropdown-content">
      <a href="Dodgeball.html">Dodgeball</a>
      <a href="Plans.html">Plans</a>
      </div>
 <li><a href="Facilities.html" class="class1" >Facilities</a></li>
 <li class="dropdown">
 <li><a href="Classes.html" class="class1">Classes</a></li>
 <li><a href="PersonalTraining.html" class="class1">Personal Training</a></li>
 <li><a href="Contact.html" class="class1">Contact Us</a></li> 
 <li><a href="FAQ.html" class="class1">FAQ</a></li>
 </ul>
</nav>
<body>
<div class="howcanwehelp">
<h2> Membership </h2>
<h3> Create an Account to Purchase Membership </h3>
</div>
<div class="loginbackground">
  <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
  <div class="container3">
    <label for="username"><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="username" id="username" class="form-control<?php echo (!empty($username_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $username;?>">
    <span class="invalid-feedback"><?php echo $username_err;?></span>
    <br><br>
    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="password" id="password" class="form-control<?php echo (!empty($password_err)) ? 'is-invalid' : '';?>" value="<?php echo $password;?>">
    <span class="invalid-feedback"><?php echo $password_err;?></span>
    <br><br>
    <label for="confirm_password"><b>Repeat Password</b></label>
    <input type="password" placeholder="Repeat Password" name="confirm_password" id="confirm_password" class="form-control<?php echo (!empty($confirm_password_err)) ? 'is-invalid' : '';?>"
    value="<?php echo $confirm_password;?>">
    <span class="invalid-feedback"><?php echo $confirm_password_err;?></span>
    <hr>
    <button type="submit" value="Submit" class="loginbtn">Register</button>
    <p>Already have an account? <a href="login.php">Sign in</a></p>
  </div>
  </div>
</form>
</body>
<footer>
<table class="openinghours">
  <tr>
    <th>Opening Hours</th>
  </tr>
  <tr>
    <td>Monday</td>
    <td>6:00am - 10:00pm</td>
  </tr>
   <tr>
    <td>Tuesday</td>
    <td>6:00am - 10:00pm</td>
  </tr>
 <tr>
    <td>Wednesday</td>
    <td>6:00am - 10:00pm</td>
  </tr>
 <tr>
    <td>Thursday</td>
    <td>6:00am - 10:00pm</td>
  </tr>
 <tr>
    <td>Friday</td>
    <td>6:00am - 10:00pm</td>
  </tr>
 <tr>
    <td>Saturday</td>
    <td>6:00am - 10:00pm</td>
  </tr>
 <tr>
    <td>Sunday</td>
    <td>6:00am - 10:00pm</td>
  </tr>
  </table>
  <table class="quicklinks">
    <tr>
    <th>Quick Links<th>
    </tr>
    <tr>
    <td><a href="index.html" class="class3">Homepage</li></td>
    </tr>
    <tr>
    <td><a href="AboutUs.html" class="class3">About Us</a></li></td>
    </tr>
    <tr>
    <td><a href="Dodgeball.html" class="class3">Dodgeball</a></td>
    </tr>
    <tr>
    <td><a href="Plans.html" class="class3">Plans</a></td>
    <tr>
    <td><a href="Facilities.html" class="class3">Facilities</a></li></td>
    </tr>
    <tr>
    <td><a href="Classes.html" class="class3">Classes</a></li></td>
    </tr>
    <tr>
    <td><a href="PersonalTraining.html" class="class3">Personal Training</a></li></td>
    </tr>
    <tr>
    <td><a href="Contact.html" class="class3">Contact Us</a></li></td>
    </tr>
    <tr>
    <td><a href="FAQ.html" class="class3">FAQ</a></li></td>
    </tr>
</table>
</footer>
</body>
</html>
Ben
  • 1

0 Answers0