• No results found

Note the session information can even be stored permanently in a database if required.

Figure 3-7. The values of the session variables are set and a link is displayed to access and view the session variables

The sessionscript2.php script shown in Listing 3-9 completes the following tasks: • Checks whether a session exists. If not, it starts a new session.

• Checks if the username variable is set and displays a welcome message.

• Retrieves the values in the session variables, cartquantity and cartprice, which were set in the sessionscript1.php script, and displays them on the screen.

Listing 3-9. The sessionscript2.php script retrieves values from the session variables <?php if (session_status() == PHP_SESSION_NONE) { session_start(); } ?> <html> <body> <?php if (isset($_SESSION['username'])) $username=$_SESSION["username"]; else $username="Sir/Ma'm"; $cartquantity=$_SESSION["cartquantity"]; $cartprice=$_SESSION["cartprice"];

echo "Session is On and the session id is " . session_id() . "<br>"; echo "Welcome $username. <br>";

echo "There are $cartquantity products chosen in the cart worth $$cartprice";

?> </body> </html>

When the script runs, the session ID is displayed along with a welcome message to the user. Also, the cart quantity and cart price are displayed, as shown in Figure 3-8.

Figure 3-8. The web page displaying the session ID, user name, cart quantity, and price

How about adding the user’s sign-in information to the header of the site, displayed through the topmenu.php script shown in Listing 3-5?

You can display the Login and Signup links in the header, and they will complete the following tasks:

• Display the username of the user who is signed in or display the Login link that will enable a user to sign in.

• Display the Signup link, which will display a form that enables the user to register on your site.

To do these tasks, modify the topmenu.php script to appear as shown in Listing 3-10. Only the code in bold is new; the rest is the same as shown in Listing 3-5.

Listing 3-10. The topmenu.php header file of the e-commerce site, modified to display

signed-in user information <!DOCTYPE html>

<head>

<meta charset="utf-8">

<title>Bintu Online Bazar</title>

<link rel="stylesheet" href="css/style.css"> </head>

<body>

<table width="100%" cellspacing="0" cellpadding="2"> <col style="width:30%"> <col style="width:40%"> <col style="width:20%"> <tr><td style="background-color:cyan;color:Blue;"></td><td style="background-color:cyan;color:Blue;"></td><td style="background-color:cyan;color:Blue;"> <?php if (session_status() == PHP_SESSION_NONE) { session_start(); }

echo "<span id=\"userinfo\"><a href=\"signin.php\">Login</a>&nbsp;&nbsp; &nbsp;<a href=\"validatesignup.php\">Signup</a></span></td></tr>"; ?>

<tr><td style="font-size: 35px;color:blue;background-color:cyan;"> <b>Bintu Online Bazar</b></font></td>

<td bgcolor="cyan">

<form method="post" action="searchitems.php"> <input size="50" type="text" name="tosearch"> <input type="submit" name="submit" value="Search"> </form></td>

<td bgcolor="cyan" ><a href="cart.php"><img style="max-width:40px; max-height:40px;width:auto;height:auto;" src="images/cart4.png"></img> <span id="cartcountinfo"></span></a></td></tr>

</table>

<div class="container"> <nav>

<ul class="nav">

<li><a href="index.php">Home</a></li>

<li class="dropdown"><a href="index.php">Electronics</a> <ul>

<li><a href="itemlist.php?category=CellPhone">Smart Phones</a></li> <li><a href="itemlist.php?category=Laptop">Laptops</a></li>

<li><a href="index.php">Cameras </a></li> <li><a href="index.php">Televisions</a></li> </ul>

</li>

<li class="dropdown">

<a href="index.php">Home & Furniture</a> <ul class="large">

<li><a href="index.php">Kitchen Essentials</a></li> <li><a href="index.php">Bath Essentials</a></li> <li><a href="index.php">Furniture</a></li> <li><a href="index.php">Dining & Serving</a></li> <li><a href="index.php">Cookware</a></li> </ul> </li> <li><a href="index.php">Books</a></li> </ul> </nav> </div> <p>

The code in bold starts a session. Thereafter, it adds a span with the ID userinfo to the header. The span displays two hyperlinks—Login and Signup (see Figure 3-9). The Login hyperlink will navigate users to the signin.php script, which in turn will display a form prompting the user to enter credential information to sign in to your site. The Signup link will navigate the user to the validatesignup.php script, which displays a form that enables the user to register to your site.

Figure 3-9. The header modified to display the Login and Signup links, which enable visitors to signup and signin to the site