Search This Blog

Saturday, December 12, 2015

How to make a chat room ? Answer: Building a jQuery/PHP Powered Chat Room

The Plan

In this tutorial we are going to build a little chat program that is really easy to get up and running on any server running PHP. No database is required - as the chat will store itself in a simple text file. The technologies used:
  • PHP - Will handle all the server side stuff
    • Writing new messages to the text file
    • Reading out new messages from the text file
    • Retrieving the "state" of the text file
    • Basic security
  • jQuery/JavaScript - Will handle the client side stuff. This is an AJAX-y application, meaning that messages pop onto the screen (both yours and others) without needing any page refresh.
    • Periodically asking the server if there are new messages that have been posted
    • Appending new messages to the chat
    • Scrolling the chat down to the most recent messages
    • Asking and setting the user name
    • Limiting the text input to prevent gigantic ridiculous messages
    • Basic security
  • Text File - Stores the chat

NOT The Plan

This tutorial covers all kinds of interesting things and interesting technology and the end result is definitely a chat room. What it isn't is the World's Most Full-Featured Chat Room. You can't kick people out or ban people. People can potentially have the same name. You can't see all the current members of the chat. In other words, this ain't IRC. It's just a fun demo and totally workable in a number of situations. If you want to take this and run with it and make it more full featured, be our guest!

Basic HTML Structure

<div id="page-wrap">

    <h2>jQuery/PHP Chat</h2>
    
    <p id="name-area"></p>
    
    <div id="chat-wrap"><div id="chat-area"></div></div>
    
    <form id="send-message-area">
        <p>Your message: </p>
        <textarea id="sendie" maxlength = '100'></textarea>
    </form>

</div>
Precious little markup here folks. Even what you are looking at above is 50% unrelated to the specific functionality of this tutorial. The page-wrap is to center things. The double-divs thing going on with the chat-wrap and chat-area is just to pull off the totally unnecessary (but cool) double border effect in the chat area.
The two most important areas are the textarea with the id of "sendie" and the chat-area div. JavaScript will be targeting these.

The JavaScript Half of the Engine

We're going to get a little Object Oriented with our JavaScript. We'll create a "Chat" function that is parent to a bunch of other functions for dealing with Chat-related stuff.
function Chat () {
    this.update = updateChat;
    this.send = sendChat;
    this.getState = getStateOfChat;
}
updateChat will ask the server if there are new lines in the text file. If there are, it will return them as JSON and then this function will append those new lines to the chat. sendChat will be called when a message is entered into the text area and return is pressed. The function will pass that data to the server to do what it will with it. getStateOfChat asks the server basically how many lines the current text file is, so it has something to compare against and know when lines are "new" or not. This information is returned as JSON as well. And those functions look like:
//gets the state of the chat
function getStateOfChat() {
	if(!instanse){
		instanse = true;
		$.ajax({
			type: "POST",
			url: "process.php",
			data: {'function': 'getState', 'file': file},
			dataType: "json",	
			success: function(data) {state = data.state;instanse = false;}
		});
	}	
}

//Updates the chat
function updateChat() {
	if(!instanse){
		instanse = true;
		$.ajax({
			type: "POST",
			url: "process.php",
			data: {'function': 'update','state': state,'file': file},
			dataType: "json",
			success: function(data) {
				if(data.text){
					for (var i = 0; i < data.text.length; i++) {
						$('#chat-area').append($("

						"+ data.text[i] +"

						"));
					}	
				}
				document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;
				instanse = false;
				state = data.state;
			}
		});
	}
	else {
		setTimeout(updateChat, 1500);
	}
}

//send the message
function sendChat(message, nickname) { 
	updateChat();
	$.ajax({
		type: "POST",
		url: "process.php",
		data: {'function': 'send','message': message,'nickname': nickname,'file': file},
		dataType: "json",
		success: function(data){
			updateChat();
		}
	});
}
All three of these functions make use of jQuery's AJAX abilities, and do their communicating with a PHP file called process.php, which of course we'll need to build!

The PHP Half of the Engine

Part of the data that gets passed with the AJAX calls is an (arbitrary) value named "function". This is just to let the PHP file know what kind of thing we need done. As such, the first thing we'll do is snag that value and set up a switch statement that covers each possible function. We also set up a blank array to store values in that will ultimately be encoded into JSON at the end and passed back.
When we are trying to getState, the text file is read and the number of lines in returned. When we update, the file is read and any new lines are retuned. When we send, the message is processed and then written into the text file as a new line.
<?php

    $function = $_POST['function'];
    
    $log = array();
    
    switch($function) {
    
       case('getState'):
           if (file_exists('chat.txt')) {
               $lines = file('chat.txt');
           }
           $log['state'] = count($lines); 
           break;  
      
       case('update'):
          $state = $_POST['state'];
          if (file_exists('chat.txt')) {
             $lines = file('chat.txt');
          }
          $count =  count($lines);
          if ($state == $count){
             $log['state'] = $state;
             $log['text'] = false;
          } else {
             $text= array();
             $log['state'] = $state + count($lines) - $state;
             foreach ($lines as $line_num => $line) {
                 if ($line_num >= $state){
                       $text[] =  $line = str_replace("\n", "", $line);
                 }
             }
             $log['text'] = $text; 
          }
            
          break;
       
       case('send'):
       	 $nickname = htmlentities(strip_tags($_POST['nickname']));
	     $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
	     $message = htmlentities(strip_tags($_POST['message']));
	     if (($message) != "\n") {
	       if (preg_match($reg_exUrl, $message, $url)) {
	          $message = preg_replace($reg_exUrl, '<a href="'.$url[0].'" target="_blank">'.$url[0].'</a>', $message);
	       } 
	          fwrite(fopen('chat.txt', 'a'), "<span>". $nickname . "</span>" . $message = str_replace("\n", " ", $message) . "\n"); 
	     }
         break;
    }
    echo json_encode($log);
?>

Kicking it all off

We're going to need to do some JavaScript stuff to get this party started. We'll need to load jQuery, load the "engine", then do some quick functions to gather the chat participants name for joining the chat.
While we are at it, let's put the stuff in for limiting the length of the entered text and sending the text with a return-key press.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="chat.js"></script>
<script>

  // ask user for name with popup prompt    
  var name = prompt("Enter your chat name:", "Guest");
 
  // default name is 'Guest'
  if (!name || name === ' ') {
    name = "Guest";  
  }
  
  // strip tags
  name = name.replace(/(<([^>]+)>)/ig,"");
  
  // display name on page
  $("#name-area").html("You are: <span>" + name + "</span>");
  
  // kick off chat
  var chat =  new Chat();

  $(function() {
  
     chat.getState(); 
     
     // watch textarea for key presses
     $("#sendie").keydown(function(event) {  
     
         var key = event.which;  
   
         //all keys including return.  
         if (key >= 33) {
           
             var maxLength = $(this).attr("maxlength");  
             var length = this.value.length;  
             
             // don't allow new content if length is maxed out
             if (length >= maxLength) {  
                 event.preventDefault();  
             }  
         }  
                                                                                                     });
     // watch textarea for release of key press
     $('#sendie').keyup(function(e) {  
                
        if (e.keyCode == 13) { 
        
              var text = $(this).val();
              var maxLength = $(this).attr("maxlength");  
              var length = text.length; 
               
              // send 
              if (length <= maxLength + 1) { 
                chat.send(text, name);  
                $(this).val("");
              } else {
                $(this).val(text.substring(0, maxLength));
              }  
        }
     });
  });
</script>

Periodically Checking for New Messages

We need to use the "update" function of our chat to poll the text file for new messages and append them if necessary. So we'll need to call that update function on a regular basis, and we'll use JavaScript's setInterval() function for that:
<body onload="setInterval('chat.update()', 1000)">

Friday, December 11, 2015

How to Create A WordPress Membership Site (either paid, or non paid)

If you heard about a way to grow your community while increasing your income at the same time, would you be interested in learning more about it?
If so, it may be time for you to add a membership program to your site.

What’s a Membership Program?

A membership program is a members-only section of your site. Usually, to gain access to this section, people must pay to join the community, which is how you earn more money while building your community.
There’s several different types of membership programs, but before we jump into the specifics, lets look at two examples:

Example 1: Blogging Advice Membership Site

Leo Babauta, of Zen Habits fame, and Mary Jaksch run a project called A-List Blogging Bootcamps where they teach people how to become successful bloggers. They trade expertise for membership fees. (Their site design is powered by Thesis, too, by the way :-D).

Example 2: Makeup Advice Membership Site

University of Makeup offers their members a ton of videos to help them learn how to apply makeup flawlessly. Non-members are able to get a sneak peak at their video collection, while members have full access to the all of the videos they have to offer. Again, expertise for cash.

Notice a trend here?

In each example, someone trades their expertise for ongoing, monthly revenue. It’s a good business model.
Now it’s your turn. What do you specialize in? How can you trade your expertise for cash? If you’re having problems coming up with ideas, here are more examples:
  1. A woodworker can provide members exclusive access to woodworking plans as well as insight to the tools of the trade.
  2. A do-it-yourself pro can offer members tutorials for anything from fixing leaky faucets to installing a garbage disposal.
  3. A cooking aficionado can give members access to videos on how to prepare simple meals for families on-the-go.
  4. A graphic designer can grant members access to a variety of graphics for use in print, on the web, and in multimedia mediums.
  5. A professional landscaper can help people who don’t want to pay for a pricey lawn service by providing tips and tricks to keep their lawns in tip-top shape all year long.
The possibilities are endless.
So now I’m sure you’re thinking, “That’s great, Nina, but how the heck do I add a membership program to my WordPress site?”
Do not fear, my friend. I will walk you through the painless process in just three steps. Let’s get started!

How to Create a Membership Site with WordPress

WishList Member is my membership program software of choice.
It’s extremely easy to use, integrates with WordPress flawlessly, and offers great customer support.
Not to mention, it is flexible enough to meet the needs of everyone. Need to offer different membership options? Want it to play nicely with your shopping cart software? Wish to offer multi-level access to your members? If so, WishList Member has you covered.
It’s as easy to install as a plugin because it is a plugin, which means you can have your membership program up and running in seconds!

Step 1 – Upload WishList Member to Your WordPress Site

When you first sign up for WishList Member, you will receive an email with a link to download the plugin. Click on the link and save the file to your computer. You will then need to use an FTP program, such as Filezilla, to upload the “wishlist-member” folder to your plugins directory.
Warning – Be sure your FTP program is set to binary before uploading the plugin files.

Step 2 – Activate the WishList Member Plugin

Once the “wishlist-member” folder has been successfully uploaded to your server, go to your WordPress Dashboard, and then navigate to the Plugins area to activate the WishList Member plugin.

Step 3 – Enter Your Products Key for WishList Member

Once it is activated, look in the left sidebar to find WL Plugins, and then click on WL Member. Enter the products key along with the email address you used when you signed up for WishList Member.
Boom! Done.
You now have a brand new, shiny membership program on your site. That couldn’t have been easier, right?

A Quick Look at WishList Member

At the top of your screen, you will see the following WishList Member pages you can now explore:
Dashboard – This is just like your WordPress Dashboard, but it gives information about your members as well as WishList Member news.
Settings – On this page, you can configure the settings of the plugin to suit your needs.
Members – This is where you can manage your members or add them manually.
Membership Levels – You can add membership levels and content here.
Sequential Upgrade – If you wish to give your members access to your content over time, rather than all at once, you can use this page to set up sequential upgrades.
Integration – You can integrate your shopping cart software with WishList Member on this page.

The Bottom Line

As I mentioned earlier, WishList Member has an extremely supportive community.
Be sure to take advantage of the variety of video tutorials, articles, and support forums WishList Member has to offer. Within no time, you will be on your way to building a flourishing membership program for your community.
Do you run a membership program on your site? If so, how’s it going for you? If not, do you plan to start one soon? Let me know in the comments below.