DEV Community

Cover image for Chat Application using PHP

Chat Application using PHP

Rupesh Chandra Mohanty on September 20, 2020

Every programmer wants to make a chat application once in his life, be any technology stack he/she might be using. Making a simple chat application...
Collapse
 
reegodev profile image
Matteo Rigon • Edited

This post is a good example of why PHP is bashed by the whole programming community.

  • You are saving passwords in clear text.
  • All your queries are vulnerable to SQL injections.

I don't wanna sound rude but new programmers are going to see this and think this is OK. Please do not deploy this code to production.

Collapse
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

Yeah we can use md5 and salt to make the password protected. This is just an experiment on how to make a chat application with PHP.

Collapse
 
jorgecasas profile image
Jorge Casas

Please, don't use MD5 either. It's insecure. Don't try it in production.

Thread Thread
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

Yeah that's why we can use md5 and salt which can generate an unique Id hence encrypting the password field.

Thread Thread
 
jorgecasas profile image
Jorge Casas

No, seriously. Never use MD5 to encrypt a password. The are other methods more secure available php.net/manual/en/function.passwor...

Thread Thread
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

Okay I will look into it. Thank you for the suggestion 😇

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇

You can use sha256 joining the string with an application token or salt, or a cookie hash and it will be ok. If you want more security you can use 512-bit encrypt such whirlpool, sha-512 and so

Thread Thread
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

I was going to use uniqid() in php and join it with the password string. I will try your approach too. Thank you for the suggestion 😇

Thread Thread
 
reegodev profile image
Matteo Rigon

Please do not create your own auth implementation, you will regret it eventually ( there's a big yellow box in uniqid manual that says you should never use it to salt passwords).
Use one of the many well-known, tested and audited auth libraries.
If you really wanna learn how to implement a custom authentication I suggest you to browse the source code of said libraries and see how they handle it.

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇

There's nothing bad on implementing own auth if you're a senior dev and you know all about what it implies. Specially when working on a big company and being in need to create a self implemented auth to avoid extra costs of Auth APIs (Oauth, Oauth2, SSO ...), you just need to pass a security audit and the tests after building it and before linking your services to it on production

Collapse
 
xowap profile image
Rémy 🤖

That is definitely a state-of-the-art PHP implementation of a chat API, however you might want to turn to other languages which have more structured approaches.

By example, Python + Django is a fantastic way to learn web development, with neatly separated concerns and it makes it hard/unnatural to turn to bad practices (although I've seen them done too).

Collapse
 
marcstdt profile image
Marc • Edited

I am unsure, if that is a state-of-the-art PHP implementation.

I guess such an implementation would more refer to an object oriented approach.

Using mysql_real_string_escape (there are also a lot of places, where those are missing.) is kind of outdated too. If you are writing raw querys, it is more common to use prepared statements these days.
The whole procedural programming approach, which was taken here, is in my eyes absolutely not state-of-the art.

There are quite modern PHP Frameworks like Symfony, wich could do the job well.
I don't know Django, but I guess the concepts are quite similar to frameworks like Symfony.
I would recommend staying with PHP for know. Just because it's more difficult to switch a language if someone is at the beginning of his programming carrier.
It's true that PHP is not that usable in some modern web-development scenarios (websockets, realtime-apps etc.), but it's relatively easy to use (like python) and still gets the job done in most classic web-dev scenarios.

I would also suggest Python as a good language to lern next, but maybe spend some more time to get to know modern programming concepts better, with a language you already know.

I would HIGHLY recommend to learn object oriented programming and concepts like Model-View-Controller.

Collapse
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

Thank you for your suggestion😇. I will try this project again using Laravel or Symfony for sure.

Collapse
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

Yeah sure I will try that out too. Thank you for the suggestion 😇

Collapse
 
jozadaquebatista profile image
Jozadaque Batista

I understood the proposal of this post, but to make it clear for most of people that are starting in the programming world, that kind of stuff is for beginners only, in real life applications this way to do things like that is a bad practice;

Collapse
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

This post is for beginners only.

Collapse
 
joppedc profile image
JoppeDC

As mentioned before, good efforts for practising PHP, but this isn’t something you want in production.

Reinventing the wheel for a lot of things as well. I suggest you try to use a framework next time, this is what’s mostly use din the industry as well. For example, try Symfony, which uses the doctrine dbal.
It also has a security component, and components for basically everything you’re doing here.

Collapse
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

Okay I will work on it. Thanks!

Collapse
 
yellow1912 profile image
yellow1912 • Edited

Use reactphp or swoole to handle sockets, use redis to temporary save chats before saving to database?

Collapse
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

I will make this again using react, this one is for beginners

Collapse
 
e8089 profile image
e8089

how can i make the message sent from one mail to be moved to the person email address or that of the admin

Collapse
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

Sorry I didn't get your question 😅

Collapse
 
smallreporter profile image
Small Pig reporter • Edited

Can I make it into a website? I can’t find any web app hoster

Collapse
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

Yeah sure!

Collapse
 
reegodev profile image
Matteo Rigon

That's true but the number of bad php examples out numbers every other language by many orders of magnitude.

Collapse
 
madza profile image
Madza

What about Laravel? Some good practices baked in there :)

Collapse
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

I will implement this chat api using Laravel too. Thanks for the suggestion 😇

Collapse
 
gjeotech profile image
Onyeacholem Ifeanyi Joshua

It was awesome, keep it up ! more ideas and knowledge to do more .

Collapse
 
rupeshmohanty profile image
Rupesh Chandra Mohanty

Thank you