i have writte a small news manager script that runs on php 4.2.1
I have a small problable when writing the text from a form to a array
that is then written to a flat file.
I first change the "\r\n" to <br> before saving and from <br> back to
"\r\n" when loading. This works okay but when i save a text that contains
for instance 'word' then it gets written as \'word'\ in the flat file and is
off course displayed as such.
What is causing this and how can i solve this?
Relevant code snippets:
==== code from the manager ================
if ( $action == "add" )
{
// save form data
if ( $news_title != "" && $news_short != "" )
{
$rawline =
$news_title."|".$news_short."|".$news_long."|".$news_type."|".$news_status;
$news->AddItem($rawline, $news_id);
// empty vars again
$news_id = "";
$news_title = "";
$news_short = "";
$news_long = "";
$news_type = "";
$news_status= "";
$action = "add";
header("location: newsmanager.php");
}
}
==== code from the manager ================
The form is on this php and after the news items are filled in, i redirect
to the same
page with an url var set (action ==add). Then i add the line with the
function
AddItem to a var $news, which is an array that i've put in the session.
==== code from class News ================
class News
{
var $news_items; // array that is put in the session to save the newsitems
...
function AddItem ($rawline, $in_id)
{
// set \n as <br>
$rawline = ereg_replace ( "\r\n", "<br>", $rawline);
// new element being added
if ( $in_id == "" )
{
$id = $this->MakeId(); // just gets the date as long
$rawline = $id."|".$rawline."\r\n";
// add item to array
array_push($this->news_items, $rawline);
// add item to file
$fp = fopen($this->news_file, "a");
fwrite($fp, $rawline );
fclose ($fp);
}
...
==== code from class News ================
So where is the 'word' changed into \'word'\?
Thanks
> for instance 'word' then it gets written as \'word'\ in the flat file and is
> off course displayed as such.
> What is causing this and how can i solve this?
This is most likely because you have magic_quotes_gpc (gpc is short for
get/post/cookie) turned on in your php.ini. magic_quotes_gpc
automatically escapes single quotes (among other characters, see
<http://www.php.net/manual/en/ref.info.php#ini.magic-quotes-gpc>).
If you want to turn it off, you have to do it either in the php.ini or
in an .htaccess, if you have proper access. If you turn it off, don't
forget you will need to either turn it on or run addslashes() (or the
relevant database dependant escape functions) if you decide to enter gpc
data into a database.
If you don't have access to either the php.ini or an .htaccess, you may
have to run stripslashes() on the gpc data yourself.
HTH,
--
steven vasilogianis
Steven,
Indeed, i found "magic_quotes_gpc=On" in my php.ini
I don't have access to the php.ini of my site online ( i get the same result
there so i guess that magic_quotes are also on).
I 'll have to use stripslashes() then. I'll give it a try.
Thanks for your fast answer.