Hi guys,
I am learning php and mysql and am a begginer. Everything works fine on my local server, but when I upload it to my live server http://www.immystudios.com/login.php the login script gives some errors.
Below is the error I am getting
Warning: Cannot modify header information - headers already sent by (output started at /homepages/40/d666957300/htdocs/Student/login.php:6) in /homepages/40/d666957300/htdocs/Student/login.php on line 58
This is my login.php
<!--
Login Page
this file also contains html form with two input box which will take user email and user password entered by user and then after submitting the form, the php code will match that user email and password combination in database and when it finds both results in table then it will start a session and allow user to access home page else it will show appropriate message.
-->
<?php
define("TITLE", "Login | G.A.K One Stop Eletrical & Plumbing");
include('includes/header.php');
ob_start();
session_start();
require_once 'dbconnect.php';
// it will never let you open (login) page if session is set
if ( isset($_SESSION['user'])!="" ) {
header("Location: home.php");
exit;
}
$error = false;
if( isset($_POST['btn-login']) ) {
// prevent sql injections/ clear user invalid inputs
$email = trim($_POST['email']);
$email = strip_tags($email);
$email = htmlspecialchars($email);
$pass = trim($_POST['pass']);
$pass = strip_tags($pass);
$pass = htmlspecialchars($pass);
// prevent sql injections / clear user invalid inputs
if(empty($email)){
$error = true;
$emailError = "Please enter your email address.";
} else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
$error = true;
$emailError = "Please enter valid email address.";
}
if(empty($pass)){
$error = true;
$passError = "Please enter your password.";
}
// if there's no error, continue to login
if (!$error) {
$password = hash('sha256', $pass); // password hashing using SHA256
$res=mysqli_query($conn, "SELECT userId, userName, userPass FROM users WHERE userEmail='$email'");
$row=mysqli_fetch_array($res, MYSQLI_BOTH);
$count = mysqli_num_rows($res); // if uname/pass correct it returns must be 1 row
if( $count == 1 && $row['userPass']==$password ) {
$_SESSION['user'] = $row['userId'];
header("Location: home.php");
} else {
$errMSG = "Incorrect Credentials, Try again...";
}
}
}
?>
<div id = "register">
<h2>Sign In.</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off" id = "register-form">
<?php
if ( isset($errMSG) ) {
$errMSG;
}
?>
<label for = "email">Your email </label>
<input type="email" id ="email" name="email" placeholder="Your Email" value="<?php echo $email; ?>" maxlength="40" />
<span class="text-danger"><p class = "error"><?php echo $emailError; ?></p></span>
<label for = "password">Your Password </label>
<input type="password" id = "pass" name="pass" placeholder="Your Password" maxlength="15" />
<span class="text-danger"><p class = "error"><?php echo $passError; ?></p></span>
<input type="submit" class="button submit" name="btn-login" >
<a href="register.php">Sign Up Here...</a>
</form>
</div>
<?php
include('includes/footer.php');
?>
<?php ob_end_flush(); ?>
This is my header.php
<?php
$company_Name = "G.A.K";
include('includes/arrays.php');
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo TITLE; ?></title>
<link href="assets/styles.css" rel="stylesheet">
</head>
<body id = "final-example">
<div class ="wrapper">
<div id = "banner">
<a href="index.php" title = "Return to Home">
<img src = "img/banner.png" alt = "Franklin's Fine Dining">
</a>
</div><!-- banner -->
<div id = "nav">
<?php include('includes/nav.php'); ?>
</div><!-- nav -->
<div class = "content">
I would greatly appreciate some assistance in fixing this. Thanks