Alway
19 May 2008, 06:03 PM
I've been working on a simple script for my site that allows the admins of the site to post news or updates from a form. It works... almost. There are two major problems:
For some reason, the first letter of the first post will take the place of the first letter of the second post. The first letter of the second post replaces the first 2 letters of the third post, and so on. This has frustrated me for a long time.
The second problem is that when i try to display the news posts on the index page, only the first letter of the post's titles are shown. (side note: the letters in the titles are also replaced in the same ways as in the posts themselves)
Here is the code for the page that the form leads to when you press submit:
<?php
require('../script/sessionStart.php'); // start session
if($user->data['username']=='Alway'||'Vic'){ // check if admin
if($_POST['pass']=='(password here)') { //check pass
$post = $_POST['post'];
$title = $_POST['title']; // get form vars
require('news.php'); // get past news vars
if(!isset($inc)){
$inc=0;
} else{
$inc++;
}
$username=$user->data['username']; // get admin name
$date = date('d/m/y');
$myFile = "news.php"; // read news.php
$fh = fopen($myFile, 'r');
$theNews = fread($fh, filesize($myFile));
fclose($fh);
$theNews = '<?php $name['.$inc.'] = \''.$username.'\'; $title['.$inc.'] = \''.$title.'\'; $date['.$inc.'] = \''.$date.'\'; $post['.$inc.'] = \''.$post.'\'; ?>'.$theNews; // assemble new news.php page <?
//<?php
$fh = fopen($myFile, 'w') or die("can't open file"); // write new news.php page
fwrite($fh, $theNews);
fclose($fh);
$incCode = '<?php $inc='.$inc.'; ?>'; //<?
$fh = fopen($myFile, 'a');
fwrite($fh, $incCode);
fclose($fh);
echo '<html><head><meta http-equiv="refresh" content="4;url=http://safe.ulmb.com"></head><body><p>Post was successful. Now redirecting to home page.</p></body></html>'; // redirect to home
}
else{
echo 'Password was incorrect.'; // code for wrong pass
}
}
else{
echo('You must be logged in as an administrator to perform this action.'); // if not admin
}
?>
----------------------------------------------
Here's the code for my index page that tries to get information from the news.php file and display it:
require('index/news.php'); // get update vars
while($inc >= 0) { // while $inc is greater than 0...
if($name == 'Alway') { // who posted it
$avatar = 'alway.png';
}
elseif($name == 'Vic') {
$avatar = 'vic.png';
}
// echo all the code
echo '<table><tr><td class="title">'.$title[$inc].'</td></tr><tr>';
echo '<td class="box"><p><div style="font-size:8pt;background:#000000;color:#FFFFFF;text-align:center;float:left;padding:0px;margin-top:-12px;margin-right:10px;">'.$name[$inc].'<br /><img src="image/avatars/'.$avatar.'" alt="'.$name[$inc].'" style="margin:2px;margin-bottom:-1px;" /></div><strong>'.$date[$inc].'</strong> - '.$post[$inc].'<br /><br />';
echo '~'.$name[$inc].'</p>';
echo '<h3>Comments:</h3>';
echo '<div style="border:1px solid #000000;height:100px;width:90%;overflow:scroll;padding:10px;margin:0 auto;">Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br /></div>';
echo '</td></tr></table>';
$inc--; // decrement $inc
}
----------------------------------------
Finally, here's an example of what is written in the news.php page after I attempt three test updates.
<?php $name[2] = 'Alway'; $title[2] = 'TTother test.'; $date[2] = '19/05/08'; $post[2] = 'IIre I am testing this feature again.'; ?><?php $name[1] = 'Alway'; $title[1] = 'Tnother Test Update'; $date[1] = '19/05/08'; $post[1] = 'Ince again, I am testing this feature.'; ?><?php $name[0] = 'Alway'; $title[0] = 'This is a test update.'; $date[0] = '19/05/08'; $post[0] = 'I am testing this feature.'; ?> <?php $inc=0; ?><?php $inc=1; ?><?php $inc=2; ?>
I have been struggling with this for a very long time, and any help would be greatly appreciated.
For some reason, the first letter of the first post will take the place of the first letter of the second post. The first letter of the second post replaces the first 2 letters of the third post, and so on. This has frustrated me for a long time.
The second problem is that when i try to display the news posts on the index page, only the first letter of the post's titles are shown. (side note: the letters in the titles are also replaced in the same ways as in the posts themselves)
Here is the code for the page that the form leads to when you press submit:
<?php
require('../script/sessionStart.php'); // start session
if($user->data['username']=='Alway'||'Vic'){ // check if admin
if($_POST['pass']=='(password here)') { //check pass
$post = $_POST['post'];
$title = $_POST['title']; // get form vars
require('news.php'); // get past news vars
if(!isset($inc)){
$inc=0;
} else{
$inc++;
}
$username=$user->data['username']; // get admin name
$date = date('d/m/y');
$myFile = "news.php"; // read news.php
$fh = fopen($myFile, 'r');
$theNews = fread($fh, filesize($myFile));
fclose($fh);
$theNews = '<?php $name['.$inc.'] = \''.$username.'\'; $title['.$inc.'] = \''.$title.'\'; $date['.$inc.'] = \''.$date.'\'; $post['.$inc.'] = \''.$post.'\'; ?>'.$theNews; // assemble new news.php page <?
//<?php
$fh = fopen($myFile, 'w') or die("can't open file"); // write new news.php page
fwrite($fh, $theNews);
fclose($fh);
$incCode = '<?php $inc='.$inc.'; ?>'; //<?
$fh = fopen($myFile, 'a');
fwrite($fh, $incCode);
fclose($fh);
echo '<html><head><meta http-equiv="refresh" content="4;url=http://safe.ulmb.com"></head><body><p>Post was successful. Now redirecting to home page.</p></body></html>'; // redirect to home
}
else{
echo 'Password was incorrect.'; // code for wrong pass
}
}
else{
echo('You must be logged in as an administrator to perform this action.'); // if not admin
}
?>
----------------------------------------------
Here's the code for my index page that tries to get information from the news.php file and display it:
require('index/news.php'); // get update vars
while($inc >= 0) { // while $inc is greater than 0...
if($name == 'Alway') { // who posted it
$avatar = 'alway.png';
}
elseif($name == 'Vic') {
$avatar = 'vic.png';
}
// echo all the code
echo '<table><tr><td class="title">'.$title[$inc].'</td></tr><tr>';
echo '<td class="box"><p><div style="font-size:8pt;background:#000000;color:#FFFFFF;text-align:center;float:left;padding:0px;margin-top:-12px;margin-right:10px;">'.$name[$inc].'<br /><img src="image/avatars/'.$avatar.'" alt="'.$name[$inc].'" style="margin:2px;margin-bottom:-1px;" /></div><strong>'.$date[$inc].'</strong> - '.$post[$inc].'<br /><br />';
echo '~'.$name[$inc].'</p>';
echo '<h3>Comments:</h3>';
echo '<div style="border:1px solid #000000;height:100px;width:90%;overflow:scroll;padding:10px;margin:0 auto;">Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br />Test<br /></div>';
echo '</td></tr></table>';
$inc--; // decrement $inc
}
----------------------------------------
Finally, here's an example of what is written in the news.php page after I attempt three test updates.
<?php $name[2] = 'Alway'; $title[2] = 'TTother test.'; $date[2] = '19/05/08'; $post[2] = 'IIre I am testing this feature again.'; ?><?php $name[1] = 'Alway'; $title[1] = 'Tnother Test Update'; $date[1] = '19/05/08'; $post[1] = 'Ince again, I am testing this feature.'; ?><?php $name[0] = 'Alway'; $title[0] = 'This is a test update.'; $date[0] = '19/05/08'; $post[0] = 'I am testing this feature.'; ?> <?php $inc=0; ?><?php $inc=1; ?><?php $inc=2; ?>
I have been struggling with this for a very long time, and any help would be greatly appreciated.