Shoutbox

PHP - Gestbook.... help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP - Gestbook.... help (/showthread.php?tid=75470)

PHP - Gestbook.... help by distorted on 06-19-2007 at 05:29 PM

Ok i'm trying to work on a script where my friends (who have photo's on there photobucket accounts etc....) can upload the img url to my form and it adds it to a flattext file, basicly its a modifyed guestbook, but i have little/no knowladge of PHP, i have got this to work in some shape or form, but whats making me struggle is i cant get the new entrys to go to the top of the page instead of being added to the bottom. i know its not possable to write to the top of a flat text, and the way to do this would be to get it to open the existing content from the file and then add that to the end of the new entry, and write it all in at once....? but i dont know how... any suggestions. heres my code:


<html>
<head>
<title>Upload</title>
</head>
<body>
<?php
include('config.php');
$user = $_GET["name"];
$message = $_GET["message"];
print("<b>Thank You!</b><br />Your information has been added! You can see it by <a href=savedinfo.php>Clicking Here</a>");
$out = fopen("savedinfo.php", "a");
if (!$out) {
print("Could not append to file");
exit;
}
fputs ($out,implode,("\n"));
fwrite($out,"<CENTER><font size='2' face='Verdana'><A href='$user' target='_blank'><img src='$user' width='300' height='200' style='border-color:#000000' border='1'></a><BR>$message<BR><BR></font></CENTER>");
fclose($out);
?>
</body>
</html>

user/name is the url of the image, message is simply a description. Thankyou in advance.

Kirk


RE: PHP - Gestbook.... help by absorbation on 06-19-2007 at 07:56 PM

What's in config.php and where are your forms located :)?


RE: PHP - Gestbook.... help by Baggins on 06-19-2007 at 08:22 PM

There is nothing in the script that is not defined there, I am guessing config.php is empty.

If you give me half an hour I will write a script for you, free of charge.


RE: PHP - Gestbook.... help by roflmao456 on 06-19-2007 at 08:23 PM

code:
<html>
<head>
<style type="text/css">
span.info{
text-align:center;
font-family:Verdana;
font-size:2;
}
</style>
<title>Upload</title>
</head>
<body>
<?php
include('config.php');
$user = $_GET["name"];
$message = $_GET["message"];
print("<b>Thank You!</b><br />Your information has been added! You can see it by <a href=savedinfo.php>Clicking Here</a>");
$out = fopen("savedinfo.php", "a");
if (!$out) {
print("Could not append to file");
exit;
}
fwrite($out,"<span class='info'><a href='".$user."' target='_blank'><img src='".$user."' width='300' height='200' style='border-color:#000000' border=1></a><BR>$message<BR><BR></span>".file_get_contents("savedinfo.php"));
fclose($out);
?>
</body>
</html>

will add the new entry on top of the new ones?
RE: PHP - Gestbook.... help by Baggins on 06-19-2007 at 08:39 PM

code:
<html>
<head>
<title>Upload</title>
</head>
<body>
<?php
$user = strip_tags($_GET["name"]);
$message = strip_tags($_GET["message"], '<b><i><u><a>');
$handle = @fopen("./savedinfo.php", "r+");
if ($handle) {
    while (!feof($handle)) {
        $buffer .= fgets($handle, 4096);
    }
    fwrite($handle,"<CENTER><font size='2' face='Verdana'><A href='$user' target='_blank'><img src='$user' width='300' height='200' style='border-color:#000000' border='0'></a><BR>$message<BR><BR></font></CENTER>\n");
    fwrite($handle, $buffer);
    fclose($handle);
}
print("<b>Thank You!</b><br />Your information has been added! You can see it by <a href=savedinfo.php>Clicking Here</a>");
?>
</body>
</html>


This does exactly what roflmao's does, except it is safe from PHP and HTML injection, and allows for basic tags such as <b><i><u> and <a> in the message field.

I figure this method, opening the file for read/write, will be more efficent than filegetcontents() because you only need to open it once.
RE: PHP - Gestbook.... help by distorted on 06-20-2007 at 09:44 AM

Thankyou all for your prompt replys. i will try the suggested ways and see which works for me :D and yes config.php is empty, not to sure why its there either, was just in the tutorial i used to make it. thanks again x


RE: PHP - Gestbook.... help by distorted on 06-20-2007 at 10:06 AM

For some reason Baggins when i use your script it sort of works (as in new entrys go to the top) but for some reason it dosent overwrite the existing entrys it just adds the new one with the old ones again, for exaple when i upload twice i have 3 images, as the second time it adds the new image to the old and then places the new and old image behind the old one alredy there. so it goes.

upload once:

Image 1        //new

Upload Twice:

Image 1       //existing entry
Image 2       //new
Image 1       //new

after 3 uploads i have 7 images. as it adds the new one to the top of the list then writes that below the existing 3.

understand what i mean?

and when i use your's roflmao's i get this error message.

Fatal error: Call to undefined function: file_get_contents() in c:\apache\htdocs\sendinfo.php on line 23

help??


RE: PHP - Gestbook.... help by WDZ on 06-21-2007 at 02:10 AM

quote:
Originally posted by distorted
Fatal error: Call to undefined function: file_get_contents() in c:\apache\htdocs\sendinfo.php on line 23
What PHP version are you running? It must be ancient (2002 or earlier) if it doesn't have file_get_contents()... :-/

To check the version, you can use phpinfo().

If you want to fix the code Baggins posted, adding this line right before the first "fwrite" line should do the trick...

code:
rewind($handle);

RE: PHP - Gestbook.... help by distorted on 06-21-2007 at 07:00 AM

Thankyou so much WDZ your a life saver it now works perfectly. i'm running PHPTRIAD to test scripts so i presume it is rather acccent. thanks again. x