8

I have to create a login in form which a user will insert a username and a password. I have to ensure that html entities are not processed, and I can not allow single quotes or double quotes to be processed either. I must echo the data entered into the form and display it.

I must use htmlentities and str_replace. I have the htmlentities correct, but am unsure on how to utilize the str_replace function to replace the single and double quotes that the user might enter into the form. Any help would be awesome.

Here is my current PHP (which works)

<?php
$username = htmlspecialchars($_POST['username']);
$password    = htmlspecialchars($_POST['password']);
$comment = htmlspecialchars($_POST['comment']);
?>
<html>
<body>
Your username is: <?php echo $username; ?><br />
Your password: <?php echo $password; ?><br />
Your Comment was: <?php echo $comment; ?>

Techie
  • 44,706
  • 42
  • 157
  • 243
Jeremy Flaugher
  • 417
  • 2
  • 8
  • 15
  • 2
    What's wrong with the quotes ? It will do nothing harmful according to your code. (You are not using a database. PHP can handle slashes automatically) – AKS Feb 07 '13 at 05:01
  • That is what the instructor wants. He wants to make sure we know how to the str_function. So far it has only made my head hurt ( I am very new to PHP) – Jeremy Flaugher Feb 07 '13 at 05:03

7 Answers7

22

First of all, it is always better to check for the unwanted characters and get back to the user, than silently stripping them. Say, a user added a quote to their password, you removed it, and so they won't be able to login at all! So it's better to check and tell the user instead:

if (!ctype_alnum($username)) { 
    $errors[] = "Only letters and numbers allowed in username";
}
...
if ($errors) {
    echo "You've got some errors, please fix them: ". implode("<br>", $errors);
} else {
    // proceed with normal flow

But in this specific case I would advise against replacing any characters. Imagine Shaquille "Shaq" O'Neal is going to register on your site. What's the point in stripping him of all those quotes? Let alone the password, where use of punctuation is strongly encouraged.

After all, those quotes don't do any harm, if you properly handle them.

  • for HTML, simply use htmlspecialchars() with ENT_QUOTES attribute:

      Your username is: <?= htmlspecialchars($username, ENT_QUOTES) ?><br />
    
  • for SQL, use prepared statements to avoid any problems with quotes

But just for sake of literal answer, here is how to remove quotes (or any other characters and even multi-character substrings) in PHP

$yourVariable = "scor\"pi'on";
$substringsToRemove = ['\'', '"'];
$yourVariable = str_replace($substringsToRemove, "", $yourVariable);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
ScoRpion
  • 11,364
  • 24
  • 66
  • 89
4
 $keyItem = str_replace ("'","\'",$keyItem);

This replaces a single quote with an 'escaped' single quote \' .

user462990
  • 5,472
  • 3
  • 33
  • 35
3
$username = htmlentities(str_replace(array('"', "'"), '', $_POST['username']));
Barmar
  • 741,623
  • 53
  • 500
  • 612
1
$username = str_replace(array("'", "\""), "", htmlspecialchars($_POST['username']));
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
0

Try the below code to replace double quotes

str_replace(chr(34), "replace_with", $content);

For single quotes

str_replace("'", "replace_with", $content);
Techie
  • 44,706
  • 42
  • 157
  • 243
0

To delete quotes, you can use this:

$search = 'cars "black" in ...';
$search = str_replace("\"", "", $search);
// result: "cars black in ..."
Emil
  • 7,220
  • 17
  • 76
  • 135
-1
$username_test = $_POST['username'];
$username = str_replace(array("'", "\"", "&quot;"), "",htmlspecialchars($username_test) );


//same for password :) 
Cleb
  • 25,102
  • 20
  • 116
  • 151
rahul
  • 59
  • 1
  • 1
  • 1
    Could you add some details of why this should work? Code-only answers are not very useful and are subject to deletion. – Cristik Nov 04 '15 at 14:14
  • In this example we will search username.Lets suppose it is cris'tik.Now it would find the quote(') in the username and replace it with space.Answer was relevant to question so I didn't thought explanation is needed. – rahul Nov 04 '15 at 19:18
  • 1
    even if the asker wanted only the code (doesn't look to be the case here though), and for him your answer would be enough, one of the qualities of SO is the fact that answerers also try to educate people instead of giving them copy&paste answers. This is why an answer explaining the solution is received much better and gets more votes than other ones that don't. – Cristik Nov 04 '15 at 19:50