In this guide, we will be creating a simple web-based chat application with PHP and jQuery. This sort of utility is perfect for a live support system for the site.
This guide ended up being updated recently to create improvements within the chat app.
Step 1 HTML Markup
We shall start this tutorial by creating our file that is first index.php.
- We start the usual DOCTYPE to our HTML, html, mind, and body tags. Into the mind label, we add our title and link to our CSS stylesheet (style.css).
- Inside the physical body label, we structure our layout inside the #wrapper div. We will have three main obstructs a menu that is simple our chatbox, and our message input, each featuring its respective div and id.
- The #menu div shall contain two paragraph elements. The initial is a welcome towards the individual and will be regarding the left, and the second shall be an exit website link and will also be in the right. We have been making use of flexbox as opposed to floating elements for the design.
- The #chatbox div shall include our chatlog. We’ll load our log from a file that is external jQuery’s ajax request.
- The very last item inside our #wrapper div will likely be our form, which will come with a text input for an individual message and a button that is submit.
- We add our scripts last so the web page shall load faster. We shall first link to the Cloudflare jQuery CDN, even as we is using the jQuery library for this tutorial. Our script that is second tag what we is going to be working on. We shall load all of our rule following the document is prepared.
Step 2 CSS Styling
We shall now atart exercising . CSS to make our talk application l k better than aided by the default web browser styling. The code below will be included with our style.css file.
There’s nothing special about the above CSS apart from the undeniable fact that some ids or classes, which we now have set a style for, will likely be added a bit later on.
Step 3 making use of PHP to make a Login kind
Now we’ll implement a form that is simple will ask an individual their title before continuing further.
The loginForm() function we produced is made up of the login that is simple which asks the consumer with regards to their name. We then make use of an if and statement that is else validate that anyone entered a title. In the event that person joined name, we set that title as $_SESSION[‘name’] . Since our company is utilizing a c kie-based session to store the name, we must phone session_start() before any such thing is outputted to your browser.
Something that you might want to seriously consider is we’ve used the htmlspecialchars() function, which converts special figures to HTML entities, therefore protecting the title adjustable from dropping victim to cross-site scripting (XSS). Later on, we shall additionally include this function to your text variable that’ll be posted to the chat log.
Showing the Login Form
So that you can show the login form in case a user hasn’t logged in, and therefore have not created a session, we utilize another if and else statement around the #wrapper div and script tags inside our initial code. This will hide the login form and show the chat box if the user is logged in and has created a session in the opposite case.
Welcome and Logout Menu
We have been not yet finished creating the login system with this application that is chat. We nevertheless need certainly to allow the user to log away and end the talk session. If you keep in mind, our original HTML markup included a menu that is simple. Let’s return and then add code that is PHP can give the menu more functionality.
To start with, let us add an individual’s name towards the message that is welcome. We repeat this by outputting the session of this individual’s title.
To be able to enable the user to log away and end the session, we will leap ahead of ourselves and briefly use jQuery.
The jQuery rule above just shows a confirmation alert if a user clicks the #exit link. Then we deliver them to index.php?logout=true if the user verifies the exit, consequently determining to get rid of the session . This just creates a adjustable called logout with all the value of true . We have to catch this variable with PHP
We now see if a get adjustable of ‘logout’ exists utilising the isset() function. If the variable is passed using a Address, including the link mentioned previously, we check out end the session associated with the user’s name.
Before destroying the user’s name session with all the session_destroy() function, we want to write a straightforward exit message to the log that is chat. It shall say that an individual has left the talk session. We repeat this using the file_put_contents() function to govern our log.html file, which, as we shall see afterwards, will be created as our chat log. The file_put_contents() function is just a convenient solution to write information up to a text file in place of utilizing fopen() , fwrite() , and fclose() each time. Just be sure that you pass appropriate flags like FILE_APPEND to append the data at the final end of this file. Otherwise, a new $logout_message will overwrite the previous content for the file. Please be aware that a class has been added by us of msgln to the div. We’ve already defined the CSS styling for this div.
After carrying this out, we destroy the session and redirect an individual to your exact same page where the login form will show up.
Step 4 Handling User Input
Following a individual submits our kind, you want to grab their input and write it to the chat log. To do this, we ought to utilize jQuery and PHP working synchronously on the customer and server edges.
Almost anything we will do with jQuery to deal with our data will revolve around the jQuery post request.
- Before we do anything, we must grab the consumer’s input, or what the user has typed in to the #submitmsg input. This is achieved using the val() function, which gets the worth set in a questionnaire field. We now store this value in the clientmsg adjustable.
- Right Here comes our many essential component the jQuery post request. This delivers a POST request towards the post.php file that we will produce in a moment. It posts your client’s input, or exactly what happens to be saved into the clientmsg variable.
- Finally, the#usermsg is cleared by us input by setting the worthiness attribute to blank.