Getting rid of backslash escapes passed through GET

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cormanaz
Forum Newbie
Posts: 3
Joined: Fri Jun 27, 2008 9:33 am

Getting rid of backslash escapes passed through GET

Post by cormanaz »

I have a form on which I'm doing user input validation. When I find a mistake, I'm trying to pass the valid form values back to the form page via GET so the user doesn't have to re-enter them. This is all working OK except that when one of the text fields contains text with an apostrophe, it reappears with backslashes. This code reproduces the problem if you enter some text containing an apostrophe:

Code: Select all

<?php
    if ($_POST['Submit'] == 'Submit') {
            $message = urlencode($_POST['foo']);
            header("Location: test.php?msg=$message");
    }
?>
<form action="test.php" method="post" name="form" id="form">
    <input type="text" name="foo" value="<?php echo $_GET['msg']; ?>">
    <input name="Submit" type="submit" id="Submit" value="Submit">
</form>
I have tried stripslashes() to get rid of the escapes, but it only takes care of the escaped backslash--the apostrophe is still escaped. urldecode() doesn't do the trick either. What am I doing wrong?

TIA....

Steve
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Getting rid of backslash escapes passed through GET

Post by Eran »

You have magic_quotes_gpc turned on in your php configuration file. It is recommended in general to have this option turned off, and it will solve your current predicament
cormanaz
Forum Newbie
Posts: 3
Joined: Fri Jun 27, 2008 9:33 am

Re: Getting rid of backslash escapes passed through GET

Post by cormanaz »

That did it, thanks!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Getting rid of backslash escapes passed through GET

Post by pickle »

To make your code more portable, use get_magic_quotes_gpc()

Code: Select all

$varPassedviaGet = (get_magic_quotes_gpc()) ? stripslashes($_GET['varName']) : $_GET['varName'];
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply