The backslashes come from an old PHP setting, magic quotes, and scripts that escape text again. Make each script escape text itself, only when saving to the database, then turn the setting off and clean stored text once.

A backslash in every message

An enquiry arrives from the website's contact form. The sender's surname, O'Neill, has become O\'Neill, and every apostrophe in the message carries a backslash in front of it.

Pages edited through the site's own admin screens are worse. A heading saved once shows one backslash before its apostrophe. Edit it again and there are three; once more, seven.

Retyping the text removes them until the next save. The cause is not in the text at all, but in the way the server and the site's scripts handle it.

An old setting meant to protect databases

Older versions of PHP, the language behind many websites, have a setting called magic quotes. When it is on, the server adds a backslash before every apostrophe, quotation mark and backslash typed into a form.

The idea was to make text safe to place inside a database query, where a stray apostrophe can end a piece of text early and let the rest be read as commands.

It was a blunt tool. It changes everything typed into every form, whether or not it is headed for a database, and it leaves the clean-up to each script.

How the slashes pile up

The fourth explains the growth. Each save escapes the backslash already there as well as the apostrophe, so one becomes three, and three become seven.

The fifth needs no database at all, which is why a site with nothing but a contact form can show the same symptom.

What the mail script misses

Form emails are the simplest case. The mail script takes whatever the form sent and drops it into a message.

If the server has added backslashes and the script never strips them, they go straight into the inbox, on every enquiry, for as long as the site runs.

Platforms that add slashes themselves

WordPress adds the same slashes to form data on its own, whatever the server setting, so that older add-ons written with magic quotes in mind keep working.

A form add-on or theme that forgets to remove them shows the familiar backslashes even where the setting is off. There, the fix belongs in the add-on, not the server.

Why the setting cannot simply be switched off

Turning magic quotes off stops the slashes, but it also removes the only protection some old scripts ever had.

A script that relied on it, and never escaped text itself, is then open to attack.

An attacker types an apostrophe followed by database commands into a form, and the script runs them. Automated tools try this attack, known as SQL injection, on forms all over the web.

PHP 5.4 and later have no magic quotes at all, so the same exposure arrives when a host upgrades PHP. The scripts have to be made safe first, whichever way the change comes.

A worked example

A sign painter in Nelson letters shop windows, trucks and carved wooden signs, and gets most new work through a quote form that asks for the wording, the size and where the sign will go.

The wording arrived with a backslash before each apostrophe, and sizes in inches arrived with one before each inch mark. Twice she nearly lettered a backslash onto a customer's window.

The captions in her gallery, edited through a small admin page built years earlier, had grown to three or seven backslashes each. Her host still ran an older PHP with the setting on.

The admin page also escaped everything again before saving. A developer rewrote it to save through prepared statements, which keep text and query apart, and changed the quote form to send exactly what the customer typed.

Only then was the setting turned off for her site. The captions were cleaned once, after a backup, and the form was tested with wording full of apostrophes, inch marks and a stray backslash.

What a customer types is now what reaches her inbox, and a caption saved ten times comes back exactly as written.

Cleaning up what is already saved

Fixing the cause does not repair text saved while the fault was active. Pages, captions, product descriptions and customer records saved through the faulty script may all hold extra backslashes.

Clean them once, with a careful script run against a copy of the database before the live one.

Removing every backslash blindly can damage text that genuinely needs one, so look at what the script changes before trusting it.

Testing with awkward text

Keep a test sentence with an apostrophe, a pair of quotation marks, an inch mark and a backslash. Send it through every form and read the email that arrives.

Then save it into a page, open the page for editing, save it again without changes, and check that nothing has grown.

Run the same test after any change to the server or the scripts, including the host's PHP upgrades.

The counter-case

A site whose only form sends email, and which saves nothing, has an easier path. Removing the slashes inside the mail script fixes the symptom completely, and there is no stored text to clean.

And on a server already running PHP 5.4 or later, magic quotes cannot be the cause. The slashes there come from a platform or a script, which narrows the search.

Wherever a site saves form text, though, its scripts need to escape that text themselves, correctly and once. That protects the database whether the old setting is on, off or gone.

Fixing it at the source

  1. Send a test message with an apostrophe, quotation marks and a backslash.
  2. Ask the host which PHP version runs the site and whether magic quotes is on.
  3. List every script that saves form text or sends it by email.
  4. Make each script escape text itself, only where it saves to the database.
  5. Turn magic quotes off once the scripts are safe without it.
  6. Clean the stored text once, after a backup.
  7. Save a page twice and check that nothing grows.

Step four has to come before step five. Switch the setting off first, and every old script that leaned on it is open to the very attacks it was meant to stop.

If form messages stop reaching the office altogether, rather than arriving with extra slashes, one common cause is explained in enquiry emails that never leave the web server.


Frequently asked questions

Why do form emails show backslashes before apostrophes?

The server adds them to everything typed into a form, through an old PHP setting called magic quotes, and the mail script never removes them before sending.

Why does the number of backslashes keep growing?

Each save escapes the backslash already in the text as well as the apostrophe, so one becomes three and three become seven after a few edits.

Can we just turn magic quotes off?

Only after the scripts escape text themselves. A script that relied on the setting becomes open to database attacks the moment it is switched off.

Will a PHP upgrade fix it?

PHP 5.4 and later have no magic quotes, so those slashes stop, but any script that relied on the setting loses its protection at the same moment.

Why does a WordPress site show them with the setting off?

WordPress adds slashes to form data itself, for the sake of older add-ons. An add-on or theme that forgets to remove them shows backslashes on any server.

What about text already saved with backslashes?

Fix the cause first, then clean the stored text once with a careful script, tried on a copy of the database before the live one.

West Coast Media Solutions Inc. provides web design, web development, hosting, digital marketing, and business consulting to organisations across Canada, drawing on more than twenty-five years in the field.

Backslashes turning up in your form emails?

Send us a form email that shows them, and we will trace where the slashes are added and fix the scripts safely.

Start a Conversation