Jump to content
Larry Ullman's Book Forums

Advanced Database Concepts : Session_Write_Close

Recommended Posts

I appreciate this book, and as usual, it is very easy to understand and great way to learn these topics.


My question concerns step 8 on page 94 of the book. The </body> and </html> tags are closed within the php tags, although the opening <body> and <html> were not. Is there any reason for this?


The next step mentions that session_write_close can be written anywhere in the script as long as the modifications to session data is over, so I'm assuming there isn't any reason to close the body and hmtl tags prior to this.


Hopefully I'm not over thinking this!

Link to comment
Share on other sites

If you look at the sessions script below the HTML tag is this <html lang="en">, its just got a language attribute in it. Body open tag just appears after the head.


Doesn't matter if you have php code written inside html tags before or after, so you can open up the session and close it anywhere you like as its server side code. Because the PHP code will be interpreted by the server and the HTML will be interpreted client side by the browser. You can use session_write_close() so long as you you current session manipulations are complete.


<?php # Script 3.2 - sessions.php
/* This page does some silly things with sessions.
* It includes the db_sessions.inc.php script
* so that the session data will be stored in a database.

// Include the sessions file:
// The file already starts the session.
?><!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>DB Session Test</title>
<link rel="stylesheet" href="style.css">
// Store some dummy data in the session, if no data is present:
if (empty($_SESSION)) {
$_SESSION['blah'] = 'umlaut';
$_SESSION['this'] = 3615684.45;
$_SESSION['that'] = 'blue';

// Print a message indicating what's going on:
echo '<p>Session data stored.</p>';

} else { // Print the already-stored data:
echo '<p>Session Data Exists:<pre>' . print_r($_SESSION, 1) . '</pre></p>';
// Log the user out, if applicable:
if (isset($_GET['logout'])) {
echo '<p>Session destroyed.</p>';

} else { // Otherwise, print the "Log Out" link:
echo '<a href="sessions.php?logout=true">Log Out</a>';
// Reprint the session data:
echo '<p>Session Data:<pre>' . print_r($_SESSION, 1) . '</pre></p>';
// Complete the page:
echo '</body>
// Write and close the session:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...