Hey All,

I've got a web page that displays multiple boxes of text using php. The text are coming from .txt files instead of using a database to store the data. I can update these text files when I want to change the information.

My problem is that I need stripslashes to be put in because I'm getting (don\'t) instead of (don't), and I can't figure out where to put the stripslashes function to make it work. This only happens when I use my update form. If I alter the .txt document manually, no problem, so my problem must lie in my update.php file.

Here is my Update.php file so you can see what I've done.

<?php
if($_POST['Submit']){
$open = fopen("b12.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />"; 
echo "File:<br />";
$file = file("b12.txt");
foreach($file as $text) {
echo $text."<br />";
}
echo "<p><a href=\"./index_update.php\">click here to return to main page</a></p>"; 
}else{
$file = file("b12.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"20\">";
foreach($file as $text) {
echo $text;
} 
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>

if anyone can point me in the right direction it would be great. I'm still new to this but learning all the time.

Thanks.

Member Avatar for diafol

The $_POST array variable may be slashed. To get rid of the slashes, just do this:

$text = stripslashes($_POST['update']);
$text = stripslashes($_POST['update']);

Sorry Ardav, in between reading the post and answering

Thank you for the reply, but it didn't work, i'm still getting the \ before the apostrophy. I'll explain a little more. My main page (index.php) uses include statements within html:

<?php 
	  include('b12.txt');
?>

I then have an update button that I click which takes me through a login page to an "update_index.php" page. This page has edit links for each of the 12 header and body .txt files. When I click the edit link, it opens the .txt file in a file called update.php (displayed above) where the text is changed. I think this is where my problem is but i'm not sure now that your instruction didn't work.

I didn't have this problem until I had to uncomment the magic_quotes_gpc = on in my php.ini file for another program.
Do you think my problem lies elsewhere or is the file above my problem? Ideas?

test Just the update file
on your local server run the update.php against a blank(empty) b12.txt and examine the stored data,
then against an edit of the edited file
using the modified $text = stripslashes($_POST['update']);$text = stripslashes($_POST['update']); if that works the bug is elsewhere,

it should work
and the bug should be elsewhere

Member Avatar for diafol

magic_quotes_gpc should be off. Ych-a-fi mochandra diawl!!! Removed in pHp 6.0 anyway. All your text should be escaped and slashed properly. Addslashing and Stripslashing should be under your control, not left to some hocus pocus invisible function!

What'll happen when our hosts upgrade to php6? Will the magic go 'poof'?

Even pHp's online manual says not to rely on it.

test Just the update file
on your local server run the update.php against a blank(empty) b12.txt and examine the stored data,
then against an edit of the edited file
using the modified $text = stripslashes($_POST['update']);$text = stripslashes($_POST['update']); if that works the bug is elsewhere,

it should work
and the bug should be elsewhere

If I understand you correctly, i'm to empty the b12.txt file, then add data and see what happens? When I empty the file, it displays nothing as it should. When I add data, I use simply the word "don't", and it comes out don\'t.

I'm not sure where else the problem could be, the page that displays it is a simple include statement (in this case

include('b12.txt');

. The update.php file is writing the \ to the txt file. I can edit the b12.txt file directly, remove the \ and save it, and it displays like it should. I must be doing something wrong, I just can't figure out what.

magic_quotes_gpc should be off. Ych-a-fi mochandra diawl!!! Removed in pHp 6.0 anyway. All your text should be escaped and slashed properly. Addslashing and Stripslashing should be under your control, not left to some hocus pocus invisible function!

What'll happen when our hosts upgrade to php6? Will the magic go 'poof'?

Even pHp's online manual says not to rely on it.

I agree, but I've got to go through the other program i'm running and get that fixed because it was not slashed properly from the beginning. But in the mean time I just want to get this other portion right until I can take the time to fix the other one. Fortunately I'm doing my own hosting so a php upgrade will not be coming anytime soon. So much for small blessings :)

Thank you almostbob and ardav for your help. It appears that your fix did work, but required a reboot on my part. Go figure. But the problem is fixed. I can now work on getting the slashes corrected in the other program, time willing.

Much appreciated guys!!!!!

It were Gremlins
Gremlins wot dunnit

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.