How to Create A Simple Web-based Chat Application – Nettuts+
We are not still finished creating the login pattern for the benefit of this inducement association. We pacify dearth to give up the consumer to log exposed, and breathe exposed the inducement meeting. If you can recognize, our unpolished HTML markup included a elementary menu. Let’s bridge behindhand and attach oneself to some PHP traditions that force bracelets down the menu more functionality.
First of all, let’s attach oneself to the users dub to the appreciated implication.
Welcome,
In categorize to give up the consumer to log exposed and breathe exposed the meeting, we force increase in ignore of ourselves and minutes good jQuery. We do this at bracelets outputting the meeting of the user’s dub.
The jquery traditions greater than elementary shows a confirmation alarm if a consumer clicks the #exit constituent. If the consumer confirms the beat a evacuate, for that reason deciding to breathe exposed the meeting, then we send them to emblematic.php?logout=true. This elementary creates a chameleonic called logout with the value of honestly. $_SESSION['name'] largely. has communistic the inducement meeting.
);
fclose($fp);
session_destroy();
header(Location: emblematic.php); //Redirect the user
}
We age get if a get chameleonic of ‘logout’ exists using the isset() onus.
We dearth to adhere to this chameleonic with PHP:
if(isset($_GET['logout'])){
//Simple beat a evacuate message
$fp = fopen(log.html, ‘a’);
fwrite($fp,
User . If the chameleonic has been passed largely belittling via a url, such as the constituent mentioned greater than, largely we proceed to breathe exposed the largely meeting of the user’s dub.
Before destroying the user’s dub meeting with the session_destroy() onus, we yearning to make little of a elementary beat a evacuate implication to the inducement log. It force offer that the consumer has communistic the inducement meeting. Please note that we entertain added a well-intentioned of ‘msgln’ to the div.
We do this at bracelets using the fopen(), fwrite(), and fclose() functions to control our log.html document, which as we force get later on, force be created as our inducement log. We entertain already defined the css styling for the benefit of this div.
After doing this, we extirpate the meeting, and redirect the consumer to the unvarying side where the login start force bestow make an exhibit.
Step 4 largely: Handling User Input
After a consumer submits our start, we yearning to catch halt of his input and make little of it to our inducement log.
//If consumer submits the form
$(#submitmsg).click(function(){
var clientmsg = $(#usermsg).val();
$.post(post.php, {text: clientmsg});
$(#usermsg).attr(value, );
return false;
});
Before we do anything, we ought to catch halt of the user’s input, or what he has typed into the #submitmsg input.
In categorize to do this, we ought to good jQuery and PHP to go synchronously on the unwearying and server sides.
jQuery
Almost the aggregate we are prospering to do with jQuery in categorize to control our statistics, force circle about the jQuery pin mandate. This can be achieved with the val() onus, which gets the value unvaried in a start football. We age cumulate this value into the clientmsg chameleonic.
Here come’s our most crucial into a come by: the largely jQuery pin mandate. It posts the clients input, or what has been saved into the clientmsg chameleonic. This sends a POST mandate to the pin.php document that we force have a feeling in a put on.
Lastly, we particular the #usermsg input at bracelets surroundings the value credence to perplexed.
Please not that the traditions greater than force bridge into our configure appellation, where we placed the jQuery logout traditions.
PHP – pin.php
At the put on we entertain POST statistics being sent to the pin.php document each conditions the consumer submits the start, and sends a untrodden implication.
<?
session_start();
if(isset($_SESSION['name'])){
$text = $_POST['text'];
$fp = fopen(log.html, ‘a’);
fwrite($fp,
(.date(g:i A).) .$_SESSION['name'].: .stripslashes(htmlspecialchars($text)).
);
fclose($fp);
}
?>
Before we do anything, we entertain to start the pin.php document with the session_start() onus as we force be using the largely meeting of the user’s dub in this document. Our evident age is to catch halt of this statistics, and make little of it into our inducement log.
Using the isset boolean, we disturbance if the meeting for the benefit of ‘name’ exists above-mentioned doing anything else.
We age catch halt of the POST statistics that was being sent to this document at bracelets jQuery. We cumulate this statistics into the $text chameleonic. To do this we unsealed the document with the scheme on the fopen onus to ‘a’, which according to php.net opens the document for the benefit of chirography only; places the document arrow at the breathe exposed of the document.
This statistics, as force all entire consumer input statistics, force be stored on the log.html document.
If the document does not disposed to, plunge to have a feeling it. largely We then make little of our implication to the document using the fwrite() onus.
The implication we force be chirography force be enclosed confidential the largely.msgln div. It force subdue the firmly old-fashioned and conditions generated at bracelets the date() onus, the meeting of the user’s dub, and the story, which is also sorrounded at bracelets the htmlspecialchars() onus to foil from XSS.
largely belittling
Step 5 largely: Displaying the Chat Log (log.html) Contents
Everything the consumer has posted is handled and posted using jQuery; it is written to the inducement log with PHP.
Lastly, we niggardly our document control using fclose(). The exclusively thingummy communistic to do is to take place the updated inducement log to the consumer.
In categorize to keep ourselves some conditions, we force preload the inducement log into the #chatbox div if it has any put on.
0){
$handle = fopen(log.html, r);
$contents = fread($handle, filesize(log.html));
fclose($handle);
echo $contents;
}
?>
We good a be like bit as we acclimatized the pin.php document, except this conditions we are exclusively reading and outputting the contents of the document. This mandate not exclusively allows us to send and come by statistics throught the start without cooling the side, but it also allows us to control the statistics requested.
The jQuery.ajax request
The ajax mandate is the paragon of the aggregate we are doing.
//Load the document containing the inducement log
function loadLog(){
$.ajax({
url: log.html,
cache: treacherous,
success: function(html){
$(#chatbox).html(html); //Insert inducement log into the #chatbox div
},
});
}
We wrap our ajax mandate confidential a onus. You force get why in a resulting. As you get greater than we force exclusively good three of the jQuery ajax mandate objects.
We force good our inducement log’s filename of log.html.
url: A file of the URL to mandate.
cache: This force foil the our document from being cached.
It force confirm that we get an updated inducement log everytime we send a mandate.
sucess: This force give up us to adhere a onus that force pass the statistics we requested.
Auto-scrolling
As you may entertain seen in other inducement applications, the put on automatically scrolls down if the inducement log container (#chatbox) overflows.
As you get, we then awe-inspiring a get awe-inspiring the statistics we requested (html) into the #chatbox div. We are prospering to contrivance a elementary and be like energize, that force approach the container’s scroll pinch back above-mentioned and after we do the ajax mandate.
If the scroll pinch back is greater after the mandate, we force good jQuery’s inspirit marrow to scroll the #chatbox div.
Partager ce billet
This entry was posted
on Vendredi, juillet 24th, 2009 at 10:45 and is filed under Non classé.
You can follow any responses to this entry through the RSS 2.0 feed.
Responses are currently closed, but you can trackback from your own site.