Simple script/code help please

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
furz
Forum Newbie
Posts: 6
Joined: Wed Nov 03, 2004 5:09 am

Simple script/code help please

Post 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!
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post 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");
}
furz
Forum Newbie
Posts: 6
Joined: Wed Nov 03, 2004 5:09 am

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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;
furz
Forum Newbie
Posts: 6
Joined: Wed Nov 03, 2004 5:09 am

Post by furz »

Umm, i put that in, it doesnt however stop the input from being printed again after hitting refresh.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

where i put


// send header

you need to, send a header
furz
Forum Newbie
Posts: 6
Joined: Wed Nov 03, 2004 5:09 am

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post 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."'>";
}
furz
Forum Newbie
Posts: 6
Joined: Wed Nov 03, 2004 5:09 am

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

session_start();

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

$_SESSION['visited'] = TRUE;

}
else
{

//redirect them

}
furz
Forum Newbie
Posts: 6
Joined: Wed Nov 03, 2004 5:09 am

Post 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.
Post Reply