Archive for the ‘Tutorials’ Category

Daily Tutorial: Count The Number Of Chatters In IRC

Posted November 18th, 2008, at 02:19 AM

Eleven2In honor of IRCNode’s return, there will be several IRC related tutorials posted today. Daily tutorials start up again today, and I’ll keep going 30 days from now to apologize for my disappearance.

IRCNode’s website isn’t back up yet, but the IRC network itself is doing well. Just connect to irc.ircnode.org or use the Mibbit Web Client. Be sure to come hang out in #lobby.

The Tutorial

As with all my tutorials, I put the instructions inside the code as comments. The instructions are preceded by two back slashes (//) and are generally found below the line they are explaining.

If you have any problems with this tutorial, please post a comment or email me at naresh@eNaresh.com.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
	set_time_limit(0);
	// This script *might* take a while, so disable the time limit.
 
	$socket = fsockopen("irc.ircnode.org", 6667);
	// We'll use fsockopen to connect to the IRC network.  The second parameter is the port number.
	// Change irc.ircnode.org to the IRC network you want to conncet to.
 
	sleep(3);
	// Wait 3 seconds for the script to connect before setting the bot's nick name.
	// If the script doesn't work, try increasing this nunmber.  3 seconds seems to work well for most networks though.
 
	fputs($socket,"USER Counting eNaresh.com eN :eN Bot\n");
	// Give IRC some information about the bot.  Required for connecting.
 
	fputs($socket,"NICK [Counting]\n");
	// Tell IRC that the username should be [Counting].
	// This bot doesn't connect to any channels, but needs a nick name
 
 
	while($data = fgets($socket, 128)) {
		// For each line of data, execute the code inside the brackets.
 
		$ex = explode(' ', $data);
 
		if($ex[0] == "PING"){
			// If the IRCd PINGs, be sure to PONG.
			fputs($socket, "PONG ".$ex[1]."\n");
		}
 
		if(strpos("$data", "Current Local Users"))
		{
			// This script looks for the text "Current Local Users".  If the line contains this text, the code
			// inside these brackets will be executed.
 
			$first = explode("Current Local Users: ", $data);
			// Separate the message from the IRCD after "Current Local Users".
			// Everything before "Current Local Users: " is in $first[0]
			// Everything after "Current Local Users: " is in $first[1]
 
			$last = explode(" Max:", $first[1]);
			// Separate message again.  The number of local users is directly after the text "Current Local Users: ",
			// so we use $first[1] as the string to separate.
 
			$last[0] = $last[0]-1;
			// Subtract 1 from the number of users, so we don't count the bot.
 
			echo '<strong>Current Chatters:</strong> ' . $last[0] . '<br />
			<strong>Maximum Chatters:</strong> ' .  $last[1];
			// Display the data
 
			fclose($socket);
			// Close the socket.
 
			die();
			// Kill the script so it doesn't keep trying to get data.
			// The bot will disconnect from IRC.
		}
	}
 
	fclose($socket);
	// Close the socket.  Just in case the number of current chatters never shows up.
?>

I’d only use this code on a CRON job and save the data to a MySQL database, because it takes a few seconds to run. If you don’t have CRON jobs, email me (naresh@eNaresh.com), and I can set it up on my server.

Requested: Ban System Admin Panel

Posted November 9th, 2008, at 04:00 PM

banned admin control panelThis isn’t today’s tutorial. Today’s tutorial was on creating daily backups with PHP.

Cyrus Kafai Wu requested that I write an admin control panel for the ban system posted yesterday. Here you go, Cyrus.

Daily PHP Tutorial: Create Easy Daily Backups

Posted November 9th, 2008, at 11:00 AM

backup website with phpWe’re now 9 days in to the tutorial marathon. I’m having a good time with this, and I hope you are too!

This tutorial was requested in the comments a few days ago. It’s a PHP script that will allow you to easily create daily backups of your website. It does require that you are on a linux web server.

The “tar” command in linux allows you to create .tar archives of your directories. We’re going to use that command in conjunction with the “system” PHP Function.

The Code

As with all my tutorials, the instructions are included inside the code so even the copy and pasters can learn a thing or two. They’re inside the code as comments, or lines preceded with two backslashes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?
	$your_email = 'naresh@eNaresh.com';
	// Change the above to your email address.  This is used to send emails informing you of the status of the backup.
 
	$directory = '/home/ragsites/public_html/enaresh/wp-content/themes/Mark';
	// Change this to the path of the directory you wish to backup.  To backup the directory this file is located in, simply change it to a period.
 
	$backup_file = '/home/ragsites/backups/backup' . date("MdY") . '.tar';
	// The location where the backup will be saved.  Keep this out of your public_html folder, if possible.
	// In my example filename, date(MdY) adds the date in this format to the filename: Nov092008
 
 
	if(system("tar cvf $backup_file $directory"))
	{
		// Attempts to backup the directory using the "tar" linux command.
 
		// Inside this set of brackets, the backup was successful.
 
		mail($your_email, 'Backup Complete', 'A backup of "' . $directory . '" has been created.  It has been saved at "' . $backup_file . '"');
		// The above line sends an email similar to:
		// A backup of "/home/ragsites/public_html/enaresh/wp-content/themes/Mark" has been created.  It has been saved at "/home/ragsites/backups/backupNov092008.tar"
	}else{
		// The backup failed!  Send an email saying it failed!
		mail($your_email, 'Backup Failed', 'Your backup of "' . $directory . '" failed!');
	}
 
?>

Run this code with a CRON job for best results. I wouldn’t run it more than once a day unless your files change very frequently. Don’t have CRON Jobs? Send me the URL of this file on your server along with how frequently you want to backup, and I’ll configure my server to trigger backups at intervals of your choice. naresh@eNaresh.com

Remap Your Keyboard With SharpKeys

Posted November 9th, 2008, at 06:00 AM

remap your keyboard with sharpkeysIf you follow my Twitter, you might have seen a tweet yesterday evening saying I broke my “L” key completely. The rubber contact below the plastic key broke, and the key was rendered completely useless. I solved this problem by remapping my keyboard with SharpKeys, and my new “L” key is my former “Caps Lock”.

Remapping your keyboard allows you to reprogram a button. It can be done manually, but is a tedious process. It didn’t even work for me when I gave it a try.

SharpKeys from RandyRants.com solved the problem perfectly. You simply select which key to replace, and the new action for that key. Reboot or logout, and your keyboard is remapped!

Daily PHP Tutorial: PHP Ban System

Posted November 8th, 2008, at 11:30 PM

php ban systemWe’re on day eight of the tutorial marathon, and I’m still going strong(ish). Today’s tutorial is yet another requested tutorial teaching you how to ban your problematic website visitors. This tutorial is for you, Marcos in Austin, TX.

You can view an example of this by going to: http://enaresh.com/tut_files/banned.

PHP Tutorial: Edit Your CSS From A Web Form

Posted November 8th, 2008, at 12:08 AM

I mentioned earlier that my CSS was deleted by a PHP file I stupidly left out in the open. The file was the code for a requested tutorial that I didn’t publish until right now.

This tutorial teaches you how to create a file that will allow you to edit your CSS file easily from an admin control panel. Don’t leave this file out in the open like I did.

I know this is another simple tutorial. I’ll have part two of the forum tutorial up sometime tomorrow afternoon.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?
	$css_sheet = "/home/ragsites/public_html/enaresh/wp-content/themes/Mark/style.css";
	// Change this to the path to your CSS file
 
	if($_POST[save])
	{
		// The "save" button was pushed!  Let's write to the css file
 
		$_POST[css] = stripslashes($_POST[css]);
		// Removes backslashes before quotation marks.  Otherwise some parts of your css might not work if you use quotation marks in css
 
		$file = fopen($css_sheet, "w");
		// Opens the file for editing.  The "w" mode clears the file before saving, instead of adding the content to the bottom.
 
		$write = fwrite($file, $_POST[css]);
		// The above line uses the fwrite function to save the textarea value ($_POST[css]) to the css file.
 
		fclose($file);
		// Closes the file, clears up some memory and is good practice
 
		echo "CSS Saved";
		// Simply displays "CSS Saved"
	}else{
		// The form was not submitted, let's read the file and put the contents in a textarea:
 
		$file = fopen($css_sheet, "r");
		// Opens the file for reading only.
 
		$read = @fread($file, filesize($css_sheet));
		// Reads the contents of the file using fread.
		// The first parameter is the location of the file.
		// The second parameter is the file size of the css stylesheet.  We use filesize to ensure we read the entire file
 
		fclose($file);
		// Closes the file for editing.  
 
		echo "<form method=\"post\">
		<textarea name=\"css\" style=\"width: 100%\" rows=\"20\">$read</textarea><br />
		<input type=\"submit\" name=\"save\" value=\"Save\" />";
		// Displays a form.
		// Textarea named "css" will be $_POST[css] after the form is submitted.
		// Submit button named "save" will be $_POST[save] after submitted, and will be used to see if the form has been submitted.
	}	
?>

Please send any requests to naresh@eNaresh.com or post a comment.

PHP Tutorial: Take Your Music Library With You

Posted November 6th, 2008, at 04:53 PM

mp3As requested yesterday by email, today’s daily tutorial teaches you how to take your music library with you anywhere you have internet.

While there are other ways to do this using programs that plug in directly to iTunes, we’re just going to create a simple PHP script that will list every file in a directory, and enable them to play inside a flash mp3 player.

You can either setup PHP and Apache on your home computer or upload your MP3s to a web host.

Example: http://enaresh.com/tut_files/music/

Daily PHP Tutorial: Create A Forum (Part One)

Posted November 4th, 2008, at 04:01 PM

create a forum with phpWe’re now 4 days into the tutorial marathon here on eNaresh. If you’re new, I’m writing a new PHP tutorial every day of November. The past tutorials are in the PHP Tutorials Archive.

I’m starting a new series of tutorials today. We’re going to start coding a forum with PHP and MySQL. This series will probably have upwards of 10 parts, and I’ll make sure it’s secure, powerful, and easy to use. This series will not be object oriented, but may be re-released OOP in the future.

You can view an example of part one here: http://enaresh.com/tut_files/forum/

PHP Tutorial: Create A Podcast Player

Posted November 3rd, 2008, at 02:37 PM

I’m 3 days into the November Tutorial Marathon. I’m posting one tutorial every day throughout November. Please send requests and suggestions to naresh@eNaresh.com

Today’s Tutorial: We’re going to load a podcast RSS feed, separate the items, and display them with a nice Flash mp3 player from Yahoo. Alternating row colors will come into play today as well.

Example: http://enaresh.com/tut_files/podcast/

Oh, and there might be a bonus at the end of this post. Who knows…

TOD: Using Explode & Create PHP Function Resource

Posted November 2nd, 2008, at 07:24 PM

PHP Tutorial - explodeHere’s today’s PHP tutorial of the day. If you haven’t heard yet, I’m writing one tutorial every day for a month. This is the second tutorial of the month. Remember, I need your suggestions, so please request tutorials by emailing naresh@eNaresh.com. I’ll even plug your website in the post.

Today’s tutorial covers the explode function. I wanted to use it in a somewhat practical application, so the code in this tutorial will show information about a given PHP function, taken from the PHP Manual.

As with all of my tutorials, the instructions are located inside the code as comments. This way, even the copy and pasters can learn a thing or two.

Example: http://enaresh.com/tut_files/manual/

The rest of the tutorial is in the full post.