Web Development: Introduction to Javascript

Published on Mar 16, 2009   //  Development

Web Development

Javascript is a client-side scripting language. It is processed within the web browser (therefore it’s processed on the client-side). Javascript can be used to add interactivity and dynamic content to web pages. Javascript can also be used to reduce the number of page reloads necessary for certain actions.

Even though Javascript has existed since the days of Netscape (the creators of Javascript), it has only recently become widely used and a vital part of some websites. This is due in part to AJAX (Asynchronous Javascript and XML) becoming widely supported in browsers, allowing websites to take advantage of transmitting data with the server in the background (without reloading the page).

The name “Javascript” is deceiving, however, because it actually has little to do with the programming language Java. Their similarities end with having a syntax similar to C and sharing similar naming conventions.

Javascript is an object language. It is largely based around the Document Object Model (DOM) of the page. Although Javascript is most commonly used on webpages, it is also used elsewhere. Actionscript (Adobe Flash’s programming language) is an implementation of Javascript’s standard, EMCAScript. It is also used for some of Firefox’s GUI and to create Operating System widgets.

Over the next couple of weeks, we’ll be divulging into the world of Javascript.

Moving Beyond SSH: phpMyAdmin and SFTP

Published on Mar 12, 2009   //  Development
Off

Moving Beyond SSH

This week we’ll be going over installing phpMyAdmin and talking about SFTP. We’ll also go through in a few tips on migrating your sites to your new server.

phpMyAdmin

phpMyAdmin is a PHP application that provides a web-based interface to managing MySQL databases. It allows you to add, edit, manage and view your databases and tables. It’s the industry standard for MySQL web interfaces. Installing phpMyAdmin is fairly simple, and configuration is done via a web interface.

Start out by downloading phpMyAdmin to your server and untarring it to a web accessible part of your server (rename the folder to “phpmyadmin”). Since phpMyAdmin is just a PHP application, it needs to be somewhere within your web server directory, just as any other PHP app would. Now, we need to prepare it for the configuration:

cd phpmyadmin
mkdir config
chmod o+rw config

Now, visit /phpmyadmin/setup in your browser, prepending the domain or IP address the files are accessible via. Click the New server button. Change Authentication type to http. Click the Save button. Back in SSH:

mv config/config.inc.php .
chmod o-rw config.inc.php

You can now visit /phpmyadmin/ and login with your root MySQL account. You’re now able to manage MySQL stuff in an easy-to-use web interface.

SFTP

If you’re wondering how to transfer files onto your server without an FTP server installed, you can use SFTP. SFTP (Secure File Transfer Protocol) is basically FTP over SSH. It allows us to transfer files via our SSH server. Almost all FTP clients also support SFTP. Connecting is fairly simple, provide your IP address, SSH port, username and password to your FTP client and choose “SFTP” (or similar) under the connection type (this will obviously vary from client to client). Voila, you can now (securely) transfer files to and from your server.

Migrating

Migrating your sites to your new server is pretty simple. First of all make you’ve setup the DNS and VHost configuration for the domains you’re transferring. If you need to migrate your MySQL databases, export your databases via phpMyAdmin from your old host, then import them through phpMyAdmin on your server (create the user and database on your server first).

For transferring your files, you could download your files from your old server via FTP, them upload them to your new server via SFTP. However, what might be faster, if your old host allows you to download all your files in a .zip or .tar archive, you could then upload the archive to your server, then use your SSH savviness to unpack the files and move them to the appropriate folders. This way will definitely be quicker (it’ll involve less download/upload time from your computer).

What’s coming up next

We’ve already gotten a couple of suggestions for future articles, but we can always use more. If there’s anything you’d like to install or do with your server, post them in the comments and we’ll consider writing an article on it.

WordPress Development: Creating Options Pages

Published on Mar 11, 2009   //  Development, WordPress

WordPress Development

Often times when creating a WordPress plugin, you’ll want users to be able to set options for it. Most users will not want to have to edit your plugin file to be able to do this, so we should create an options page. Options pages usually reside under Settings in the WordPress Administration panel.

It is possible to create a new top-level menu and even create a page under a different top-level menu item. However, today we’re just going to go over adding an options page.

Adding an options page consists of doing three things. Creating a function to add a submenu, hooking that function into WordPress and creating a function for the display and processing of the options page. Let’s start with the first one. Read More »

Everything PHP: Switch

Published on Mar 10, 2009   //  Development

Everything PHP

This week on the Everything PHP series, we’ll be talking about (yet another) control structure. This one can sometimes save you time while writing the code, and sometimes even while running it. We’ll be talking about the switch statement.

The switch statement allows you to compare multiple values with the same expression. While we could easy do this with a series of if and elseif constructs, it would be redundant and tedious to program (especially if you need to compare a lot of values to the same expression). Let’s take a look at an example of using if/elseif, then we’ll show you how to do this using switch:

[code='php']if ($x == 0) {
echo "x is 0";
}
elseif ($x == 1) {
echo "x is 1";
}
elseif ($x == 2) {
echo "x is 2";
}
elseif ($x == 3) {
echo "x is 3";
}[/code]

In this script, $x is checked to see if it is multiple values, then that value is outputted, preceded by “x is “. This quickly becomes repetitive to type out, and you’ll be hoping for a better way. Luckily, there is one.

[code='php']switch ($x) {
case 0:
echo "x is 0";
break;
case 1:
echo "x is 1";
break;
case 2:
echo "x is 2";
break;
case 3:
echo "x is 3";
break;
}[/code]

Basically, switch statements start with the expression you want to test against (usually a variable). Within that statement, you will test for several “cases”. Each case is ended with a break;. So, this script is functionally identical to the previous one, but it is shorter and a lot quicker to type.

Cases can contain the usual suspects for values: integers, strings and boolean. If you want to do the same thing for multiple cases if they’re true, you can remove the code normally inside the case, and the break;. You can then just have this code in the last one. Let’s look at an example of this:

[code='php']switch ($i) {
case "a":
case "e":
case "i":
case "o":
case "u":
case "w":
echo "i is a vowel";
break'
case "y":
echo "i is sometimes a vowel";
break;
default:
echo "i is consonant";
}[/code]

In this example, you can see how we “stacked” multiple cases together to prevent redundancy (because we wanted to do the same thing for each of them). Also note in this example is the default case. This allows you to set a default if none of the other cases are true. This default needs to be the very last case listed in a switch. The ending break; for the default case is not necessary, because it is the last (and always will be).

Conclusion

There you have it, a simplified way of checking multiple values of the same expression. Next week we’ll be talking about alternative syntaxes for these control structures.

Web Development: Conditional Comments

Published on Mar 9, 2009   //  Development
Off

Web Development

Have you ever had a webpage working perfectly in Firefox and Safari, but looking horrendous in Internet Explorer? Yes, we all have. Sometimes you may be unable to change your CSS to accommodate for Internet Explorer. That’s where Conditional Comments comes in. Conditional comments are blocks of HTML that only Internet Explorer will render, other browsers will see it as regular comment and skip past it.

Conditional comments are basically specially formatted comments that Internet Explorer will understand to process their contents, but other browsers will ignore it. Conditional comments were introduced in Internet Explorer 5. Yes, we could do this using a server-side scripting language, but that quickly becomes messy, and harder to maintain.

Let’s start out with what regular HTML comments look like:

[code='xml'][/code]

As you can see, HTML comments start with <!– and end with –>.

Conditional comments are separated into two types of comments, downlevel-hidden (only Internet Explorer will render the contents of the comment) and downlevel-reveled (only browser other than Internet Explorer will render the comment). Let’s start by looking at the syntax of a downlevel-hidden conditional comment:

[code='xml'][/code]

expression can be many thing. It could simply be IE, it could also include the version of IE to display the HTML to or it could be a greater-than, less-than, greater-than-equal-to or less-than-equal-to example. For a full list of the possible values, see here. So, if we wanted to display a stylesheet to Internet Explorer version 7 and under, we would do so like this:

[code='xml'][/code]

Now, let’s take a look at downlevel-revealed conditional comments. These comments are for displaying HTML to all browsers except Internet Explorer. It’s syntax is as follows:

[code='xml'] HTML [/code]

So, if we wanted to show a stylesheet to every browser but Internet Explorer, we could do this:

[code='xml']

[/code]

Conclusion

That concludes our post on conditional comments. Hopefully, as Internet Explorer development continues to catch-up with more modern browsers, these comments won’t have to be used anymore. But, until then, we have these handy conditional comments at our disposal.

Moving Beyond SSH: Securing Our Server

Published on Mar 6, 2009   //  Development

Moving Beyond SSH

This week we’ll be going over a very important step to setting up our servers, securing it. Everyday, servers are getting hacked due to the lack of security measures on them. This is a fairly easy thing to do, and you don’t have to worry about it after you’ve set it up.

SSH Port

One of the easiest things we can do to secure our server, is to change the port SSH runs on. Because SSH attacks are always targeted at the default SSH port (22), we can avoid any such attacks by simply having it run on a different port. You’ll need a choose a port that has not been assigned to any service yet (to avoid conflicts). Check the list of port assignments for unassigned ports, you can use one of these for your SSH port. After you’ve chosen your port (you should probably write it down, just in case you forget), you can change it by editing the /etc/ssh/sshd_config file and changing:

#Port 22

to

Port 00000

Where 00000 is the port you chose. Save the file and exit. Then we need to restart the SSH service for the changes to take affect:

service sshd reload

You will probably be logged out of SSH. From now on, you’ll need to login to SSH using the port you chose. If you’re using PuTTY, there’s a box you can change this in, if you’re using Terminal in Linux or OS X, you can add the -p parameter to specify your port.

Software Firewall

Installing a software firewall will protect our server from other types of attacks. It allows you to close any ports that aren’t in use, and will even send out email alerts when certain events happen (like whenever a root user logs into SSH). For our software firewall, we’ll be using the ConfigServer Security and Firewall. ConfigServer will allow us to automatically block IP addresses attempting to hack our server, close ports, monitor SSH logins and monitor suspicious activities on our server. It also has a convenient Webmin module, which will allow you to configure it from within Webmin.

Installing ConfigServer is simple, just run the following commands in SSH:

wget http://www.configserver.com/free/csf.tgz
tar -xzf csf.tgz
cd csf
sh install.sh
perl /etc/csf/csftest.pl

Next, login to Webmin and navigate to Webmin > Webmin Configuration > Webmin Modules. Choose to install from a local file, and enter /etc/csf/csfwebmin.tgz into the textbox. Then click the Install Module button. Now, navigate to the ConfigServer configuration at System > ConfigServer Security & Firewall.

Click on Firewall Allow IPs and ensure your personal IP address (not your server’s) is listed in the file. This will ensure that your IP address does not get banned (trust me, I’ve been there, you don’t want it to happen to you).

Now, click on Firewall Configuration. ConfigServer has tons of different configuration options, but we’re only going to cover some basic ones. Feel free to read the documentation and change any other options as you see fit (be careful though, you don’t want to block people who aren’t doing anything wrong). Change the following settings:

  • TCP_IN = 53,80,443,10000
    (also add your custom SSH port here – don’t forget the comma)
  • TCP_OUT = 25,43,53,80,443,10000
    (also add your custom SSH port here – don’t forget the comma)
  • UDP_IN = 53
  • UDP_OUT = 53,113,123

Click the Change button at the bottom of the page. Then click the Restart csf+lfd button. Check to make sure you can still access your web server, Webmin and SSH. If you can, click on Firewall Configuration again, change TESTING to 0, click Change and the click on Restart csf+lfd.

Conclusion

We have now completed the basic setup of our server. You should now have a LAMP (Linux, Apache, MySQL, PHP) stack installed on your secured server, ready for production use! You can now, safely, begin to use your server as a web server. Next week we’ll be going over how you can transfer files onto your server. If you have any suggestions for future articles, please post them in the comments.

Page 19 of 42« First...10...1718192021...3040...Last »