O'Reilly logo

PHP and MongoDB Web Development by Rubayeet Islam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - logging page visits with MongoDB

We are going to implement user request logging in the blog web app that we created in Chapter 2, Building your First MongoDB Powered Web App. We will build a Logger class that will handle logging. We will modify the blog.php file, the script used for viewing individual blog posts, to log page views through the logger module:

  1. Open up your text editor and create a new file named log.php. Add the following code to it:
    <?php require_once('dbconnection.php'); define('LOGNAME', 'access_log'); class Logger { private $_dbconnection; private $_db; public function __construct() { $this->_dbconnection = DBConnection::instantiate(); //obtain a reference to the collection where the data //will be logged $this->_collection ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required