Λυπάμαι, εγώ έπρεπε να διαφοροποιηθεί όταν είπα να χρησιμοποιείτε πάντα htmlentities. Όταν ασχολούμαστε με την είσοδο του χρήστη, θα πρέπει να κάνει τη διαφορά ανάμεσα σε 2 είδη της εισόδου του χρήστη: είσοδος χρήστη όπου HTML δεν επιτρέπεται, και την είσοδο του χρήστη, όπου HTML δεν είναι επιτρέπονται. Για παράδειγμα, το κείμενο που έχουμε γράψει σε αυτό το σύστημα είναι το είδος όπου δεν επιτρέπεται HTML (που είναι ο λόγος για τον οποίο χρησιμοποιεί ετικέτες BBCode αντί), και αυτό είναι το είδος της εισόδου θα βρείτε το 99% του χρόνου. Στην πραγματικότητα, σχεδόν το μόνο μέρος που θα βρείτε συνήθως εισόδου του χρήστη, όπου HTML δεν επιτρέπεται είναι WYSIWYG συντάκτες σε CMS'es. Για την είσοδο του χρήστη, όπου HTML δεν επιτρέπεται, είναι πολύ απλό: εφαρμόζει πάντα htmlentities πριν επαναλαμβάνοντας τίποτα. Ο λόγος για αυτό είναι απλός: ας υποθέσουμε ότι έχετε μια ιστοσελίδα όπου μπορείτε να συνδεθείτε με ένα cookie. Αν είχε αποτύχει να εφαρμόσει htmlentities, ένας κακόβουλος χρήστης θα μπορούσε να τεθεί κάποιο javascript κώδικα που διαβάζει το cookie και το στέλνει σ 'αυτόν, με τον τρόπο αυτό ουσιαστικά που να του επιτρέπει να συνδεθείτε και όποιος έχει δει τη σελίδα όπου εμφανίζεται κωδικό του Javascript (και εκτελείται, επειδή του είσοδος ήταν απλά εισάγεται στον κώδικα HTML). Αυτό είναι γνωστό ως XSS εκμεταλλεύονται? ότι είναι αρκετά κοινό αυτές τις μέρες και οι άνθρωποι (ή bots) θα προσπαθήσουμε για το site σας. Για την είσοδο του χρήστη, όπου επιτρέπεται HTML, θα πρέπει να σκεφτούμε αν εμπιστεύεστε τους χρήστες σας για να εισέλθετε μη κακόβουλο εισόδου ή όχι. Δεδομένου ότι αυτό το είδος της εισόδου βρίσκεται συνήθως σε ένα CMS το οποίο μόνο οι ιδιοκτήτες της ιστοσελίδας μπορεί να έχει πρόσβαση, μπορείτε συνήθως εξόδου η HTML ως έχει, χωρίς την εφαρμογή οποιασδήποτε διαφυγή (η οποία είναι η αναμενόμενη συμπεριφορά από ένα πρόγραμμα επεξεργασίας WYSIWYG). Το πραγματικό πρόβλημα έρχεται όταν θα πρέπει να επιτρέψει ενδεχομένως σε κακόβουλους χρήστες να εισέλθουν εισόδου HTML. Αυτό είναι όπου θα πρέπει να αφαιρέσει το χέρι οποιαδήποτε πιθανώς κακόβουλα πράγματα (όπως
...
tags, etc) before displaying it. The problem with this approach is that this can get very complex to do reliably; most browsers allow for such a high degree of malformed input that it has proven to be virtually impossible for you to take all of it into account, and there have been countless examples of savvy people bypassing the input stripping by making it malformed, but in such a way that the browsers can still understand it.
That is why, to avoid this entire hassle but to still allow for some basic HTML capabilities for the grand public, alternatives like BBCode and UBB are frequently used. Major advantage here is that you control explicitly which tags can be used and which can't, and that tags that are not allowed simply won't be parsed. Since this approach replaces the usual HTML tags (which are parsed client-side) with equivalent BBCode tags (which are parsed server-side), you'll still want to apply htmlentities though - otherwise users could still use regular HTML tags. Writing a BBCode or UBB parser can be more complicated than it looks at first sight, so I'd advise you to use some of the excellent existing libraries out there rather than write your own.
As for htmlentities vs htmlspecialchars, the difference is that htmlentities escapes more characters than htmlspecialchars. For the purposes of escaping user input htmlspecialchars would work fine too, but why not escape more?
For your
problem, here's what you do: the user enters his input in a standard . This textarea contains his input with the lines separated by a newline character (
). You simply take the input from the textarea, and insert it into the DB as-is. Then, when displaying it again, you apply htmlentities to it, and then run it through nl2br - this will convert the newlines to
tags. Otherwise the newlines would only show up in the HTML source, and not on the rendered page. Note that you first apply htmlentities and then nl2br; if you had done it the other way around, the
's would have escaped by htmlentities.
When you want to enable the user to edit his input again, you apply htmlentities and echo it back between and . Here, you don't have to apply nl2br - the textarea control picks up on the newlines in the source code. Also, the entities escaped by htmlentities will be un-escaped again by your browser before sending the form data; this way you'll keep their original input instead of saving the escaped entities in the DB.
Μεταφράζονται, παρακαλώ περιμένετε..
