Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
problem with writing format of a string to a txt file
Old 03-08-2009, 02:05 PM problem with writing format of a string to a txt file
Experienced Talker

Posts: 38
Trades: 0
Hi,

I'm having problem with a php script writing a string to a file in a correct format.

Well, no problem with plain text. the problem comes when i type

'<a href="http://google.com">hotmail.com</a>'

when i look at the txt file, the output will be

<a href=\"google.com\">hotmail.com</a>

if you notice, there is a slash before the quote, automatically added. I'm not sure where the script goes wrong. appreciate if you can please help me out, thanks.

PHP Code:
<?php
// Function to display form
function showForm($errorMesg=false){
   if (
$errorMesg)  $errorTextMesg  "Please enter your text!";
   echo 
'<form action="notepad.php" method="POST"><center><p><br><br><table border="1" cellpadding="10" cellspacing="0">';
    
// Display message field an error if needed
   
echo '<tr><td>Entry:</td><td><textarea name="mesg" rows="10" cols="60"></textarea></td></tr>';
   if (
$errorMesg) echo "<tr><td colspan='2'>$errorTextMesg</td></tr>";
   echo 
'<tr><td align="center" colspan="2"><input type="submit" name="updateNotepad" value="Submit"></td></tr>';
   echo 
'<form>';
}
if (!isset(
$_POST['updateNotepad'])) {
   
showForm();
} else {
   
//Init error variables
   
$errorMesg  false;
   
$filename  "notepad.txt";
   
// $mesg  = isset($_POST['mesg'])  ? trim($_POST['mesg'])  : '';
   
$Cmesg  = isset($_POST['mesg']);
   
$mesg  $_POST['mesg'];
   if (
strlen($mesg)<3$errorMesg true;
   
// Display the form again as there was an error
   
if ($errorMesg) {
      
showForm($errorMesg);
   } else {
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
    if (!
$handle fopen($filename'a')) {
         echo 
"Cannot open file ($filename)";
         echo 
'<form><input type="button" value="Back" onclick="history.go(-1);return false;" /></form>';
         exit;
    }
    
// Write $mesg to our opened file.
    
if (fwrite($handle"$mesg\n") === FALSE) {
        echo 
"Cannot write to file ($filename)";
        echo 
'<form><input type="button" value="Back" onclick="history.go(-1);return false;" /></form>';
        exit;
    }
    echo 
"Success, wrote ($mesg) to file ($filename)";
    
fclose($handle);
    echo 
'<meta http-equiv="refresh" content="5;url=notepad.php">'
} else {
    echo 
"The file $filename is not writable";
    echo 
'<form><input type="button" value="Back" onclick="history.go(-1);return false;" /></form>';
}
}
}
?>

Last edited by superkingkong; 03-08-2009 at 02:17 PM..
superkingkong is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-08-2009, 02:12 PM Re: problem with writing format of a string to a txt file
Decaf's Avatar
Ultra Talker

Posts: 489
Name: Adam
Trades: 0
You should avoid double quotes inside of single quotes. Or use the \" to display double quotes.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Decaf is offline
Reply With Quote
View Public Profile Visit Decaf's homepage!
 
Old 03-08-2009, 02:43 PM Re: problem with writing format of a string to a txt file
Experienced Talker

Posts: 38
Trades: 0
anyway to find "double quote" in $mesg and maybe to filter it out or like you said, add \" to it?

thanks.
superkingkong is offline
Reply With Quote
View Public Profile
 
Old 03-08-2009, 05:42 PM Re: problem with writing format of a string to a txt file
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Maybe replace this
PHP Code:
if (fwrite($handle"$mesg\n") === FALSE) { 
with
PHP Code:
if (fwrite($handle$mesg."\n") === FALSE) { 
or even
PHP Code:
if (fwrite($handlestripslashes($mesg)."\n") === FALSE) { 
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 03-09-2009, 01:49 AM Re: problem with writing format of a string to a txt file
dark_lord's Avatar
Experienced Talker

Posts: 41
Name: Parijat Roy
Location: INDIA-KOLKATA
Trades: 0
it's happening because of the PHP magic codes check is on. And it should be On to avoid security problems. What you can do is to use str_replace before writing those to file.
__________________
I AM THE BEAUTIFUL NIGHTMARE

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
dark_lord is offline
Reply With Quote
View Public Profile Visit dark_lord's homepage!
 
Old 03-09-2009, 03:16 AM Re: problem with writing format of a string to a txt file
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by dark_lord View Post
it's happening because of the PHP magic codes check is on. And it should be On to avoid security problems. What you can do is to use str_replace before writing those to file.
Magic quotes helps to protect beginners from sql injections. Relying on it however, is not a great idea, which is part of the reason it is being removed in PHP6.

Using stripslashes as Insensus pointed out should fix the problem. Turning off magic quotes would also do the trick.

http://us.php.net/manual/en/security.magicquotes.php
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to problem with writing format of a string to a txt file
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29363 seconds with 12 queries