I'm having trouble figuring out how to direct a user back to the page he/she was on before they decided to log in. I've looked at both the $SERVER['REQUEST_URI'] and the $SERVER['HTTP_REFERRER'] methods but the code examples I have seen do not match my code so it is hard to understand how to use these. I also want, once a valid log in has occurred, to change the "Log In" and "Create Account" links to "Log Out" and "Edit Profile". Plus add a greeting to welcome the user.
I know this has everything to do with the session variable but how to set it up and get it to do these things baffles me. Any help would be greatly appreciated.
Oh, one last thing. My showForm() messages do not work properly. It either states "Welcome" or "Username and Password Do Not Match" are my if statements correct??
Here is the code for my log in page:
<?php
session_start();
if (!isset($_SESSION['ValidLogIn'])){
//if username and password are empty display welcome message
if(empty($_POST['txtUserName']) && empty($_POST['txtPassword']))
{
showForm('Welcome!');
exit();
}
//validate text was entered in UserName text box
if(empty($_POST['txtUserName']) && isset($_POST['btnSubmit']))
{
showForm('Please Enter A User Name');
exit();
}
else
{
$UserName = $_POST['txtUserName'];
}
//validate text was entered in password text box
if(empty($_POST['txtPassword']) && isset($_POST['btnSubmit']))
{
showForm('Please Enter A Valid Password');
exit();
}
else
{
$Password = $_POST['txtPassword'];
}
$UserName = $_POST['txtUserName'];
$Password = $_POST['txtPassword'];
//validate username and password match
if($Password != Password($UserName) && isset($_POST['btnSubmit']))
{
showForm('User Name And Password Do Not Match!');
exit();
}
}
function Password($UserName)
{
//database login
$dsn = 'mysql:host=XXX;dbname=XXX';
$username='XXX';
$password='XXX';
//variable for errors
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
//try to run code
try {
//object to open database
$db = new PDO($dsn,$username,$password, $options);
//check username against password
$SQL = $db->prepare('SELECT * FROM user WHERE USER_NAME = :UserName and USER_PASSWORD = :Password');
$SQL->bindValue(':UserName', $UserName);
$SQL->bindValue(':Password', $Password);
$SQL->execute();
$username = $SQL->fetch();
if($username == FALSE)
{
$Password = null;
showForm('Invalid log in information.');
exit();
}
if($username == TRUE){
$UserName = $username['USER_NAME'];
$Password = $username['USER_PASSWORD'];
$_SESSION['ValidLogIn'] = $UserName;
include 'index.php';
}
return $password;
$SQL->closeCursor();
$db = null;
} catch(PDOException $e){
$error_message = $e->getMessage();
echo("<p>Database Error: $error_message</p>");
exit();
}
}
function showForm($formMessage = "Welcome!")
{?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Log In</title>
<link rel="stylesheet" href="styles/default-styles.css" type="text/css" />
<link rel="stylesheet" href="styles/FormStyle.css" type="text/css" />
<script type="text/javascript" src="js/validateLogInForm.js/validateLogInForm.js"></script>
</head>
<body id="logPage">
<div id="wrapper">
<?php include('includes/header.php'); ?>
<?php include('includes/topNavigation.php'); ?>
<div id="mainContent">
<div class="formDiv">
<form name="registerForm" id="registerForm" action="" method="post">
<?php if ($formMessage !="") echo "<h2 style=\"color:#FF0000; text-align: center\">".$formMessage."</h2>"; ?>
<h1 style="color:#FF530D; text-align: center">Log into your account here!</h1>
<fieldset id="security">
<legend>Security</legend>
<label for="txtUserName" class="boxLabel">User Name:</label>
<input type="text" id="txtUserName" name="txtUserName" autofocus="autofocus" required="required" />
<script type="text/javascript">
if(!("autofocus" in document.createElement("input")))
{
setTimeout(function(){
document.getElementById("txtUserName").focus();
}, 10);
}
</script>
<label for="txtPassword" class="boxLabel">Password:</label>
<input type="password" id="txtPassword" name="txtPassword" required="required" />
</fieldset>
<fieldset id="submission">
<div id="buttons">
<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit" onclick="return validateLogInForm()"/>
<input type="reset" id="btnReset" name="btnReset" >
</div><!--end buttons-->
</fieldset>
</p>
</form>
</div><!--end div class=formDiv-->
</div><!--end div id=mainContent-->
<?php include('includes/footer.php'); ?>
</div><!--end div id=wrapper-->
</body>
</html>
<?php
}
?>
And here is the code where I want to place the changes to the "Log In" links etc.
<!--Check to see if user is logged in. If session does not exist, serve header without personal greeting. If session does exist, serve second header-->
<?php
session_start();
if (isset($_SESSION['ValidLogIn'])){
//add code to get user name and change link messages
}
else {?>
<div id="header">
<div id="headerTop">
<div id="greeting">
</div><!--end div id=greeting-->
<ul id="logIn">
<li><a href="logIn.php">Log-In</a></li>
<li><a href="registerResponse.php">Register</a></li>
</ul>
</div><!--end div id=headerTop-->
<div id="headerBottom">
<div id="logo">
</div><!--end div id=logo-->
<div id="keyboard">
</div><!--end div id="keyboard"-->
</div><!--end div id=headerBottom-->
</div><!--end div id=header-->
<?php
}
?>