phrants.net
14Jan/110

Stripping MS Word Garbage from HTML form

I recently noticed that my mysql database was filling up with a lot of strange junk code, like StartFragment and <w:WordDocument>, and other miscellaneous xml garbage. I quickly discovered that the problem stemmed from some users copying and pasting text from Microsoft Word into the HTML form editor. This was creating havoc in the database, as frequently the tags would be broken, and then formatting would suffer elsewhere. I couldn't find any decent solutions online, so I created my own, albeit imperfect, solution.

Step 1 : Recognize problematic code in the string-- assign variables for these code fragments.

$bad_fragment = "<w:";
$bad_fragment2 = "StartFragment";

Step 2: Check to see if the form submitted contains either bad code fragment.

$contains_junk_code = strpos($submitted_form, $bad_fragment);
$contains_junk_code2 = strpos($submitted_form, $bad_fragment2);

Step 3:  If the bad code was found, go ahead and strip out ALL formatting.

$submitted_form = strip_tags($submitted_form);

Of course, you could just always strip the tags off every single string formatted using the above function, but I want to keep HTML formatting if it's not problematic. If the bad MS Word xml code fragments are found, they are stripped and I alert the user.

echo "Your submission has been saved as text only. This occurs when copying and pasting from MS Word. To add formatting, edit the text by clicking below.";

While this solution is not perfect, it was the most simple and straightforward approach I could think of for my web app. Hopefully it will help you or inspire you to create a better fix.

$bad_tag = "w:";
$bad_tag2 = "!--";
$bad_tag3 = "StartFragment";
4Fév/100

Apostrophe PHP problem – Putting an ‘ into a mySQL database

You may notice that when you are putting data into your mySQL database from PHP, somehow a magical back slash appears from nowhere. This happens basically because PHP doesn't want anyone inserting malicious code or SQL injections into your database. The slash is there to protect us from our own coding. It appears from nowhere, and it is aptly named "Magic Quotes".  There are a couple of places online that explain how to address this, but it still took me a good few hours to really understand what I needed to do.

.

The first step for me was to get the input from the HTML form that was submitted:

$name=$_POST['name'];

Now, your code checks to see if Magic Quotes is on in your server's version of PHP
If it is, it will remove any of those extra slashes in names like O'Brian which was O\'Brian

if(get_magic_quotes_gpc())
{
$name = stripslashes($name);
}

Now you can use $name as it is in your code to display without the annoying backslash. To make it safe for your database, however, you need one final step. This last bit prepares your variable to be safely put in your mySQL database:

$name = mysql_real_escape_string($name);

I'm still a relative newbie at this stuff and wasn't able to find the above few lines of code just how I needed them. Hopefully they will be of service to you as you learn PHP

Taggé comme: , Aucun commentaire
2Fév/100

PHP – Global Variables / Passing Variables to another page

I was at a total loss when trying to set a variable that I could use from one PHP page to another. The POST and GET functions worked to a point for the basic stuff, but I was quickly outgrowing the introductory course. I wanted to do something like an #include for each page, or to set a global variable that each individual page could access. It was extremely difficult to realize, but I finally figured out that what I wanted to do was to set a Session variable. As long as the session variable is set, you can use it at any time from any of your other PHP pages. This turned out to be incredibly easy:

session_start(); //this goes at the top of each page - it either creates or maintains a session with the server

$_SESSION['user_name']; //creates a session variable and can be assigned to a regular variable

$user_name = $_SESSION['user_name']; //now the 'local' variable is assigned from the 'global'

Session variables are amazing, you can just call on them from out of the blue and they will be there as long as the session is open. Sessions do expire after a prolonged period of time or if you close the browser or call on a PHP function to destroy it.  I had a great "Aha!" moment once I realized that I could assign a variable in one file and easily use it in any other PHP file. I guess this is what happens when you teach yourself PHP from online tutorials.

Taggé comme: , Aucun commentaire
2Fév/100

#1062 – Duplicate entry ‘1’ for key 1

This error kept popping up on me in myPhpAdmin while working with a mysql database. It was driving me up the wall as I didn't want to re-create the table just to assign a primary key and set auto-increment in an empty table. The first problem I noticed was that there was more than one "1" in the table. Makes sense, obviously you can't set the primary key in a column where there are duplicate entries. The second piece of the puzzle, however, was much more difficult to identify. It turns out that myPhpAdmin won't allow you to set auto_increment to a column where there is a "0" (zero) as unique key. I changed the zero to a 999 and then was able to set the auto increment. Then I went back in and manually changed the 999 back to zero. I couldn't find this solution anywhere else online, so I thought I would share it here.