Page 201 - Open Soource Technologies 304.indd
P. 201

Unit 8: Objects



            // write information to the account file fclose($this->fp);                           Notes
            return array(‘filename’);
            }
            }
            ?>
            Store the Log class definition in a file called Log.inc. The HTML page in Example uses the Log
            class and PHP sessions to create a persistent log variable, $l.
            Example: front.php
            <?php include_once(‘Log.inc’);
            session_start( );

            ?>
            <html>
            <head>
            <title>Front Page</title>
            </head>
            <body>

            <?php $now = strftime(“%c”);
            if (!session_is_registered(‘l’))
            { $l = new Log(“/tmp/persistent_log”);
            session_register(‘l’);
            $l->write(“Created $now”);

            echo(“Created session and persistent log object.<p>”);
            } $l->write(“Viewed first page $now”);
            echo “The log contains:<p>”;
            echo nl2br($l->read( )); ?>
            <a href=”next.php”>Move to the next page</a>
            </body>

            </html>
            The output when this page is viewed is shown in Figure 8.3.
                                       Figure 8.3: The Front Page



                            http://192.168.0.1?~gnat/test/next.php  Google search
                         The log contains:

                          Created FriJan4 08:19:30 2010
                          Viewedfirst pageFri Jan4 08:19:30 2010
                          Move to the next page


            Example shows the file next.php, an HTML page. Following the link from the front page to this
            page triggers the loading of the persistent object $l. The _ _wakeup ( ) call reopens the logfile



                                             LOVELY PROFESSIONAL UNIVERSITY                                   195
   196   197   198   199   200   201   202   203   204   205   206