单击清除日志文件链接时再次重定向到日志文件,它不会清除日志文件

时间:2017-11-02 12:02:29

标签: php html

有错误显示:

  

“未定义索引:清除”

问题出在logfile.php中,但我找不到错误。我还附上了错误的快照。单击clear logfile链接时重定向到logfile.php,它也不会清除log.txt文件。我该怎么办。 #救命 :) enter image description here

的login.php

<?php
require_once("../../includes/initialize.php");
// require_once("../../includes/user.php");
// require_once("../../includes/function.php");
// require_once("../../includes/session.php");
if($session->is_logged_in()){
    redirect_to("index.php");
}
 //Remember to give your form's submit tag a name ="submit " attribute
 if(isset($_POST['submit'])){
    $username =trim($_POST['username']);
    $password =trim($_POST['password']);

 $found_user=User::authenticate($username,$password); 

 if($found_user){
    $session->login($found_user);
    log_action('Login',"{$found_user->username} logged in.");
    redirect_to("index.php");


 }else{
    //username/password was not found in the database
    $message ="username/password combination incorrect";
    echo $message; }
 }else{
    //Form has been submitted.
    $username ="";
    $password=""; 
 }


?>
<!DOCTYPE html>
<html>
<head>
    <title>form</title>
</head>
<body>
<form action ="login.php" method ="post">
<table>
<tr>
    <td>Username:</td>
    <td><input type ="text" name="username" maxlength ="30" value ="<?php echo htmlentities($username)?>"/></td>
</tr>
<tr>
    <td>Password:</td>
    <td><input type ="Password" name ="password" maxlength="20" value ="<?php echo htmlentities($password)?>"/></td>
</tr>
<tr>
<td>
    <input type="submit" name="submit" value ="login"/>
</td>
</tr>
</table>
</form>
</body>
</html>

function.php

<?php require_once('initialize.php');?>
<?php
function strip_zero_from_data($marked_string = ""){

//first remove the marked zero
    $no_zero = str_replace('*0','',$marked_string);
// then remove any remaining marked_string
$cleaned_string = str_replace('*','',$no_zero);
return $cleaned_string;
}

function redirect_to($location=Null){

if($location!=Null){
    header("Location: {$location}");
    exit;
}

function output_message($message = ""){

    if(!empty($message)){
        return "<p class =\"message\">{$message}</p>";}
        else{return "";}
    }
}
function __autoload($class_name){
$class_name =strtolower($class_name);
$path ="../includes/{$class_name}.php";
if(file_exists($path)){
    require_once($path);
}
else{
    die("The file {$class_name}.php could not be found");
}
}

function include_layout_template($template="")
{
    include(SITE_ROOT.DS.'public'.DS.'layouts'.DS.$template);
}

function log_action($action,$message =""){
$logfile =SITE_ROOT.DS.'log'.DS.'log.txt';
$new = file_exists($logfile)?false :true;

if($handle = fopen($logfile,'a')){//append
    $timestamp = strftime("%m-%d-%Y-%H-%M",time());
$content ="{$timestamp} |{$action} :{$message}\n";
fwrite($handle,$content);
fclose($handle);

if($new){
    chmod($logfile, 0755);
}
}
else{
    echo "Could not open log file for writing";
}
}

    ?>

logfile.php

<?php
require_once("../../includes/initialize.php");
?>
<?php
if(!$session->is_logged_in()){
    redirect_to("login.php");
}
?>


<?php
$logfile =SITE_ROOT.DS.'log'.DS.'log.txt';


if($_GET['clear']=='true'){
    file_put_contents($logfile,'');

    //add the first log entry
    log_action("Log cleared","by user id {$session->user_id}");
    //redirect to the same page so that the URL wont have "clear=true" anymore
    redirect_to('logfile.php');
}
?>

<?php
include_layout_template('admin_header.php');
?>
<a href="index.php">&laquo; 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')){
    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}.";
}
?>

1 个答案:

答案 0 :(得分:0)

"logfile.php ?clear =true"

删除2个空格。

另外

if(isset($_GET['clear'])){
    //use $_GET['clear'] variable here, not outside
}
相关问题