LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-27-2006, 11:50 AM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,505
Blog Entries: 1

Rep: Reputation: 67
Question php: character \ in an input


I'm creating one PHP application what will read a string and will write it in one LDAP (Active Directory) to set it as a user's profilePath. So far so good, I've been able to write stuff into the AD without a problem, however I want to make it in a web page so I can make as may tries as necessary.

I'm testing rigth now and I have noticed that when I write a \ in a text input, in PHP it's read as '\\'.

Even when I render the page I happen to place the value that was read in the value of the input, and I get to see a \\ for each time I wrote a \ in the request. If I wrote \\hello, I'll get \\\\hello in the refresh of the field.

How can I avoid that behavior?

Simplified problem:
Code:
$afield = $_GET['afield'];
?>
<input type="text" name="afield" value="<?= htmlentities($afield) ?>">
<?

Last edited by eantoranz; 09-27-2006 at 11:55 AM.
 
Old 09-27-2006, 11:54 AM   #2
senyahnoj
Member
 
Registered: Jul 2004
Location: Gloucestershire, UK
Distribution: Ubuntu, Debian & Gentoo
Posts: 74

Rep: Reputation: 15
Try using the stripslashes() function:

http://uk2.php.net/manual/en/function.stripslashes.php
 
Old 09-27-2006, 12:00 PM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,505
Blog Entries: 1

Original Poster
Rep: Reputation: 67
It worked. Thanks!!!
 
Old 09-27-2006, 11:03 PM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 10.04, Crunchbang Statler
Posts: 3,325

Rep: Reputation: 168Reputation: 168
I think an explanantion should be here.

Why do you need to use stripslashes in this situation? If you did not use addslashes (to add the backslash), this is caused by a setting in php.ini called magic_quotes_gpc.
You can create a php page that calls phpinfo to display configuration information to check.

If magic_quotes_gpc is on, an additional backslash will be added for characters like single and double quote and backslash. If magic_quotes_gpc is off, this will not happen.

Be carefull as there might be some issues here:
1)
If you are not in control of the webserver, you depend on what the administrator sets magic_quotes_gpc to. If you are in control, you can determine the setting.
This is a global setting for all sites that are served from the server (so an administator might be reluctant to change the setting and if you're the administrator you should be aware of the implications of randomly changing it).
2)
I've read an article a while ago (could not locate it now) that magic_quotes_gpc will disappear in the future and will be set to off. So your stripslashes might have the site-effect that it strips the backslash in the actual data.

To prevent problems in future, you might want to use the following code that will check if magic_quotes_gpc is on and only strip in that case
Code:
function dostripslashes($str)
{
    if(get_magic_quotes_gpc())
        $str=stripslashes($str);

    return $str;
}
In a similar fashion, you can have a function that adds the slashes when necessary
Code:
function doaddslashes($str)
{
    if(!get_magic_quotes_gpc())
        $str=addslashes($str);

    return $str;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
input japanese character maurolinux Linux - Newbie 2 02-14-2006 07:14 AM
Japanese Character Input atheist Linux - Software 1 07-10-2004 11:52 PM
chinese character input and display -- need help carboncopy Slackware 5 07-06-2004 08:12 PM
character code and keyboard input question blish_blash Programming 1 04-24-2004 06:09 PM
winkey mapped to multiple character input g-off Linux - General 0 02-24-2004 02:26 AM


All times are GMT -5. The time now is 06:42 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration