I am currently studying PHP, and am making a logging file function for the backend of a photogallery site.
<?php require_once("../includes/initialize.php"); ?>
<?php if (!$session->is_logged_in()) { redirect_to("login.php"); } ?>
<?php
$logfile = SITE_ROOT.DS.'logs'.DS.'log.txt';
if (isset($_POST['clear']))
{
if($_GET["clear"] == true) {
file_put_contents($logfile, '');
//Adds first log entry
log_action('Logs Cleared', "by USER {$session->username}");
//redirect to this same page so that the URL will be clean (IE won't have 'clear = true')
redirect_to('logfile.php');
}
}
?>
<?php include_layout_template('admin_header.php'); ?>
<a href="admindex.php">« Back</a><br />
<br />
<h2>Log File</h2>
<p><a href="logfile.php?clear=true">Clear log file</a><p>
<?php
if( file_exists($logfile) && is_readable($logfile) &&
$handle = fopen($logfile, 'r')) { // read
echo "<ul class=\"log-entries\">";
while(!feof($handle)) {
$entry = fgets($handle);
if(trim($entry) != "") {
echo "<li>{$entry}</li>";
}
}
echo "</ul>";
fclose($handle);
} else {
echo "Could not read from {$logfile}.";
}
?>
<?php include_layout_template('admin_footer.php'); ?>
I am having trouble getting the clear function to work properly.
The link works, but it is not clearing the log.txt file like I want it to, and when it redirects, it still says "logfile.php?clear=true at the browser line, and I want it to redirect to LogFile.php. "
what am I doing wrong?