I'm trying to create a PHP Login System but I got this error while writing it :
session_start(): Cannot send session cache limiter - headers already sent
I created three pages, the index.php
which contains the form, the logyourself.php
which contains the check PHP code and home.php
which contains the page when the user gets redirected to on successful login.
logyourself.php:
session_start();
$host = ""; // Host name
$username = ""; // Mysql username
$password = ""; // Mysql password
$db_name = ""; // Database name
$tbl_name = ""; // Table name
// Connect to server and select databse.
@mysql_connect("$host", "$username", "$password") or die("cannot connect");
@mysql_select_db("$db_name") or die("cannot select DB");
@mysql_query("set names 'utf8';");
// Define $myusername and $mypassword
$username = $_POST['username'];
$password = $_POST['password'];
// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql = "SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result = mysql_query($sql);
// Mysql_num_row is counting table row
$count = mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if ($count == 1) {
// Register $myusername, $mypassword and redirect to file "admin.php"
$_SESSION['username'] = "username";
$_SESSION['password'] = "password";
header("location:home.php");
} else {
echo "wrong Password or username";
echo "<br>";
echo "<a href=index.php>";
echo "Back To Login";
echo "</a>";
}
home.php:
session_start();
if (!isset($_SESSION['username'])) {
header("location:index.php");
}
$username = $_SESSION['username'];
echo "Hi $username
<br>
<a href=\"logout.php\">Logout</a>
";
Why am I getting this error?