![]() |
Quiz Answer Help: Apostrophe Issue
I created a quiz to help me study for an exam I have coming up. It matches the answer with the correct answer to see if I got the answer right or not. This works for most of the questions, but if the answer has an ' in it it will display the answer as \' instead, and they will not match. I'm not sure why this is happening, though I think it's something pretty simple that I haven't noticed.
PHP Code:
|
Is the \' coming from a string entered by the user on a form, or from the selection called on the database?
Also, do you have a formatted version of this? Its pretty hard to follow when there's no indentation :P |
The \' Is coming from the radio button after it is chosen. It will display as ' in the question page, and when I call the database to check the answer it displays as '. The question page displays fine, but when I click "calculate results" when it displays the results page it gets switched over to the \' so the check doesn't work as they are now different strings.
Sorry no better formatted version than this one. |
This is caused by an ini directive called magic_quotes_gpc. It was one of the worst ideas they ever came up with, and fortunately the functionality is now gone as of 5.4.0.
Still, until you program specifically for the 5.4+, you must take care to remove them. If the post is simple, you can cheat it by simply mapping the array instead of walking it (if you were making a larger system, I'd suggest walking all the globals instead). PHP Code:
PHP Code:
|
Hmm I threw in the changes you mentioned and it's still giving me:
Quote:
|
I used eclipse to format it. It's different than I like, but it doesn't seem to be capable of linefeeding the braces in the PHP editor. Not sure why; pretty sure it does that with the java editor.
I think I'm confused by exactly what the branches are doing here. No matter, remove the if branch for the magic quotes completely, and above the isset($_POST['number']) check simply do it there: PHP Code:
|
If it is useful I use the following code, which handles post-arrays:
PHP Code:
|
Hmm well I've come to understand what you're telling me. Is it's basically just taking the backslash out when magic quotes are present? So basically I'd be comparing ' to ' which still wouldn't match. But now without the backslash I can use preg_replace to change ' to '. I still don't understand why this is an issue if I have PHP 5.2 with magic quotes off selected, shouldn't they be off then? Maybe something to take up with my host? Also why does POST change ' to '?
EDIT: I also tried switching to PHP 5.4 to fix the problem. It was still turning the ' to ', I figure this has something to do with post? Only thing I noticed with the change was it took away the backslash that occurred. As it caused errors in other pages I reverted back for now, but will upgrade as soon as I get a chance. |
No you want to make sure you are comparing ' to '. When you take input from a form with magic_quotes_gpc enabled, this escapes the ' to become \'. You stripslash it so it removes the escape from the string. Since PHP isn't sensitive to using the addslashes (implicitly from the magic_quotes) and the mysql_real_escape_string, it would definitely corrupt the data when inserting to a database. Likewise, since you are not comparing using the SQL query itself, you need to make sure the state of the apostrophe is the same in both the input string and the retrieved string.
If you are seeing ' I'd suspect that is coming from your storage where htmlentities were used to convert it. Don't convert with htmlentities before storage; use it after selection instead. That said, assuming it is also the case the htmlentities can be used on the input string (using the ENT_QUOTES as the second parameter) to compare the two. |
| All times are GMT +1. The time now is 09:00 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.