Page 1 of 1

Simple script/code help please

Posted: Wed Nov 03, 2004 5:15 am
by furz
Ok, this is a basic script... I didnt write it, got it from somewhere a long time ago cant remember where... basically just gets input from user, saves it to file, then prints the file to screen (basically a simple chat system).

Ok, its kinda messy, i understand that... if anyone has another way of doing it, i'm sure it can be done more efficiently...
Basically my problem is that after you submit your thing, the data still stays there (as in, if you press refresh, it updates the thing again, with what was previously in there...). How do i go about clearing what's in the boxes? so that if refresh is pressed, it wont display the same thing...
(hope this makes sense, i didnt explain it to well).

Anywho, here's the code:

Code: Select all

<?
$chat_file_ok = "ideas.txt";
$chat_length = 25;
$max_single_msg_length = 100000;
$max_file_size = $chat_length * $max_single_msg_length;
$file_size= filesize($chat_file);
if ($file_size > $max_file_size)
{
	$lines = file($chat_file_ok);
	$a = count($lines);
	$u = $a - $chat_length;
	for($i = $a; $i >= $u ;$i--)
	{
		$msg_old =  $lines[$i] . $msg_old;
	}
	$deleted = unlink($chat_file_ok);
	$fp = fopen($chat_file_ok, "a+");
	$fw = fwrite($fp, $msg_old);
	fclose($fp);
}
$person = str_replace ("\n"," ", $person);
$person = str_replace ("<", " ", $person);
$person = str_replace (">", " ", $person);
$person = stripslashes ($person);
$msg = str_replace ("\n"," ", $message);
$msg = str_replace ("<", " ", $msg);
$msg = str_replace (">", " ", $msg);
$msg = stripslashes ($msg);
if ($msg != "" && $msg != "Quote" && $person != "Author" && $person != "" )
{
	$fp = fopen($chat_file_ok, "a+");
	$fw = fwrite($fp, "\n"$msg" - $person<br>");
	fclose($fp);
}
$lines = file($chat_file_ok);
$a = count($lines);
$u = $a - $chat_length;
for($i = $a; $i >= $u ;$i--)
{
	echo "$lines[$i]";
}
?>
Thanks!

Posted: Wed Nov 03, 2004 11:07 am
by Getran
If i'm right in thinking what you want, maybe you could try to add a header() just after the data is stored.

Something like this..

Code: Select all

if ($msg != "" &amp;&amp; $msg != "Quote" &amp;&amp; $person != "Author" &amp;&amp; $person != "" )
{
    $fp = fopen($chat_file_ok, "a+");
    $fw = fwrite($fp, "\n"$msg" - $person&lt;br&gt;");
    fclose($fp);
    header("Location: thisfilename.php");
}

Posted: Wed Nov 03, 2004 7:04 pm
by furz
Hrm... I put that in and it seems to break...

Warning: Cannot modify header information - headers already sent by (output started at /data/www/fury/html/index.php:12) in /data/www/fury/html/index.php on line 130

Line 12 being the start of css (after the line with the <head> tag)
Line 130 being: header("Location: index.php");

Thanks.

Posted: Wed Nov 03, 2004 8:36 pm
by rehfeld
you cant send a header after outputting html, or whitespace.

you will need to make sure the header is sent before anything else.

you could change

Code: Select all

for($i = $a; $i >= $u ;$i--)
{
    echo "$lines[$i]";
}

to

Code: Select all

$output = '';
for($i = $a; $i >= $u ;$i--)
{
    $output .= "$lines[$i]";
}

// send header

echo $output;

Posted: Wed Nov 03, 2004 9:22 pm
by furz
Umm, i put that in, it doesnt however stop the input from being printed again after hitting refresh.

Posted: Wed Nov 03, 2004 10:19 pm
by rehfeld
where i put


// send header

you need to, send a header

Posted: Thu Nov 04, 2004 6:49 am
by furz
// send header
Sorry how is this done?

header("Location: index.php");

adding that just causes the same error as above

i'm sure you've already realised this script is placed within html...

anywho, thx for your help.

Cheers.

Edit:
BTW, i tried splitting it up so everything was before the head and the printing bit was in the body but it didnt load the page.

Posted: Thu Nov 04, 2004 1:44 pm
by rehfeld
again, you cannot send any html or any outher output before sending a header

you need to fix that.

Code: Select all

<?php

// do all code here
// dont output anything yet
// if you need to send a header, do it now before sending any html
?>
<html>
... etc

<?php echo $output; ?>

</html>
this is a band aid, and will slow your pages down a bit, but you can add

Code: Select all

<?php ob_start(); ?>
at the very top of the script, BEFORE ANY OUTPUT. then it will work.

http://php.net/ob_start

Posted: Mon Nov 08, 2004 9:43 am
by Getran
you could just scrap the whole header() and use a html redirect like this:

Code: Select all

if ($msg != "" && $msg != "Quote" && $person != "Author" && $person != "" )
{
    $fp = fopen($chat_file_ok, "a+");
    $fw = fwrite($fp, "\n"$msg" - $person<br>");
    fclose($fp);
    // Time taken in seconds to redirect
    $redirect_time = '2';
    // Place to redirect to, can just be the same page
    $redirect_url = 'pagetogoto.php'
    echo "<meta http-equiv='REFRESH' content='".$redirect_time.";url=".$redirect_url."'>";
}

Posted: Tue Nov 09, 2004 6:09 am
by furz
Getran, that still doesnt solve the problem of data being resend if you press refresh.
I havent had time to play with it yet...
I have an idea i'll try after the weekend (have last exam on thursday so yeah).

Thx.

Posted: Tue Nov 09, 2004 6:35 am
by John Cartwright

Code: Select all

session_start();

if (empty($_SESSION['visited']))
{

$_SESSION['visited'] = TRUE;

}
else
{

//redirect them

}

Posted: Wed Nov 10, 2004 1:19 am
by furz
Sorry Phenom, where does this code go?
(i'm not too good at coding :P it's all guess and test to me).

Thanks.