I am getting a "Cannot modify header information - headers already sent". I know this has been asked a lot but I still cannot get it working after going through other answers.
Here is my code.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style/index.css">
<script type="text/javascript">
function loadRegister() {
document.getElementById('log_container').style.display = "none";
document.getElementById('reg_container').style.display = "block";
}
function loadLogin() {
document.getElementById('reg_container').style.display = "none";
document.getElementById('log_container').style.display = "block";
}
</script>
</head>
<body>
<div id = "container_div">
<div id = "logo_container">
<img id = "polygon_logo" src="images/logo.png"/>
</div>
<h1>POLYGON</h1>
<div id = "log_container" class="login container">
<form id="register_form" method="post" action="index.php">
<input type="text" name="username" class="input" placeholder="Your Username!"><br>
<input type="password" name="pass" class="input" placeholder="Your Password!"><br>
<input type="submit" name="submit_login" value="Login" class="action-btn">
<input type="button" name="go-to-register" value="Don't have an account? Register" class="text-btn" onclick="loadRegister();">
</form>
</div>
<div id = "reg_container" class="register container">
<form id="register_form" method="post" action="index.php">
<input type="text" name="username" class="input" placeholder="Your Username!" required><br>
<input type="text" name="useremail" class="input" placeholder="Your Email!" required><br>
<input type="password" name="pass" class="input" placeholder="Your Password!" required><br>
<select class="country" name="country_code">
<option value="AF">Afghanistan</option>
<option value="AX">Åland Islands</option>
<option value="AL">Albania</option>
<option value="DZ">Algeria</option>
<option value="ZW">Zimbabwe</option>
</select>
<input type="submit" name="submit_register" value="Register" class="action-btn">
<input type="button" name="go-to-login" value="Already have an account? Login" class="text-btn" onclick="loadLogin();">
</form>
</div>
</div>
<?php
include 'connect.php';
if(isset($_POST['submit_register'])) {
$name=$_POST['username'];
$mail=$_POST['useremail'];
$pass = $_POST['pass'];
$country = $_POST['country_code'];
$hash = hash('sha512',$pass);
$sql = "INSERT INTO user_details (user_name, user_email, user_password, country) VALUES ('$name','$mail','$hash', '$country')";
if ($conn->query($sql) === TRUE) {
//mail($mail,'Welcome','Thank you for signing up with Polygon','From: dfarrelly96@gmail.com');
header('Location: home.php');
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
if(isset($_POST['submit_login'])) {
$name=$_POST['username'];
$pass = $_POST['pass'];
$hash = hash('sha512',$pass);
$sql = "SELECT user_name,user_password FROM user_details WHERE user_name='$name' AND user_password='$hash'";
$result = $conn->query($sql);
if($result->num_rows > 0) {
// Set session variables
$_SESSION["name"] = $name;
$_SESSION["mail"] = $mail;
$sql = "SELECT user_id FROM user_details WHERE user_name ='$name'";
$result = $conn->query($sql);
if($result->num_rows > 0) {
$row = mysqli_fetch_array($result);
$_SESSION["user_id"] = $row["user_id"];
header('Location: home.php');
}
else {
echo "Unknown Error!";
}
}
else {
echo "Incorrect username or password. Please try again!";
}
}
?>
</body>
</html>
The warning says output started at C:\xampp\htdocs\Polygon\index.php:293 which is the first "
edit: forgot to mention I am getting this for both forms, if that makes a difference?