Thursday, 23 June 2016

Issue with executing a PHP error message from a separate file in a login form system



I am running PHP 5.5.3. Essentially I have a web page [djaccess.php] with a simple login form on it that is connected with a MySQL database (with one username and password currently located within it):



            


Username


Password




include ('login.php');
if($error){ ?>

There was an issue with the form"



Find out more about DJing




When a user enters the username and password in the form fields, and clicks the submit button, the PHP script that detects whether this username and password is correct is located in login.php:


// Variables to connect to database
$dbhost = "localhost";
$dbusername = "root";
$dbpass = "";



// Database connection
$dbhandle = mysql_connect($dbhost, $dbusername, $dbpass) or die("Could not connect to database");
$selected = mysql_select_db("boxlogin", $dbhandle);


// Variables to set POST username & password
$myusername = $_POST['usernameInput'];
$mypassword = $_POST['passwordInput'];



// Protection from SQL injections
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);


// Selecting the username and password in the database table, and comparing the form values to the database values
$query = "SELECT * FROM test WHERE Username='$myusername' and Password='$mypassword'";



// Querying the form entries with the database, and ensuring that the count is equal to '1'
$result = mysql_query($query);
$count = mysql_num_rows($result);


// Re-direct to loginsuccess.php
if($count == 1){

$seconds = 18000 + time();
setcookie(loggedin, date ("F jS - g:i a"), $seconds);

header("location:loginsuccess.php");
} else {
header("location:djaccess.php") && echo $error;
}

// Ends connection to MySQL
mysql_close();
?>



If a username enters the password and username correctly, he/ she is successfully directed to loginsuccess.php HOWEVER, my problem is this:



I have set a count in the login.php page that if the username and password match it is equal to '1' therefore relocating that user to the loginsuccess.php page, but if that count does not equal to '1' I want them to be located back to the djaccess.php page with an error message present underneath the form that states that a username or password is incorrect, however I am having problems implementing this.



I have tried to set a header:location in the login.php if the count does not equal to '1' along with an echo $error variable but this does not seem to work. Also for reference I am new to PHP so still trying to get my head around the syntax. My code above was taken from hours of research and tutorials, and I understand what I have written, but I am stuck on this particular problem.



Can anyone help?


Answer



Convert your redirect handling to pass a message to the djaccess.php file. You can't use && logical operators to 'append' an action.




// Re-direct to loginsuccess.php
if($count == 1){

$seconds = 18000 + time();
setcookie(loggedin, date ("F jS - g:i a"), $seconds);
header("location:loginsuccess.php");
} else {
// CHANGE THIS
// header("location:djaccess.php") && echo $error;
$error = "Your username and password do not match";

header("location:djaccess.php?error=$error");
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...