Jump to content

htmlentities() htmlspecialchars() question. (yes, I read the manual)


frijole

Recommended Posts

both of these functions encode the HTML tags, but, I don't understand what the purpose is? you encode them, and them when you use them again just decode them? I would appreciate if someone could give me some insight into this, or maybe just an example. thanks.

Link to comment
Share on other sites

For example, if i had a form..

 

If someone entered ">Script Error

<input name="form" type="text" value="<?php echo $name; ?>" />

My form would break and script error would be on the website.

However

<input name="form" type="text" value="<?php echo htmlentities($name); ?>" />

The script error would be still inside the form and it wouldnt break.

 

Link to comment
Share on other sites

as far as i know there is no "decode_htmlentities()". the purposes of the functions is to ensure that plain text characters like greater than, less than, etc. don't screw up the form presentation when they are used in a form element. as mentioned, if you echo a less than sign in a form element, chances are good that it will screw up the form element. i think the worst are single and double quotes.

Link to comment
Share on other sites

You don't have to always encode it. You encode it when you want to output user input.

Example- this very forum- our messages may contain html, but obviously we don't want the html we write in our messages to be interpreted as html, we simply want other to see exactly what's written. So if I write </html> our browsers won't think it's part of the html code, because SMF uses htmlentites() on the posts (check the source if you want to, you'll see < and >)

 

Orio

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.