This post is part 4 of the Basic PHP Crash Course. If you have never red before this Crash Course, you should read
part 1,
part 2 and
part 3 first.
In this post we will discuss about PHP Session. It is very useful when you write a web
application.
Let's think about our
pizza shop website. After the user have
submitted the order form, our application will call the process.php. Then I want to show the order form again, if the submitted
form has the errors. To do so, we have a problem. We cannot know the
error data from process.php when we show the order form again. So we
must use PHP Session.
What is PHP Session?
Session is a way to
store information for the
individual
user in our server by using session ID. This ID is automatically
generated by PHP and store also on the user computer for the lifetime of
a session. The content of the session data is store at the server.
Simple Example
<?php session_start(); ?>
<html>
<head>
<title>Page 1</title>
</head>
<body>
<?php
$_SESSION['user_name'] = "John";
?>
</body>
</html>
Before we use the session data, we need to load session_start()
function. This function will check whether there is already a session.
If not, it will create one.
Now we can use the superglobal $_SESSION array. So I set the user_name
as John.
Below is the another page.
<?php session_start(); ?>
<html>
<head>
<title>Page 2</title>
</head>
<body>
<?php
echo "Hi ".$_SESSION['user_name'];
?>
</body>
</html>
In this page, we echo the user_name from session data array. If you run
the page2, you will see "Hi John". In this way, you can save the data
you need for other page. However session data is not
permanent storage. For permanent storage, we will use the
database like MySql(I will also explain about
MySql database later in this series.).
Enhancing our pizza shop website
Now it is time to enhance our pizza shop site. Below is our index.php.
<?php session_start(); ?>
<html>
<head>
<title>Order Process</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
if( $_POST['cus_name'] != '' && $_POST['quantity'] != '' && $_POST['address'] !='' )
{
$cus_name = $_POST['cus_name'];
$quantity = $_POST['quantity'];
$address = $_POST['address'];
?>
<p>Thank <?php echo $cus_name; ?> !</p>
<p>You order <?php echo $quantity; ?> pizza.</p>
<?php
$total = $quantity * 10;
?>
<p>It will cost you <?php echo $total; ?> $.</p>
<p>We will send withing 2 hours to <?php echo $address; ?>.</p>
<?php
}
else
{
$_SESSION['error'] = "You need to fill all data.";
header("location: index.php");
}
?>
<?php } ?>
</body>
</html>
As you know, firstly we need to call the session_start() function to use
the session data. If the user submit the form without filling all data,
we will set the session error data as "You need to fill all data.". And
then redirect our site to index.php by using header() function. This
function will tell the browser to load
the page
that you send as parameter.
In our index.php file, we will also start with session_start() function.
Then we will retrieve the error data from the session array and will
echo before our order form. Below is our index.php.
<?php session_start(); ?>
<html>
<head>
<title>Pizzs Show: Home</title>
</head>
<body>
<?php
if($_SESSION['error'] != '')
{
echo $_SESSION['error'];
$_SESSION['error']='';
}
?>
<h3>Pizza Shop Order Form</h3>
<form action="process.php" method="post">
<p>
<label for="cus_name">Customer Name:</label>
<input type="text" name="cus_name" />
</p>
<p>
<label for="address">Shipping Address:</label>
<input type="text" name="address" />
</p>
<p>
<label for="quantity">Pizza Quantity:</label>
<input type="text" name="quantity" />
</p>
<p>
<input type="submit" name="submit" value="Submit Order" />
</p>
</form>
</body>
</html>
0 comments:
Post a Comment