I am having problems with my login process. I am using sessions and reloading so that I can have a single page site. On Chrome, this adds a ? so the end of the url (anytime the ? is present, login does not work). And on both Firefox and Chrome, the login-logout process only works once and then login stops working. I have an if statement on my index.php page that is essentially:
if($_SESSION["LoggedIn"] != true) {
/*display login button*/
}
else {
/*display logout button*/
}
Then I set my scripts file:
$("#toolbarTitle-Login").click(function() {
var loginEmail = $("#toolbarTextbox-Email").val();
var loginPassword = $("#toolbarTextbox-Password").val();
if (loginEmail != "") {
$("#toolbarPleaseWait").text("Please wait...");
$.post(
'ajax/loginProcess.php',
{
'email': loginEmail,
'password': loginPassword
},
function (response) {
window.location.reload(true);
}
);
}
});
$("#toolbarTitleLoggedIn-Logout").click(function() {
$("#toolbarPleaseWait").text("Logging out...");
$.post(
'ajax/logoutProcess.php',
{
},
function (response) {
window.location.reload(true);
}
);
});
Then my processing pages loginProcess.php:
<?php
session_start();
include "../incl/databaseConnection.php";
$email = $connection->real_escape_string($_POST["email"]);
$password = $connection->real_escape_string(md5($_POST["password"]));
$sql = "SELECT clientId
FROM clients
WHERE (studentEmail = '$email' AND studentPassword = '$password') OR (parentEmail = '$email' AND parentPassword = '$password');";
if (!$result = $connection->query($sql)) {
die ('There was an error running the query [' . $connection->error . ']');
}
$rows = $result->num_rows;
$row = $result->fetch_assoc();
if ($rows == 1) {
$_SESSION["loggedIn"] = true;
$_SESSION["clientId"] = $row["clientId"];
}
else if ($rows > 1) {
echo 'CHOOSE BETWEEN ACCOUNTS';
}
else if ($rows == 0) {
echo 'SHOW ERROR MESSAGE';
}
$connection->close();
?>
Next logoutProcess.php:
<?php
session_start();
session_unset();
?>
Okay, so it ended up being a very simple fix. I did not include
return false;beneath my ajax post. This caused the page to reload as soon as I pushed the submit button (this action took place before the jquery fired), which is the natural behavior for<input type="submit">. The “?” was an indication of this submission, and showed that no POST variables had not been defined, or they would have been listed next to it.