Currently I have a PHP script that works as a login system which works perfectly fine, I have successfully connected to the database and am able to make interactions between the site and the database.
So, to the problem. I have used the include function in my main login.PHP page (which contains the the actual form) to call the script that handles the log in functionality. I have "included" this between the body tags because there is content that pops up when you log in successfully or unsuccessfully. Now here is where the problem actually comes in.
I need add a session after the user logs in successfully so that it works across all pages. The only way this will work is if I put the session_start() at the very top of the page before anything. But I need the content to display under the form which is in the body tag. I am very confused as to what I should do to fix this. Would anyone happen to have any ideas?
Code below is located in body tag of login.php:
<?php
// DATABASE VARIABLES
$user_name = "";
$pass_word = "";
$database = "";
$server = "";
// CONNECTS TO DATABASE
$db_handle = mysql_connect($server, $user_name, $pass_word);
$db_found = mysql_select_db($database, $db_handle);
// ACCOUNT INFORMATION
$email;
$password;
$num_rows = 0;
// IF SUBMIT IS CLICKED
if (isset($_POST['submit'])) {
// STORES INPUTS AS VARIABLES
$email = $_POST['email'];
$password = $_POST['password'];
// REMOVES HARMFUL CODE
$email = htmlspecialchars($email);
$password = htmlspecialchars($password);
if ($db_found) {
/*
// SUCCESS
print '<div class="password-wrapper"><div class="password-match">';
print '<li class="pass-match">Login Successful</li>';
print '</div></div>';
// FAILURE
print '<div class="password-wrapper"><div class="password-match">';
print '<li class="pass-nomatch">Email Already Exists</li>';
print '</div></div>';
*/
/*// REMOVES SQL INJECTION
$email = quote_smart($email, $db_handle);
$password = quote_smart($password, $db_handle);*/
$SQL = "SELECT * FROM accounts WHERE email = '$email' AND password = '$password'";
$result = mysql_query($SQL);
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
print '<div class="password-wrapper"><div class="password-match">';
print '<li class="pass-match">Login Successful</li>';
print '</div></div>';
/*
session_start();
$_SESSION['login'] = "1";
header("Location: page1.php");
*/
}
else {
print '<div class="password-wrapper"><div class="password-match">';
print '<li class="pass-nomatch">Invalid Credentials</li>';
print '</div></div>';
/*
session_start();
$_SESSION['login'] = '';
*/
}
}
else {
}
}
?>