oderza Posted October 19, 2012 Share Posted October 19, 2012 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 More sharing options...
Edward Posted October 20, 2012 Share Posted October 20, 2012 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. require('db_sessions.inc.php'); ?><!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>DB Session Test</title> <link rel="stylesheet" href="style.css"> </head> <body> <?php // 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'])) { session_destroy(); 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> </html>'; // Write and close the session: session_write_close(); ?> Link to comment Share on other sites More sharing options...
oderza Posted October 20, 2012 Author Share Posted October 20, 2012 Thanks Edward, I appreciate the response. 1 Link to comment Share on other sites More sharing options...
Recommended Posts