1

I have a chat that uses ajax to send messages to the server. My problem is that when the user tries to send a hashtag, everything after the hashtag is omitted. I saw a similar question like this that said encoding would help. I tried encoding it, but it created bigger problems when it got to the php. It started counting the + sign as a space and made the first two numbers after a % sign go missing. Is there a workaround to this in php, or should I try something other than encoding.

here is the relevant javascript with the encoding.

ajax.open("GET",   "/chatAdd.php?msg=" + encodeURIComponent(message.value) +   "&user=" + encodeURIComponent(user) + "&id=" + encodeURIComponent(id));

And the php i used to decode it.

$user = urldecode($_REQUEST["user"]);
$userID = urldecode($_REQUEST["id"]);
$msg = urldecode($_REQUEST["msg"]);
Squirrel
  • 15
  • 6
  • 1
    Possible duplicate of [What is the equivalent of JavaScript's encodeURIcomponent in PHP?](http://stackoverflow.com/questions/1734250/what-is-the-equivalent-of-javascripts-encodeuricomponent-in-php) – Jacob Aug 20 '16 at 04:03
  • 1
    I don't think you need those calls to `urldecode`, it should be decoded automagically. – Alexander O'Mara Aug 20 '16 at 04:04

1 Answers1

2

Use Percent encoding. Replace the hash with %23.

Edit: Also, a really great resource for escaping in JS and PHP - http://www.the-art-of-web.com/javascript/escape/

Munsterlander
  • 1,356
  • 1
  • 16
  • 29