Forum Moderators: coopster

Message Too Old, No Replies

unknown problem?

problem parsing?> on line 18?

         

yt404

8:55 pm on May 1, 2004 (gmt 0)

10+ Year Member



<?php
$filename = "test.txt";
$box = "http://www.example.com/PHP/shout.php"; // URL of shout box
print "Your shout has been added to the box, <a href=$box>Click Here</a> to go view the box";
if (empty($name) ¦¦ empty($message)) {
header( "Location: [example.com...] );
}
else {
$a = fopen( $filename, "a") or die ("could not open $filename for writing please check file permissions");
flock($a, LOCK_SH);
fwrite($a, "<font face=verdana size=2><b>$name</b> : $message</font>
<hr noshade size=1>");
flock($a, LOCK_UN);
fclose($a);
?>

i am getting a parse error on the line that contaisn the php close tag?>
i deleted it, and i got the same error on the line that contains, fclose($a);

can anyone see any problems looking at this script, it should work! if the name field and the message field are empty then it takes the user to error.php if not they get the printed message, anyone help?

[edited by: jatar_k at 5:25 pm (utc) on May 2, 2004]
[edit reason] generalized urls [/edit]

DavidC

8:57 pm on May 1, 2004 (gmt 0)

10+ Year Member



are u missing a } (to close the if statment) at the end of the code? :)

yt404

9:04 pm on May 1, 2004 (gmt 0)

10+ Year Member



ok i realised that was blind } sorted, however there is still a problem, i have set the script up so that if the name field and message field are empty that it takes the user to the error.php page, however when i test this i get the following,

Your shout has been added to the box, Click Here to go view the box
Warning: Cannot modify header information - headers already sent by (output started at /home/www/juttuffi/PHP/add.php:4) in /home/www/juttuffi/PHP/add.php on line 6

i am totally new to php, this is my first real script that can be used by anyone and i honestly cannot see what the problem is, if anyone could point it out i would be grateful.

thanks

chris

DavidC

9:12 pm on May 1, 2004 (gmt 0)

10+ Year Member



Hi

make sure there is no white space at the top of the script. Make sure the code is right at the top of the page.

sebbothebutcher

9:25 pm on May 1, 2004 (gmt 0)

10+ Year Member



you can also write

if(!$name ¦¦!$message)

to verify the existance of a variable.
by the way, what should the "or die" mean?

strange coding some people use ;-)

WhosAWhata

9:32 pm on May 1, 2004 (gmt 0)

10+ Year Member



the or die mean execute the folling command if the function returns false (doesn't work) as in if you try to write to a file without permission
the problem with the header is that you can only set a new header if you haven't sent any other data to the browser (like print or echo statements)

yt404

10:19 pm on May 1, 2004 (gmt 0)

10+ Year Member



i see so the line that reads..

print "Your shout has been added to the box, <a href=$box>Click Here</a> to go view the box";

is being sent to the browser because the check is after that, so i should probably put that line right at the bottom of the code? or before the fwrite()

thanks for pointing that out with any luck it will work (hopefully) i'v been on with this script all day.

I dont suppose you would know how i could nest another if statement in there somewhere

i was hoping to find something that would delete the contents of the file and execute the following

if filesize() > 512 { delete contents of file }

just to stop the file getting too big, also does php take 512 as kb or just bytes?

WhosAWhata

4:04 am on May 2, 2004 (gmt 0)

10+ Year Member



filesize() returns bytes

your code might look like this

<?php
$filename = "test.txt";
if(filesize($filename) > 512) {
unlink($filename);
}

$box = "http://www.example.com/PHP/shout.php"; // URL of shout box
if (empty($name) ¦¦ empty($message)) {
header( "Location: [example.com...] );
}
print "Your shout has been added to the box, <a href=$box>Click Here</a> to go view the box";

else {
$a = fopen( $filename, "a") or die ("could not open $filename for writing please check file permissions");
flock($a, LOCK_SH);
fwrite($a, "<font face=verdana size=2><b>$name</b> : $message</font>
<hr noshade size=1>");
flock($a, LOCK_UN);
fclose($a);
?>

[edited by: jatar_k at 5:26 pm (utc) on May 2, 2004]
[edit reason] generalized urls [/edit]

yt404

10:02 am on May 2, 2004 (gmt 0)

10+ Year Member



cheers m8, thats what i was thinking, i'm determined to get this sorted by the weekend, with any luck this will work! fingers crossed

yt404

10:08 am on May 2, 2004 (gmt 0)

10+ Year Member



does php take 512 as 512 kilobits or 512 bits?

yt404

10:41 am on May 2, 2004 (gmt 0)

10+ Year Member



ok, it works, but the unlink() is having a bit of trouble, i have had a bit of a scower on php.net and a few forums and i can't find anything about specifically deleting the whole contents of a file, could this have something to do with the:

$a = fopen($filename, "a")

i want things to appear at the end of the file so i used a, i dont want to replace or overwrite data which i think w does, so i need to delete the contents when it gets to a certain size, it says i do not have permission

is this because i am opening the file to be appended? i understand it it just wont bloody work, can anyone at all shed some light on the error the codes below, thanks to everyone who has helped out so far, this has really gotten me into php, i'm definately gonna get me a decent book and read up on it!

<?php

$error = "http://www.example.com/PHP/error.php"; // location of error page
$filename = "data.txt";
$box = "http://www.example.com/PHP/shout.php"; // URL of shout box
$max = 2048; // maximum size (bytes) of data.txt file

if(filesize($filename) > $max) {
unlink($filename);
}
if (empty($name) ¦¦ empty($message)) { header( "Location: $error" );
} else {
print "Your shout has been added to the box, <a href=$box>Click here</a> to go back the the box ";
$a = fopen( $filename, "a") or die ("could not open $filename for writing please check file permissions");
flock($a, LOCK_SH);
fwrite($a, "<font face=verdana size=2><b>$name</b> : $message</font>
<hr noshade size=1>");
flock($a, LOCK_UN);
fclose($a);
}
?>

[edited by: jatar_k at 5:26 pm (utc) on May 2, 2004]
[edit reason] generalized urls [/edit]

WhosAWhata

8:02 pm on May 2, 2004 (gmt 0)

10+ Year Member



you need to manually set permissions to that file using your FTP or online control pannel.
you need to make sure file permissions are set to 0777