1

I have a login script that checks user credentials against a database, and if they are correct will assign them a session then forward them to the home screen.

For some reason, it will not forward, but will happily output text that appears after it should have read the forwarding code.

<?php
//require_once('scripts/include.php');
require_once('scripts/includePDO.php');

$error = '';
$form = $_POST['submit'];
$email = $_POST['email'];
$password = $_POST['password'];

if( isset($form) ) {
if( isset($email) && isset($password) && $email !== '' && $password !== '' ) {

$sql = "SELECT * FROM tbl_users WHERE email = :email and password = :password";

$q   = $conn->prepare($sql);
        $q->bindValue(':email',$email,PDO::PARAM_STR);
        $q->bindValue(':password',$password,PDO::PARAM_STR);
        $q->execute();

            $r = $q->fetch(PDO::FETCH_ASSOC);
            if(($r)!=0)

{ //success

$answer = $r['id'];

$_SESSION['logged-in'] = true;
$_SESSION['who'] = $answer;

echo "This text echos no probelm";

//If the login details are entered and they match those in the database, forward to new page.
header('Location: home/');

echo "Even this text will be shown";

exit;

// If information is wrong or missing, provide error message.
} else { echo "Sorry, something hasn't worked. Are you entering all the information correctly?"; }
} 

}

?>
Alexander
  • 3,129
  • 2
  • 19
  • 33
Elaine Adams
  • 179
  • 10

4 Answers4

2

There should be no output sent to the browser before the header statement. You will get headers already sent error(if enabled)

echo "This text echos no probelm";
rakeshjain
  • 1,791
  • 11
  • 11
  • Neither echo was originally there. I added them to see how far down the page it was reading. It does not forward whether they are in or not. – Elaine Adams Mar 18 '14 at 14:27
1

From the PHP manual

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

echo "This text echos no probelm";
//If the login details are entered and they match those in the database, forward to new page.
header('Location: home/');

Remove the echo.

secelite
  • 1,353
  • 1
  • 11
  • 19
  • Both the echo before the header and the one after have just been put in to show that code is being read both before and after, but it is not reading the header instruction. Originally neither echo was in and the same problem occurred. – Elaine Adams Mar 18 '14 at 14:24
  • Have you tried redirecting to an absolute URL? Like `http://www.domain.com/home/`. If this is also not working; maybe there are some whitespaces before your `header()`. – secelite Mar 18 '14 at 14:30
0

Did you make sure error_reporting was enabled? Try removing all the echo statements and adding ob_clean(); just before header(...)

David
  • 33,444
  • 11
  • 80
  • 118
0

To make it work your code, there are two options:

1) Add ob_start() on top of the code, before require_once('scripts/includePDO.php'); (Enabling output buffering)

Or

2) Remove the echo "This text echos no probelm"; from your code

Sreejith K
  • 86
  • 4