PHP Code Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phreakee
Forum Newbie
Posts: 2
Joined: Mon Feb 23, 2004 11:35 am

PHP Code Help

Post by phreakee »

Hi

I have a guestbook for wap which supon execution, stores the data in a .dat file, only problem is the file gets to big for the mobile browser to open. Is there anyway i can change it so that it will only store 10 posts at a time, any older posts will be deleted/overwritten? here is the code

<?php
$filename = "guestbook.dat";
$file = "index.php?s=" . date('U');
$name = $_POST['name'];
$message = $_POST['message'];
if ($message) {
$name = strip_tags($name);
$message = strip_tags($message);
$name = StripSlashes($name);
$message = StripSlashes($message);
$message = ereg_replace("\r\n\r\n", " ", $message);
// $date = date("l, F j Y, H:i");
$date = date('d/M/Y H:i');
$message = "$name<br/>$date<br/>$message<br/>---<br/>";
$fp = fopen($filename, "r");
$gb = fgets($fp);
fclose($fp);
$guestbook = "$message" . "$gb";
$fp = fopen ($filename, "w");
fwrite ($fp, $guestbook);
fclose ($fp);
}
header('Content-type: text/vnd.wap.wml');
echo '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n";
?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<head>
<meta http-equiv="Cache-Control" content="no-cache"/>
</head>
<card id="post" title="Message Posted" ontimer="<?php
echo $file?>" >
<timer value="1"/>
<p align="center">
Please Wait....
<br/>
</p>
</card>
</wml>


Thanks for anyhelp ppl :)
steve@oeic.net
Forum Newbie
Posts: 8
Joined: Wed Feb 18, 2004 2:47 pm

Getting rid of older posts

Post by steve@oeic.net »

you could use the ftruncate function, in conjunction with filesize, to keep your file smaller than a certain size. This could very well result in partially deleted messages, however.

$size = filesize($filename);
if($size > 10000){
$fp = fopen($filename, 'w');
ftruncate($fp, 10000);
}

http://www.php.net/manual/en/function.ftruncate.php
phreakee
Forum Newbie
Posts: 2
Joined: Mon Feb 23, 2004 11:35 am

Post by phreakee »

where would i enter that code into the above? thanks again
Post Reply