I am adapting some scripts that I found through Google that monitor users who login in to a website.
This consists of including this in each page I want to monitor:
<?
include_once("config.php");
include_once("functions.php");
?>
config.php looks like this (I have removed info on actual database):
<?php
DEFINE ('DB_USER', '');// database username
DEFINE ('DB_PASSWORD', '');//database password
DEFINE ('DB_HOST', '');//database host, usually localhost
DEFINE ('DB_NAME', '');//and finally the database name
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error());
@mysql_select_db (DB_NAME) OR die('Could not select the database: ' . mysql_error() );
?>
functions.php looks like this:
<?php
$page = $_SERVER['PHP_SELF'];
$user = $_SESSION["name"];
$logq = "INSERT INTO logs (Name, Page, Date) VALUES ('$user', '$page', NOW())";
$logr = @mysql_query($logq);
?>
This works fine, i.e. the table in my database is populated correctly.
However the file that is supposed to generate a table on a webpage to show the information in the MySQL table isn’t working. The file, logs.php, looks like this:
<?php
$lq = "SELECT id, Name, Page, DATE_FORMAT(date, '%d %M, %Y') as sd FROM logs ORDER BY id DESC LIMIT 50";
$lr = @mysql_query($lq);
if($lr){
echo "<table><th>Name</th><th>Page</th><th>Date</th>";
while($lf = mysql_fetch_array($lr, MYSQL_ASSOC)){
echo "<tr><td>" . $lf['Name'] . "</td><td>" . $lf['Page'] . "</td><td>" . $lf['sd'] . "</td></tr>";
}
echo "</table>";
}
else
{
echo "No results!";
}
?>
Even though there is data in the MySQL table I am always getting just ‘No results!’ showing when I browse to the page.
Can anyone see what the problem is?
Perhaps you don’t have a db connection in logs.php? If your code is the full file then this should be the problem.
If so, just include config.php at the top of the page.