The problem of Jesus telling others not to reveal his identity is known as the 'Messianic Secret' and is a motif primarily found in the Gospel of Mark. According to William Wrede (Das Messiasgeheimnis in den Evangelien, 1902) the messianic secret is a prohibition to make known the messianic character of Jesus.
The following essay: 'Mark Essay 3: Is there a Messianic Secret in Mark?' says that Wrede rules out from history any theory such as a progression in the revealing of Jesus' personality, on the correct grounds that the time-framework of the gospels is imposed on the material by the evangelist, and must therefore be attributed to his theology rather than to historical reality.
The essay author concludes that the secret is a function of Mark's irony. If Mark is to maintain the dual level of discourse which is the basis of all irony, he cannot allow the actors in the drama to come too quickly to a realisation of Jesus' identity. Hence his insertion on various occasions of the secrecy commands (stylistically shown to be his own work) and of the parable-secret, which originally had a quite other import. Ian Wilson says, in Jesus: The Evidence, page 29, that while not necessarily giving this idea their full endorsement, most modern scholars accept that Wrede may well have been right in regarding Mark’s author as more concerned with putting over theology than with writing straight history. As the first narrative account of the mission of Jesus, Mark was often read to gentiles who had not yet become Christians, and the Secret added suspense and drama to the story, also perhaps explaining why this knowledge was not so widely circulated in the decades prior to authorship of the Gospel.
Matthew is a synoptic gospel and often follows Mark quite closely, including some references to the Messianic Secret, although fewer than in Mark itself. On the other hand, John's Gospel is further removed from Mark's Gospel and developed along different lines. In John, Jesus is not at all concerned with keeping his true identity secret. Scholars generally refer to the intended audience of John as the 'Johannine community', a group of Christians already familiar with the story of Christ. For them, the Messianic Secret would have made little sense, whereas they may have found Jesus' bold assertions of his identity, as in John 5:39, to be inspirational.