My PHP tagboard is almost finished.

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
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

My PHP tagboard is almost finished.

Post by Daisy Cutter »

I've finally carried out my dream of creating a PHP tagboard. Yay!
I am almost finished polishing it off....

What is left to do is make it so that the script displays the last N number of lines of tag.txt, but with the option of displaying the number of pages, and links to them in a drop-down list. I can imagine this would be something not too difficult, but I can't figure out how.

All help is appreciated :)
And here's the script:

Code: Select all

<?php 
// tagboard 1.0
// only works on *nix with commands rm, touch, and echo installed.
/* 

this file is protected under the GPL: http://www.gnu.org/copyleft/gpl.html
i dont know why it does some of the things it does, but it works 
admin mode coming soon

*/
$randstr = RandomString(5);
if(!isset($write)) {
system("rm .*");
system("touch .$randstr"); }

//check if tag.txt exists, if not, create it.
if (file_exists("tag.txt")) {}
else { system("touch tag.txt");chmod("tag.txt", 0666); }

//thanks to xentrik.net for these bits of maliciousness stripping code.
$msg = str_replace("<","<",$msg);
$msg = str_replace(">",">",$msg);
$msg = str_replace("\r\n"," ",$msg);
$msg = str_replace("\n"," ",$msg);
$msg = str_replace("|","l",$msg);
$msg = stripslashes($msg);
$name = str_replace("<","<",$name);
$name = str_replace(">",">",$name);
$name = str_replace("\r\n"," ",$name);
$name = str_replace("\n"," ",$name);
$name = str_replace("|","l",$name);
$name = stripslashes($name);

$file = "tag.txt";
$Array = file($file);

// print the tagboard
// TODO: limit # of tags output, create a list of pages
if (!isset($write)) {
foreach ($Array as $key => $val) {
echo $val;
echo "<br />";
}
}

//write the tag
if(isset($write)) { 
	if($ver=="") { echo "enter the verification code. <a href=\"javascript:history.go(-1)\">back</a>";exit; } else {
		// if you've entered the right code
		if (file_exists(".".$ver)) { 
			system("rm .*");
			echo "<h4>success!</h4><a href=\"javascript:history.go(-1)\">back</a>"; 
			//add the string to tag.txt
			system("echo \"<strong>$name</strong>: $msg\">>tag.txt"); 
		} else { echo "enter the verification code. <a href=\"javascript:history.go(-1)\">back</a>";exit; }
	}
	exit;
 } else {}
?>

<hr />
<em>note: you may have to refresh your page to view your new tag.</em><br />
<form method="post" action="tag.php?write"><div>
<input type="text" name="name" value="name" /><br />
<input type="text" name="msg" value="message" /><br />
enter: <code><?php echo $randstr ?></code><br />
<input type="text" name="ver" value="" /><br />
<input type="submit" value="submit" />
</div></form>

<?php 
// function to generate random strings 
function RandomString($length=32) 
{ 
$randstr=''; 
srand((double)microtime()*1000000); 
//our array add all letters and numbers if you wish
$chars = array ( 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); 
for ($rand = 0; $rand <= $length; $rand++) 
{ 
$random = rand(0, count($chars) -1); 
$randstr .= $chars[$random]; 
} 
return $randstr; 
} 
?>
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

to be honest, i did not understand the use of the application but lemme c whether i can help u

u want to split a file into pages.
if the above stmt is rt,,,then

Code: Select all

assign a fixed number of lines to a page, say 10 lines
you have the array of lines from file($file), count the lines; divide by 10 ; now those are the pages available
create dynamic select box with option having values of each page no and have a submit button
on $_GET, get the page no, if its 2, then you have to display 11 to 20 lines so read the file again file($yourfile)
use the array and display lines between ($arrayindex < $page*10 && $arrayindex > ($page*10) - 10)
hope this helps :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

or he could just search the boards for "pagination" :P
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

Pagination (what an odd name that is) looks like it will work with some patience. My issuse is now not how to make this:

Code: Select all

foreach ($Array as $key => $val) {
echo $val;
echo "<br />";
}
Output only a few lines at the end of the file. For the record, $Array is just a plaintext file with my tags put in it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you get the last x number of elements, array_slice() works quite well.
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

feyd wrote:you get the last x number of elements, array_slice() works quite well.
I can see how you'd use that to get the first few lines of a file, but the last few, in a file that gets content constantly added... I can not figure that out.

Maybe system("tail --lines=x tag.txt"); would work, but that seems very half-assed (like the rest of my script :p)

There must be some PHP way to get just the end of an array to display, even if the array's size will change constantly. I tried combining end() and array_slice(), but I couldn't get it to start at the end and back up, say, 5 entries.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

get the last 5 elements of a given array:

Code: Select all

$output = array_slice($input, -5);
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

feyd wrote:get the last 5 elements of a given array:

Code: Select all

$output = array_slice($input, -5);
Brilliant. Feyd, I owe you about a million by now.
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

after all that hard work I've finally finished my tagboard. you can view the final source at my site:
http://kafene.org/scripts/tag.txt

I dont know if I'm ever going to change that. You can use a live version (please no spamming, even though it helps protect against that somewhat) at my site (it's linked under my sig.)

It works, and you can include it on a page just fine. I even took some steps to ensure portability, but as the license says "no guarantees."

It doesn't work on Windows servers, unless you have the unix commands used in it installed and working. I may someday write those out, but don't count on it.

It's free for all to use under the GPL, I hope you find it as useful as I have. You may want to change the $file variable.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

I'm surprised no one mentioned storing the file in reverse order, then using fgets() to get the first 5 lines (latest 5 messages), that wouldn't work as well with pagination tho.


EDIT: just tested your tagboard, your validation code is basically meaningless just to let you know, since you're outputting it as plain text and all, you need to output it inside of an image.. (it was also hard to read as the font was so small)
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

jshpro2 wrote:I'm surprised no one mentioned storing the file in reverse order, then using fgets() to get the first 5 lines (latest 5 messages), that wouldn't work as well with pagination tho.


EDIT: just tested your tagboard, your validation code is basically meaningless just to let you know, since you're outputting it as plain text and all, you need to output it inside of an image.. (it was also hard to read as the font was so small)
Well it's not meaningless against indiscriminate spamming robots, someone could always script a workaround that gets the code, then fills it in, but ... if someone is that bored, they need a life, because deleting those posts would be as simple as editing tag.txt. You can always copy/paste it, as well.

I had one that wasn't in reverse order before, I found some code that writes to the beginning of a file, but decided against using it.

I tweaked it a little bit more, the verification code is a little more obvious now.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Daisy Cutter wrote:
jshpro2 wrote:I'm surprised no one mentioned storing the file in reverse order, then using fgets() to get the first 5 lines (latest 5 messages), that wouldn't work as well with pagination tho.


EDIT: just tested your tagboard, your validation code is basically meaningless just to let you know, since you're outputting it as plain text and all, you need to output it inside of an image.. (it was also hard to read as the font was so small)
Well it's not meaningless against indiscriminate spamming robots, someone could always script a workaround that gets the code, then fills it in, but ... if someone is that bored, they need a life, because deleting those posts would be as simple as editing tag.txt. You can always copy/paste it, as well.

I had one that wasn't in reverse order before, I found some code that writes to the beginning of a file, but decided against using it.

I tweaked it a little bit more, the verification code is a little more obvious now.
Actually, 90%+ of spamming bots now are made to get around security measure's like that. Even putting it in an image isn't 100% secure, just a lot better than plain text.
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

LiLpunkSkateR wrote:
Daisy Cutter wrote:
jshpro2 wrote:I'm surprised no one mentioned storing the file in reverse order, then using fgets() to get the first 5 lines (latest 5 messages), that wouldn't work as well with pagination tho.


EDIT: just tested your tagboard, your validation code is basically meaningless just to let you know, since you're outputting it as plain text and all, you need to output it inside of an image.. (it was also hard to read as the font was so small)
Well it's not meaningless against indiscriminate spamming robots, someone could always script a workaround that gets the code, then fills it in, but ... if someone is that bored, they need a life, because deleting those posts would be as simple as editing tag.txt. You can always copy/paste it, as well.

I had one that wasn't in reverse order before, I found some code that writes to the beginning of a file, but decided against using it.

I tweaked it a little bit more, the verification code is a little more obvious now.
Actually, 90%+ of spamming bots now are made to get around security measure's like that. Even putting it in an image isn't 100% secure, just a lot better than plain text.
Really? I never even had an issue with spam bots, even when I had no protection at all on the form. I wrote a charachter limit for the name and message to (16/64 seemed good, but they can be changed), if spam becomes a real issue, then I can make it an image. The text code at least helps against people laying into the refresh button.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

I've never had problems either, but I know how they are built. A lot of times they will target forums, but if someone had something against you they could whip up one real quick.
Post Reply