I have a php page which contains a form.
Sometimes this page is submitted to itself (like when pics are uploaded).
I wouldn't want users to have to fill in every field again and again, so I use this as a value of a text-input inside the form:
value="<?php echo htmlentities(@$_POST['annonsera_headline'],ENT_COMPAT,'UTF-8');?>">
This works, except it adds a "\" sign before every double-quote...
For instance writing 19" wheels gives after page is submitted to itself:
19\" wheels
And if I don't even use htmlentities then everything after the quotes dissappears.
What is the problem here?
UPDATE:
Okay, so the prob is magic_quotes... This is enabled on my server...
Should I disable it? I have root access and it is my server :)
Whats the harm in disabling it?
Looks like you have magic quotes turned on. Use below condition using
stripslashes
with whatever text you want to process:Now you can process
$your_text
variable normally.Update:
Magic quotes are exaplained here. For well written code there is normally no harm in disabling it.
Yes, you should disable magic quotes if you can. The feature is deprecated, and will likely go away completely in the future. If you've relied on magic quotes for escaping data (for instance when inserting it into a database) you will may be opening yourself up to sql injection vulnerabilities if you disable it. You should check all your queries and make sure you're using
mysql_real_escape_string()
.I include the following file to undo magic quotes in apps that are deployed to servers not under my control.
Looks like your server is setup to use Magic Quotes.
You can fix it by stripping them with
stripslashes
, or better, by turning off Magic Quotes.You likely have magic quotes turned on. You need to
stripslashes()
it as well.Nicest way would be to wrap this in a function:
Which you can use as
By the way:
It's actually still there in the HTML source, you only don't see it. Do a View Source ;)
Update as per your update: the magic quotes is there to prevent SQL injection attacks in code of beginners. You see this often in 3rd party hosts. If you really know what you're doing in the code, then you can safely turn it off. But if you want to make your code distributable, then you'll really take this into account when gathering request parameters. For this the above function example is perfectly suitable (you only need to write simliar
get_boolean()
,get_number()
,get_array()
functions yourself).This is actually a function of PHP trying to be security conscious, luckily there is an easy fix for it that looks something like this:
There isn't a huge problem in having it enabled, it comes down to personal preference. If you code will be moving servers much and you can't disable it through your php.ini file, it's best to use something as described above.
If you have access to your php.ini file and you want to change it, because you don't want to have to validate it each time you can add the following line to php.ini
Or the following to your .htaccess:
Hope this helps clear things up.