Hi all,
I am trying to create a small login system, but so far I suck at it.
I have a form in one page.
It contains only: EMAIL, PASSWORD.
I want to do this:
When the form has been submitted to the processing page, I am trying to check, if the email and password has been filled out.
IF they havent, or just one of them havent, I want to store an error message in a session, to use to display an error back on the form page.
So I want to return false/redirect to the form again, and display the error message stored in the session, into the form field.
I know I am doing it wrong, so please be kind :-)
This is my processing page:
<?php
session_start();
require_once ("../mysql/connection.php");
$email = '';
$password = '';
// validate email
if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
$email = mysqli_real_escape_string($connection, $_POST['email']);
echo '<p>Valid email.</p>'; // Used for testing
} else{
if(empty($_POST['email'])){
$_SESSION['email'] = '<p>Not valid email.</p>';
}
}
// validate password
if(!empty($_POST['password'])){
$password = mysqli_real_escape_string($connection, $_POST['password']);
echo 'You entered a password'; // Used for testing
} else {
if(empty($_POST['password'])){
$_SESSION['password'] = '<p>Enter a password.</p>';
}
}
// If no mistakes, look for user and pass in DB
$query = "SELECT email, pass FROM users WHERE
email='$email' AND pass='$password'";
$result = mysqli_query($connection, $query);
// if ONE match is found - store username in session
if(mysqli_num_rows($result) == 1){
$row = mysqli_fetch_array($result, MYSQLI_NUM);
$_SESSION['username'] = $row['username'];
if($row['type'] == 'admin')$_SESSION['user_name'] = true;
header("location:login_form.inc.php");
} // END mysql_num_rows IF-ELSE Statement
This is my form page:
<form action="login.inc.php" method="post" name="login_form" accept-charset="utf-8">
<p>
<label for="email"><strong>Email Addresse:</strong></label>
<br />
<!-- Below, I want to display the error, stored in the $_SESSION['email'], but only if it has a value, an actualn error occured -->
<input type="text" name="email" id="email" value="<?php if(!empty($_SESSION['email']))echo $_SESSION['email']; ?>"/>
<br />
<label for="password"><strong>Password:</strong></label>
<br />
<input type="password" name="password" id="password" value="<?php if(!empty($_SESSION['password']))echo $_SESSION['email']; ?>"/>
<a href="glemt_password.php" align="right">Forgot Password?</a><br />
<input type="submit" value=" Login → ">
</p>
</form>
This is really not working....The processing code doesnt even execute.
I thought I could use a header location:blah blah..And the use the session data, in the form.
Isnt that idea in general ok?
I am trying to avoid ajax so far, just to get to know php and sessions better.
Can it be done thinking this way?
Jan