Hello everyone, I am very new to PHP and I am having difficulty with my login. I can do standard logins via directing to another page, however this one is different.
I would like a login section at the top of my website which is always displayed. If your not logged in then it would always show the login boxes, if you are logged in then it would always display logout. I am also using sessions once you have logged in. I have a feeling I need a loop.
I am rubbish with if and else as I am new to all of this so I appologise if the mistakes are simple. One of the problems I am having is that the messages are being displayed on a seperate page rather than with the rest of my website.
This is what I have so far:
PHP Code:
<?php
include('../includes/config_inc.php');
if(isset($_SESSION["username"])) {
echo" <a href='../admin/logout.php'>Logout</a>";
}
else {
echo" <form method='post'>";
echo" <label for='username'>Username</label>";
echo" <input type='text' name='username' id='username' />";
echo" <label for='password'>Password</label>";
echo" <input type='password' name='password' id='password' />";
echo" <input type='submit' name='submit' value='Log in' />";
echo" </form>"; //Puts in form if not logged in
echo" <a href='../admin/register.php'>Register</a>";
//form validation
if(!isset($_POST['submit'])) //if the form has been filled out
{
$username = $_POST["username"];
$password = md5($_POST["password"]);
$authenticate = mysql_query("select * from customer where username='$username' and password='$password'");
if(mysql_num_rows($authenticate)==1)
{
$_SESSION["username"] = $username;
echo "Hello $username"; // a session created
}
else
{
//a session not created, displays form again with login incorrect message
}
}}
?>