
May 9th, 2003, 11:26 AM
|
|
LordNitrous
|
|
Join Date: May 2003
Location: England
Posts: 137
Time spent in forums: < 1 sec
Reputation Power: 6
|
|
|
Simple News Script Problems
Hey I am trying to code a news script in PHP and this is what I have done so far - note that it doesn't even post the news onto a text file even though it is Chmoded to 666 and is in all the right directories.
PHP Code:
<html>
<head><title>News</title></head>
<?php
// This part checks that the form is submitted, and then validates the password
if($http_post_vars['submit']) {
if($http_post_vars['password'] == 'passes') {
// This part makes sure that you entered a username
if(!$http_post_vars['name']) {
echo "you must enter a name";
exit;
}
// This part checks that you entered the blog entry
if(!$http_post_vars['news']) {
echo "you must enter some news";
exit;
}
/* This part checks that you do not include the pipe symbol because it will ruin the content
news file */
if(strstr($http_post_vars['name'],"|")) {
echo "name cannot contain the pipe symbol - |";
exit;
}
if(strstr($http_post_vars['news'],"|")) {
echo "news cannot contain the pipe symbol - |";
exit;
}
$fp = fopen('news.txt','a');
if(!$fp) {
echo "error opening file!";
exit;
}
$line = date("m.d.y") . "|" . $http_post_vars['name'];
$line .= "|" . $http_post_vars['news'];
$line = str_replace("\r\n","<br>",$line);
$line .= "\r\n";
fwrite($fp, $line);
if(!fclose($fp)) {
echo "error closing file!";
exit;
}
} else {
echo "bad password";
}
}
?>
<form action="<?=$php_self?>" method="post" name="newsentry">
your name:<br>
<input type="text" size="30" name="name"><br>
the news:<br>
<textarea name="news" cols="40" rows="5"></textarea><br><br>
news password:<br>
<input type="password" size="30" name="password"><br>
<input type="submit" name="submit" value="post it!"><br>
</form>
</body>
</html>
That was "addnews.php and here is the news display page:
PHP Code:
<html>
<head>
<title> new document </title>
<meta name="generator" content="editplus">
<meta name="author" content="">
<meta name="keywords" content="">
<meta name="description" content="">
</head>
<body>
<?php
// this sets the variable for data to the news file
$data = file('news.txt');
// this reverses the data so that the newest news is displayed first
$data = array_reverse($data);
foreach($data as $element) {
$element = trim($element);
$pieces = explode("|", $element);
echo $pieces[2] . "<br>" . "<b>posted by " . $pieces[1] . " on " . $pieces[0] . "</b><br><br>";
}
?>
</body>
</html>
Could someone please fix and also explain the code and why it wasn't working 
|