0

I'm having a small issue with a header redirect on a login form. I have debugged the If & While statement and they both work fine, which surely can only be the header. I have tried redirecting it too numerous sites and pages but still nothing. Would really appreciate any help. Thanks!!!

  <center>
  <?php
  include './index.php';// This is a login form
  include './connection.php';// SQl connection page
  include './session.php'; // Sessions page
  error_reporting(E_ALL);
  $mysqli = new mysqli ("c3433870.co.uk.mysql", "c3433870_co_uk", "BFUWGpn3",        "c3433870_co_uk");

 if($mysqli->connect_errno > 0) {
  die('Unable to connect to database [' . $mysqli->connect_error . ']');
 }

 session_start();

 if(isset($_POST['submit'])){

    $stmt = $mysqli->prepare("SELECT username, password FROM users WHERE    username=? AND  password=? LIMIT 1");
    $username = $_POST['username'];
    $password = $_POST['password'];
    $stmt->bind_param('ss', $username, $password);
    $stmt->execute();
    $stmt->bind_result($username, $password);
    $stmt->store_result();
    if($stmt->num_rows == 1)  //To check if the row exists
        {
            while($stmt->fetch()) //fetching the contents of the row

              {
               $_SESSION['Login_sessions'] = true;
               $_SESSION['login_user'] = $username;
               header('Location: ./profile.php');


               }

        }
        else {
            echo "Wrong Username or Password!";
        }
        $stmt->close();
        $stmt->free_result();
    }
    else 
    {   
     echo "Not Found";
    }
 $mysqli->close();
 ?>
 </center>
Zach Hall
  • 27
  • 1
  • 1
  • 8

1 Answers1

0

Server headers are sent after the first echo. when you echo your center tag you thwart yourself. Since the headers are already sent, you cannot change the location inside.

For this same reason, it is considered best practice not to close your php tags, as the php parser doesn't care either way.

I would remove the ?>

steve
  • 290
  • 1
  • 11