Articles

Set An IP Address From The Command Prompt In Windows

Rather than use the old connection properties dialog in Windows you can open up a command prompt and use the netsh to set up all sorts of network specific settings. The most useful part of this is that you can create a bat file that will allow you to quickly change your local IP address very quickly.

To see a list of the network connections available you can use the following command.

netsh interface show interface

This runs the netsh program, in the interface context, and shows the interfaces available. There are lots of other contexts to chose from, just type netsh and then ? to see a list of commands. If you type in netsh and hit enter key you will see the prompt change to netsh meaning that you are in that program mode. Just type exit or bye to exit.

You can also ask for information about individual interfaces by using the name parameter.

Randomising The Middle Of Words In PHP

I was sent an email the other day that contained some text were the start and end letter of each word were left alone, but the middle of each word was randomized. The weird part was that the text was still readable, which is due to the way in which the brain processes words.

I wondered if I could replicate this using a PHP script. All I would need to do is split apart the sentence into the component words and loop through those words, randomizing the middle of them. Clearly, it is not possible to mix up the order of letters in a word less than four characters long so a check would be needed for this. This is what I cam up with:

Generate Password Function In PHP

I have talked about generating random passwords before. Although that function generated some nice passwords, they perhaps aren't as unique as they ought to be.

This function, take from Webtoolkit creates passwords of different length with varying levels of complexity.

Create Images Thumbnails And Cache Them In PHP

Creating image thumbnails is a pretty common practice, and there are a few scripts available that allow you to do this in PHP using the GD2 library. However, they are normally overkill for what should be a simple task, so after a bit of searching and testing I found the following ImageResize class, which is taken from http://shiege.com/scripts/thumbnail/. I have modified the code to be PHP5, but if you want the PHP4 version then you can get it from the site.

Creating A Simple Widget In Wordpress

A widget is a little program that fits into the side menu of your site. These widgets can be moved around using the admin section of your Wordpress blog and there are quite a few to chose from with a default install.

To create a theme that supports widgets you can follow the instructions in the post creating a widget proof Wordpress theme.

You can create a widget in one of two places, either within your functions.php file of your template, or in a plugin. To get a widget to display you need to call a function called register_sidebar_widget(). This function takes two parameters, the name of the widget in the admin section, and a callback function that controls what the widget contains.

Get The IP Address Of A Visitor Through PHP

I have talked previously about getting an IP address of a visitor with PHP. The failing in using the value of $_SERVER["REMOTE_ADDR"] is that if the visitor is using a proxy then you will get the proxy IP address and not the visitors real IP address.

This function works by going through any variables in the $_SERVER array that might exist that would contain information to do with IP addresses. If they are all empty then the function finally looks at $_SERVER["REMOTE_ADDR"] value and returns this as a default.

Edit Any Web Page With JavaScript

If you want to directly edit any page that you are looking at then copy and paste the following JavaScript into your address bar.

javascript:document.body.contentEditable='true' document.designMode='on' void 0

I have created a handy little bookmarklet that you can use to do the same thing.

Edit

This code has been tested in IE7, Firefox and Chrome and works well in all three.

Of course, it goes without saying that you don't actually edit the site, just the content that you see on the screen. Hitting refresh will remove any editing that you have done. This little tool is ideal if you want to see what different content would look like on the page without having to edit HTML. It can also be used to create spoof pages with false content.

Moving Files In PHP

I have been asked a couple of times recently if PHP has a function that will move a file, so I thought I would put the solution here.

PHP has a function called rename() which can be used to rename a file, but because of the way it works it can be used to move files. Let's say that you wanted to rename a file called test.txt to test_back.txt, this can be accomplished by doing the following.

rename("test.txt", "test_back.txt");

However, if you want to move the file from one directory to another you can do the following.

rename("test.txt", "backups/test_back.txt");

The return value of rename() is boolean that tells you if the rename was successful or not, this can be used to error check like this.

Using Simple Input Detailed Ouput Principles With Web Forms

If you have a web site then the chances are that there will be a form of some kind on there somewhere. This might be a search box, or a contact form or even a tool. There is one thing that should be followed no matter what sort of form you create and this is the rule of Simple Input Detailed Output, or SIDO for short.

The idea behind SIDO is that you get the user to enter the absolute minimum amount of information when filling out a form, but once complete give them as much information as you can in return. The perfect form should have a single input box, and return at least a page of information from this single starting point.

Recursive chmod Function In PHP

File permissions are important, especially if you want to let a user agent view a file. If the file doesn't have the correct permissions then it will not be accessed and could cause your script to fail. To get around this you might want to use the following function. It uses the PHP function chmod(), which sets permissions, but it does this recursively from wherever you set it off from.