A
A
Afafks1231321321652020-05-23 14:11:48
PHP
Afafks123132132165, 2020-05-23 14:11:48

How to decrypt server response?

Well, I did a chat, and I reached the display of messages, and they are displayed as: The response from the server (php file) comes correctly but is replaced by &b or something like that. How can I decrypt this and make the display correct? js
<b>Name<b>Text<br>
<b>

<html>
    <head>
        <meta charset = "utf-8">
        <title>Chat</title>
    </head>
    <body>
        <div id = "div"></div>
        <div id = "div2">
          <input id = "name" type = "text" placeholder = "Name" required>
          <input id = "text" type = "text" placeholder = "Text" required>
          <button id = "button" onclick = "send()">Send</button>
        </div>
        <style>
            #div{
                margin: 0;
                padding: 0;
                background-color: black;
                width: 100%;
                height: 90%;
                color: white;
                overflow-x: scroll;
            }
            #div2{
                margin: 0;
                padding: 0;
                width: 100%;
                height: 10%;
            }
            #name{
                margin: 0;
                padding: 0;
                width: 100%;
                height: 50%;
            }
            #text{
                margin: 0;
                padding: 0;
                float: left;
                width: 80%;
                height: 50%;
            }
            #button{
                margin: 0;
                padding: 0;
                float: left;
                width: 20%;
                height: 50%;
            }
            #button:hover{
                background-color: red;
            }
        </style>
        <script>
            function send(){
                name = document.getElementById("name").value;
                text = document.getElementById("text").value;
                name = encodeURIComponent(name);
                text = encodeURIComponent(text);
                var xhr = new XMLHttpRequest();
                xhr.open("GET","chat.php?" + "Name=" + name + "&text=" + text + "&status=" + "0");
                xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                document.getElementById("text").value = "";
                xhr.send();
            }
            function text(){
                var xh = new XMLHttpRequest();
                xh.open("GET","chat1.php?" + "status=" + "1");
                xh.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                xh.send();
                xh.onreadystatechange = function(){
                if(xh.readyState === 4 && xh.status === 200){
                    document.getElementById("div").innerHTML = xh.responseText;
                    document.getElementById("div").scrollTop = 9999;
                }
                }
            }
            setInterval(text,500);
        </script>
    </body>
</html>

Code that writes:
<?php
$str = "";
if($_GET["status"] == 0){
  $name = $_GET["Name"];
  $text = $_GET["text"];
  $fd = fopen("chat.txt","a");
  $a = "<b>".$name."</b>".$text."<br>";
  fwrite($fd,"\n");
  fwrite($fd,$a);
  fclose();
}
?>

Code that reads from a file:
<?php
$str = "";
if($_GET["status"] == 1){
  $fd = fopen("chat.txt","r");
  while(!feof($fd)){
    $str .= htmlentities(fgets($fd));
  }
  echo $str;
}
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
netrox, 2020-05-23
@Afafks123132132165

You are using htmlentities()while reading, so the result is expected. But if you want to render markup safely, then use HTML Purifier.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question