LINUX.ORG.RU

История изменений

Исправление znavko, (текущая версия) :

Here 4 files that doing what you need:

- db.txt - is the database

- set.php - this file puts new lines into database

- get.php - this script shows in 2 different ways messages

- del.php - clears database

Listing set.php

<?php

// full path to db file
define("DB_FILE", "db.txt");

echo $_GET["line"];

$f=fopen(DB_FILE, "a");
fwrite($f, $_GET["line"]."--".time()."\n");

echo "<br>done!";

?>

This works like this: you send a GET-request like this: site.com/chat/set.php?line=I-was-looking-for-you-for-you-for-you and your message is writing into database file db.txt with a timestamp in the end

Listing db.txt

I-was-looking-for-you-for-you-for-you--1537393917

Listing get.php

<?php

// full path to db file
define("DB_FILE", "db.txt");
 
 
function read_tail($file, $lines) {
    //global $fsize;
    $handle = fopen($file, "r");
    $linecounter = $lines;
    $pos = -2;
    $beginning = false;
    $text = array();
    while ($linecounter > 0) {
        $t = " ";
        while ($t != "\n") {
            if(fseek($handle, $pos, SEEK_END) == -1) {
                $beginning = true; 
                break; 
            }
            $t = fgetc($handle);
            $pos --;
        }
        $linecounter --;
        if ($beginning) {
            rewind($handle);
        }
        $text[$lines-$linecounter-1] = fgets($handle);
        if ($beginning) break;
    }
    fclose ($handle);
    return array_reverse($text);
}
 

// main start
if (isset($_GET["mode"]) && $_GET["mode"]=="full") {
	$f=file_get_contents(DB_FILE);
	echo "<pre>".$f."</pre>";

} elseif (isset($_GET["n"]) && is_numeric($_GET["n"])) {
	$fsize = round(filesize(DB_FILE)/1024,2);
	echo "File size is {$fsize} Kb\n\n<br>";
	echo "Last ".$_GET["n"]." lines of the file:\n\n<br><br>";
	echo "<pre>\n";
	$lines = read_tail(DB_FILE, $_GET["n"]);
	foreach ($lines as $line) {
		echo $line;
	}
	echo "</pre>\n";

} else {
	//readl last line
	$f=fopen(DB_FILE, "r");
	$cursor = -1;
	fseek($f, $cursor, SEEK_END);
	$char = fgetc($f);
	// Trim trailing newline chars of the file
	while ($char === "\n" || $char === "\r") {
		fseek($f, $cursor--, SEEK_END);
		$char = fgetc($f);
	}
	// Read until the start of file or first newline char
	while ($char !== false && $char !== "\n" && $char !== "\r") {
		//Prepend the new char
		$line = $char . $line;
		fseek($f, $cursor--, SEEK_END);
		$char = fgetc($f);
	}
	
	echo $line;
}

echo "<br>done!";

?>

Here are, as you can see, two methods of viewing data: site.com/chat/get.php?mode=full - this displays all the lines. and site.com/chat/get.php?n=1 or n=2 - it shows last n lines.

Listing del.php

<?php


// full path to db file
define("DB_FILE", "db.txt");

file_put_contents(DB_FILE, "");

echo "<br>clear!";

?>

site.com/chat/del.php - deletes all data.

So the password and other options are in your further work.

Исходная версия znavko, :

Here 4 files that doing what you need:

- db.txt - is the database

- set.php - this file put new lines into database

- get.php - this script show in 2 different ways messages

- del.php - clear database

Listing set.php

<?php

// full path to db file
define("DB_FILE", "db.txt");

echo $_GET["line"];

$f=fopen(DB_FILE, "a");
fwrite($f, $_GET["line"]."--".time()."\n");

echo "<br>done!";

?>

This works like this: you send a GET-request like this: site.com/chat/set.php?line=I-was-looking-for-you-for-you-for-you and your message puts into database file db.txt with time in the end

Listing db.txt

I-was-looking-for-you-for-you-for-you--1537393917

Listing get.php

<?php

// full path to db file
define("DB_FILE", "db.txt");
//seconds between messages to check server online
define("PERIOD", 80);
 
 
function read_tail($file, $lines) {
    //global $fsize;
    $handle = fopen($file, "r");
    $linecounter = $lines;
    $pos = -2;
    $beginning = false;
    $text = array();
    while ($linecounter > 0) {
        $t = " ";
        while ($t != "\n") {
            if(fseek($handle, $pos, SEEK_END) == -1) {
                $beginning = true; 
                break; 
            }
            $t = fgetc($handle);
            $pos --;
        }
        $linecounter --;
        if ($beginning) {
            rewind($handle);
        }
        $text[$lines-$linecounter-1] = fgets($handle);
        if ($beginning) break;
    }
    fclose ($handle);
    return array_reverse($text);
}
 

// main start
if (isset($_GET["mode"]) && $_GET["mode"]=="full") {
	$f=file_get_contents(DB_FILE);
	echo "<pre>".$f."</pre>";

} elseif (isset($_GET["n"]) && is_numeric($_GET["n"])) {
	$fsize = round(filesize(DB_FILE)/1024,2);
	echo "File size is {$fsize} Kb\n\n<br>";
	echo "Last ".$_GET["n"]." lines of the file:\n\n<br><br>";
	echo "<pre>\n";
	$lines = read_tail(DB_FILE, $_GET["n"]);
	foreach ($lines as $line) {
		echo $line;
	}
	echo "</pre>\n";

} else {
	//readl last line
	$f=fopen(DB_FILE, "r");
	$cursor = -1;
	fseek($f, $cursor, SEEK_END);
	$char = fgetc($f);
	// Trim trailing newline chars of the file
	while ($char === "\n" || $char === "\r") {
		fseek($f, $cursor--, SEEK_END);
		$char = fgetc($f);
	}
	// Read until the start of file or first newline char
	while ($char !== false && $char !== "\n" && $char !== "\r") {
		//Prepend the new char
		$line = $char . $line;
		fseek($f, $cursor--, SEEK_END);
		$char = fgetc($f);
	}
	
	echo $line;
}

echo "<br>done!";

?>

Here are, as you can see, two methods of viewing data: site.com/chat/get.php?mode=full - this displays all the lines. and site.com/chat/get.php?n=1 or n=2 - it shows last n lines.

Listing del.php

<?php


// full path to db file
define("DB_FILE", "db.txt");

file_put_contents(DB_FILE, "");

echo "<br>clear!";

?>

site.com/chat/del.php - deletes all data.

So the password and other options are in your further work.