0

Trying to make a login page using php following a tutorial, it's successfully logging in getting both the password and username from the database then showing echo "wrong password" and welcome for each scenario. However it doesnt redirect to my login_success.php page stays on check_login.php, heres my code for the check page:

<?php

$host="localhost"; 
$username="root"; 
$password="root";
$db_name="test";
$tbl_name="members"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 


$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count > 0){
echo "Welcome";

session_register("myusername");

session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

any help would be great.

  • try commenting out `echo "Welcome";` – meda May 04 '14 at 00:16
  • Add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` plus you're not calling `session_start();` which should most likely be in there. Plus, you'll most likely see an error stating `headers already sent in...` when adding error reporting. – Funk Forty Niner May 04 '14 at 00:19
  • It is because you cannot send headers *after* outputting something. – Sverri M. Olsen May 04 '14 at 00:19
  • possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Sverri M. Olsen May 04 '14 at 00:19

1 Answers1

0

You cannot send anything to the client (i.e. echo "Welcome";) before setting your headers.

Reference http://www.php.net/manual/en/function.header.php

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. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Nor can you send anything to the client before starting sessions.

user1032531
  • 24,767
  • 68
  • 217
  • 387