Well first of all it's hard to help you with code when you're simplifying things!
Generally Login forms need something to compare to. I.E. You need to be matching information to a database.
Without a database information won't be able to be retained.
I'll try to make some suggestions, hopefully it will help you solve what it is you are trying to accomplish.
When I work with sessions I like to split it up into a few different documents
header.php
--> Here is where you open all your html document, link to all your external css files and any navigation
footer.php
--> Footer.php contains the address line that will be shown at the bottom of every page and a single line of php code after the html tags are closed.
<?php
ob_end_flush();
?>
initalize.phpob_start();
if (!isset($_SESSION))
{
session_start();
}
--->
Initalize.php is an extremely important piece, this document will look to see on
every page if a session has been initiated,
If it has it will call up that session where user information is stored, or if the session tests negative it will open one.
This file is called in after the header in the body content portion of every page
After this we can start getting into the meat programming the GUI
welcome.php
---> A very basic page as far as content goes, all it really needs to have is WELCOME in big letters, if you are not already familiar,
here we will have our first encounter with a new term. "include_once".
include_once("templates/initalize.php");
include_once("templates/header.php");
?>
<h4> WELCOME </h4>
<p>Please register to order any books from out library.</p>
<br />
<?php
include_once("templates/footer.php");
?>
include_once calls other pages into the php document. By calling initalize.php we can automaticly check if a session has been started without hardcoding this php script into every single page. It is the same concept for header.php but instead php calls to the document that contains the nav menu list and the link to the external stylesheet.
So now we need to build the register page
register.php
--> Here we create a form using the same template as the welcome page. Where welcome is printed on the previous page we will build a form that contains text input fields for: First Name, Last Name, Credit Card, Street, City and Postal code. You can make your forms sticky (they remember information if user makes a mistake and the page refreshes) the code for this follows the basic html form convention with one exception. In the Value field is a small portion of php which will check to see if any information has been entered into the session and will echo that information back.
<input type="text" name="first_name"
value="<?php if (isset($_SESSION['first_name'])) echo $_SESSION['first_name'];?>">
</input>
So the form is done! SWEET! now let's store all that information and run some Validations to make sure there's no funky user input hiding
create_account.php
---> The registration form will be sent to the create_account.php document to be processed, this file will import all of the text entries from the previous form and will store them into your session.
After the entries have been stored into the session the script will check to see if more than one character has been enetered into all value fields. If one is missing you will be returned to the register.php file untill this condition is satisfied. Once all conditions are met and the form is re-submited the header will relocate you to the bookstore.php
you'll notice I use a strip_tags function on all requests. If a user has tries to enter html or anything other type of code, it will be deleted from the user input.
include_once("templates/initalize.php");
$first_name = trim(strip_tags($_REQUEST['first_name']));
$_SESSION['first_name'] = $first_name;
$last_name = trim(strip_tags($_REQUEST['last_name']));
$_SESSION['last_name'] = $last_name;
$credit_card = trim(strip_tags($_REQUEST['credit_card_number']));
$_SESSION['credit_card_number'] = $credit_card;
$street = trim(strip_tags($_REQUEST['street']));
$_SESSION['street'] = $street;
$city = trim(strip_tags($_REQUEST['city']));
$_SESSION['city'] = $city;
$postal = trim(strip_tags($_REQUEST['postal']));
$_SESSION['postal'] = $postal;
if (strlen($first_name) > 1 && strlen($last_name) > 1 && is_numeric($credit_card))
{
if (strlen($street) > 1 && strlen($city) > 1 && strlen($postal) > 1)
{
header('Location: bookstore.php');
}
else
{
header('Location: register.php');
}
}
else
{
header('Location: register.php');
}
?>
header('Location: '); is the code used for re-directing to another page, when you would like to process a form and conditions are not met you can tell php to return the browser to the registry or to continue onto your store.
You only get one more piece.
bookstore.php
---> Contains a few interesting things, immediately after we call our include_once commands an if loop is executed to see that if the "first_name" session has not been initialized on our registration form, the website will automaticly redirect you back to the registry page, this way you can not skip the registration and go directly to shopping.
include_once("templates/initalize.php");
include_once("templates/header.php");
if (!isset($_SESSION['first_name']) && !isset($_SESSION['last_name']) && !isset($_SESSION['address']))
{
header('Location: register.php');
}
?>
<form action="process_orders.php" method="post">
<fieldset><legend>Enter which title you would like to order.</legend>
<br />
<label>Author
<input type="text" name="author"
value="<?php if (isset($_SESSION['author'])) echo $_SESSION['author'];?>">
</input>
</label><br />
<label>Title
<input type="text" name="title"
value="<?php if (isset($_SESSION['title'])) echo $_SESSION['title'];?>">
</input>
</label><br />
<input type="submit" name="submit" value="Buy" />
</fieldset>
<fieldset><legend>Are you done shopping?</legend>
<input type="submit" name="done" value="Done">
</fieldset>
</form>
<?php
include_once("templates/footer.php");
?>
My app contained two buttons, a BUY button if you would like to add the book to your cart then continue shopping and a DONE button to continue on to the shopping cart. You will see that corrisponding names have been assigned to both buttons above.
When you are done everything sessions are needed for --> Destroy! Destroy!
session_destroy(); will delete all information entered into the session when a user has completed using this website and leaves the forms empty for the next person.
In your senario this would be called on the bottom of your 'log out' page.
<?php
session_destroy();
include_once("templates/footer.php");
?>
TERMS
include_once :includes and evaluates the specified file during the execution of the script. As the name suggests, it will be included just once.
header : The header() function sends a HTTP header to a client. It is important to notice that header() must be called before any actual output is sent
In PHP 4 and later, you can use output buffering to solve this problem.
sessions : preserves certain data on the server side. This enables you to build more customized applications and increase the appeal of your web site. All information is in the Session reference section.
session_start(); : starts a new session for the user whose using your web application
session_destroy(); : deletes all information fromm the session when a user has completed what they're doing.
ob_start(); : This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
ob_end_flush(); : This function will send the contents of the topmost output buffer (if any) and turn this output buffer off. If you want to further process the buffer's contents you have to call ob_get_contents() before ob_end_flush() as the buffer contents are discarded after ob_end_flush() is called.
IN CLOSING
I hope that helped, I'm sure anything past this you can figure out on your own.
This app I built will retain sessions as long as the page/browser is left open.
Us PHP nerds tend to be pretty reliable when it comes to problem solving