Please be gentle I have never programmed a single thing in my life and have been using PHP for about 3 weeks now.
I am writing a very very simple admin script:
I am trying to allow my user to update his own information on his website. I set up the information page to read from a simple text file (this user has no database). And the admin script is updating the text file.
When ever an ' is entered into the content section of the form it is being replace with ' and written to the text file in that format. Then when the information page reads from the text file low and behold all of the ' are showing at '.
I have been trying to find out why for about a week now. I have read through my PHP book and the PHP manual. I know I could write a section to find and replace ' using ereg() or variants but I would think there would be a better way to go about this.
I tried commenting out the form validation and the find replace thinking that maybe one of the two was causing my problem. However that did not fix it. So I am left completely baffled.
Please any information would be of great help. π
<?php
//create short variables
$debug = 'N';// <- 'Y' or 'N' Value
$date = date('F jS, Y').' at '.date(' g:i a');
$page_name = ($HTTP_POST_VARS['page_name']);
$take_action = ($HTTP_POST_VARS['take_action']);
$content = $HTTP_POST_VARS['content'];
$text_file = $page_name.'.txt';
//set $content to match the format for a particular page
if ($page_name = 'index' || 'events'){
$content = '<span class="sep">Update '.$date.'</span>'."\n\n".$content."\n\n\n";
}
else{
$content = $content."<br /><br />";
}
//set $took_action according to $take_action. This will be used for form feedback
if ($take_action == 'a') {
$took_action = 'Appended '.$page_name.' Page';
}
else {
$took_action = 'Replaced '.$page_name;
}
//Validate data inputted by user
if (!class_exists('validator')) {
include_once("../objects/validator/validator-class.php");
}
$update_content = new validator;
$required = "page_name, take_action, content";
$passed = $update_content->validate_fields($required);
if (!$passed) {
echo $update_content->error;
exit;
}
else {
//change all carriage returns to <br />.
$content = eregi_replace("(\r\n|\n|\r)", "<br />", $content);
$content_nl = $n.$content; // add an extra carriage return to the beginning of the line in the file. This will make the file easier to read.
}
//begin add new content script
$fp = fopen ("$DOCUMENT_ROOT/testing/$text_file", $take_action, 1);
if (!$fp){
//error if there is a problem opening the text file
echo '<p>The server was unable to complete your open request at this time. Please contact your <a href="mailto:joshual@cox.net?subject=Problems opening messages file">administrator</a>.<br />';
}
else {
if (!fwrite ($fp, $content_nl, 2048)){
//error if there is a problem writing to the text file
echo '<p>I am sorry but we are unable to complete your write request at this time. Please contact your <a href="mailto:joshual@cox.net?subject=Problems adding post to the messages">administrator</a>.<br />';
}
else {
//report to user the action that has been taken
echo $took_action.' with<br />'.$content.'<br />';
}
fclose($fp);
}
//debugging code
if ($debug == 'Y'){
$debug_info =
'fp = '.$fp.'\n\r'
.'date = '.$date.'\n\r'
.'page_name = '.$page_name.'\n\r'
.'take_action = '.$take_action.'\n\r'
.'took_action = '.$took_action.'\n\r'
.'search = '.$search.'\n\r'
.'replace = '.$replace.'\n\r'
.'content = '.$content.'\n\r'
.'text_file = '.$text_file.'\n\r';
echo 'fp = '.$fp.'<br />';
echo 'date = '.$date.'<br />';
echo 'page_name = '.$page_name.'<br />';
echo 'take_action = '.$take_action.'<br />';
echo 'took_action = '.$took_action.'<br />';
echo 'search = '.$search.'<br />';
echo 'replace = '.$replace.'<br />';
echo 'content = '.$content.'<br />';
echo 'text_file = '.$text_file.'<br />';
$errorlog = fopen ('/errorlog.txt', 'w', 1);
fwrite ($errorlog, $debug_info, 3070);
fclose ($errorlog);
}
?>