Apache Logo

Back in the early days of DDOS attacks this would have been a highly dangerous tool. Thankfully it is very easy to block a basic attack like this, so i don’t see the risk of explaining how to use this method. Apache has a tool built into it that will allow you to send a predefined number of requests to a website in order to see if it can handle the load. Here is what you need to do to benchmark a website using Apache.

Apache has a fantastic benchmark tool that you can use to check the performance of your website. If you are expecting a flood of traffic for some particular reason, it would be good to know if your server is capable of handling such a flood. This tool will give you a good idea on whether the server is going to be able to handle the load of traffic.

The format of the command is very simple. You can run it on any server that is using apache. Since most web servers that run apache, will be using some flavor of Linux, i will give you the linux command line method of performing this. The application is called ApacheBench. It may already be installed, but if it isn’t it should be easy to install it.

Once you have it installed you can run a quick test to see how your server handles it. The format of the request is very simple. The first number is the total number of requests you want to send and the second is the amount of requests you want to send at the same time (concurrently). The concurrent value is the most important as this is the one that is most likely to crash the server if it receives too many requests at once. You can play around with the values as you please.

The following command will send a total of 1000 requests to a single URL by grouping them in sets of 100 requests at a time. Make sure to include the “/” at the end of the website path.

If i run the above command against this website I get the following results.

 

Email

When you have obtained a list of emails from a location that required little to no validation on whether it was a real email, you will be stuck trying to determine if the email address is real or not. You don’t want to risk sending out an email to these users without checking as a regular high bounce rate is a quick way to get your email server blacklisted. There is a 2 step method that you can use to validate if an email address is valid or not. This assumes that you have first filtered out values that are missing an @ symbol and a domain. This guide will show you how to check if an email is valid. For example, how can you tell if john@somesite.com is real or fake?

Step 1

The first thing you will need to do is check if the domain name is valid and has an active mail server/ MX record associated with it. Sometimes an email may have been valid at one stage, but the website has now been shut down. Sending an email to this address wont do anything. By checking to see if the domain name is real you will be able to filter out people who provide stupid domain names that never existed and also filter out emails from valid websites that are not capable of receiving emails.

For the example I am going to use PHP to write the script for this. Many other languages have similar methods that do the same thing, so this should be fairly easy to do with other programming languages. PHP has a function called “getmxrr()”. This function will obtain the MX record for a domain. For those who do not know what this is, a MX record is used in the DNS settings to point to the IP of a domains email server. If one is missing then the domain is not capable of receiving an email and is therefore invalid.

Just because a domain has an MX record, does not mean that the email address is valid. In fact, this makes sending bad emails to this server even more likely to cause you to get blacklisted.

Step 2

This is the most difficult to test while also being the most important. If someone provides an email like asdasd@gmail.com, step 1 will return this as being a valid email address. gmail.com is a valid email domain, but asdasd is likely a non existent user. This step will allow you to determine whether this is a valid inbox or not. Keep in mind that this step requires you to directly contact the email server to essentially ask if the inbox exists. I would suggest you run this from a test machine so you do not run the risk of blacklisting the IP. This many requests in a short period might be considered suspicious.

If you have worked with mail servers in the past, you may be familiar with HELO. This can be used to easily check if a mailbox exists or not. If you send the command and get a positive response you know that this inbox exists. If not you know its fake. I have combined step 1 with step 2 to generate a complete script below that will allow you to check if an email is valid and filter out bad mailboxes.

 

Setting up a redirect using Apache is quite simple, but getting it to work with dynamic URLs can be a lot more difficult. Lets say you want to redirect all traffic from one file to another file in a different location/server. How do you perform this redirect while keeping all of the url variables intact? I have seen some many answers from people that suggest you use a mod rewrite matching method that has all sorts of complicated matching parameters that make it almost impossible to convert that answer into a solution that works for you. This solution is far more simple and will work for most instances without needing to be heavily modified.

In order to setup this redirect you will need to add some code to the .htaccess file of your website. This file can usually be found in the root directory of your website. It is more efficient to use your htaccess file when compared to using PHP. When you use PHP, Apache will need to call it and then PHP does the work. It might be easier to work with PHP, but doing the job with Apache cuts out the work the server has to do. The following piece of code will perform a simple redirect from a file in one server to a file on a different server/subdomain.

It’s as simple as that. By putting the $1 at the end, it is telling apache to match a url that starts “script1.php” and treat everything after this as a variable, when performing the redirect, put all of this content at the end of the new URL. If your files are inside a sub folder you can easily add this onto the first part of the redirect match.

This is a much cleaner and simple method of performing a redirect for a single file while keeping all of the URL variables in place.

Sometimes you get hit with a load capacity issue that you may have not expected. For example, you have created a site with multiple scripts and these scripts are now being accessed so much that the one server isn’t able to cope with all of the traffic. Rather than spending a fortune setting up fancy hadoop cluster, you can simply setup subdomains. For example if you moved http://website.com/scripts/script1.php to http://scriptserver1.website.com/scripts/script1.php you could better balance the load across multiple servers and use DNS records to change where it is pointing. Setting the subdomains up is simple, but the problem is how do you setup a redirect on the old server that will push all traffic to the new url? You could use PHP, but why not let Apache do the job instead of bringing PHP into the mix.

Using the .htaccess file in the root directory of your website you can make Apache redirect

If you are using a script that required some extra variables you can instead use the RedirectMatch method instead. This will allow you to perform the same redirect above where you will redirect a file from one location and send it to a file in a different location. The only difference here is that any url variables will be passed along with the redirect. For example, if your original URL contained something like script1.php?foo=123&bar=123454. All of the information after the ? would also be passed along to the new script.

 

Sharepoint Logo

This tutorial will show you how to programmatically create a subsite in Sharepoint. This can be done a few different ways and if you have server access you may prefer to do this on the server side, but you can just as easily create one using the Client-Side Object Model.

You can do this using a standard C# console application. For this example I used Visual Studio 2013 with .NET 4.0. You will need to include 2 assemblies in order for the code to work.

If this code throws an error then you will need to add the reference to your project. For me, I was not able to find Microsoft.Sharepoint but when I added the .client assembly it worked.

Thats it! The code above is all you need to create a new subsite. If you make a mistake and want to change it, you can also do this quite easy using similar code. You can modify pretty much everything on a subsite, but the URL becomes read only so you cannot change this once it has been set.

This is all you need to be able to add a subsite and to be able to edit it.

Wordpress Logo

If you use a large amount of categories on your WordPress site, you will find that things get very very messy with a huge amount of categories showing up in the input box. One brilliant way to resolve this is to use a plugin called Intuitive Category Checklist. The good side, is the plugin creates a collapsible tree for all of your categories, the bad side is that you cannot add new categories when using the newer versions of wordpress. The plugin has been abandoned for over 2 years now, so its unlikely that we are going to get an update for this. We are going to have ot take matters into our own hands to maintain this awesome plugin!

Before making any changes to the live file, please make a backup in the event that something goes wrong. Go to your plugin directory and open up the file called “intuative-category-checklist.php” inside the plugin folder. Replace the entire contents of the file with the code below and it should resolve the issue with not being able to add new categories in the edit page.

Adding new categories, will show up at the top of the category box. While they do appear to have ignored the hierarchy of the categories, everything is fine. Its a visual issue, if you refresh the page everything will show up correctly. If anyone wants to work on a fix for this let me know in the comments below and i will update this post.

 

cloudflare-logo

When using Cloudflare, you will notice that the REMOTE_ADDR variable no longer displays the correct user IP. Since Cloudflare acts as a proxy, the IP will always be a Cloudflare IP. If you have any code such as user votes that are using the IP as a unique way to stop users voting multiple times, you will find that this code is no longer working correctly. The solution to this is to install the Apache module which will correctly resolve the IPs that are being sent from Cloudflare. There is currently no module for Centos 7 and a few other operating systems which leaves us stuck. I looked into the issue and found that there is already a way to access the users IP without installing anything.

If you are using a language like PHP, dump out the server variables using a command such as var_dump($_SERVER); This will allow you to see all of the server variables that you currently have access to. You will see that there are a few new variables that are not normally there. The important variable here is HTTP_CF_CONNECTING_IP. This variable contains the actual user IP that would normally have been present in the REMOTE_ADDR variable. You will also find that the HTTP_X_FORWARDED_FOR variable also contains the exact same value.

This is a quick solution for anyone writing code that is depending on the IP being correct. If you are using any software that you didnt write, you might still be in trouble as this is difficult to change.

command line

Installing can be quite simple to do and also very dangerous as you install something, it completely wrecks apache and now your web server wont start back up again. Well worry not, its very easy to uninstall anything that you have installed. The first thing you need to know is that the name of the package changes from the name of the original file that you downloaded and installed. You will first need to perform a search of everything that is installed to get the name of the package you are looking for. Using grep will make this way easier.

This will generate a massive list of all of the packages installed on your system if you run it without the grep. Once you have found the name of the package you want to install run the following command to uninstall an RPM package from your system.

If the RPM that you installed was part of another application such as an apache module, make sure to run the command to restart the application in case it has crashed with the install of the RPM.

Thankfully this is very easy to perform. You can begin to get over the mini heart attack you had when your server went down now 🙂

Wordpress Logo

Most guides on how to add a custom PHP page in WordPress will explain how to do it using a template in a theme, but this is a restrictive method of adding a custom PHP page to WordPress. It’s actually quite easy, perhaps slightly inefficient, to dynamically create a custom template for a WordPress theme. If you add a custom PHP page to wordpress using a plugin you only need to write the code once and it works for every theme.

The main goal here is to create a PHP page where you can generate any content you like. For this I wanted to pull data from a database outside of WordPress and I didn’t want to create a theme template for dozens of themes. By adding it in the plugin level you can create the page once and call the theme to render the content. There are some rules that you need to follow in order to do this and there may be some further tweaks that need to be made to cover all kinds of themes. I have tested this and it works with all of the themes that I have tested. Some of the steps here might seem unneeded, but I had to add them in order to make the code work with all themes that i tested with.

 

The first thing you need to do is have a plugin to use. Create a file in this plugin called test.php. The page does not need to be a part of the plugin at all, it just needs to be in the plugin directory so you can easily move it around to other WordPress installs. By default your theme will have a default query. Some themes will just pull a random post and others will generate a post feed. There is no way to prevent this, so instead we need to override the global variables to change it. This is the part that’s a little inefficient, but as you probably know, breaking the WordPress loop isn’t something you want to dig into.

The code below will call the blog header. This will start the loop and leave you with some global variables that contain data that your theme will then display. You need to override this data with your own custom data. This sort of acts like data injection. If anything this is like a custom hook. Once you get the global variables needed, you can do whatever you like with them.

If we override all of the relevant data within these variables we can let our theme do all the hard work of rendering the content correctly. This allows us to produce content that matches the look and feel of our website without having to write css for each theme we want to support. To see what is inside each variable you can use a var_dump();

The first thing and most obvious is to override the $post variable. Put whatever you want inside these variables. Hard code them, pull them from an API or get the data from a different DB. Doesnt matter where it comes from, so long as it overrides the existing data.

We have now overridden the global $post variable with our own custom content. This is normally enough for most themes. The theme will display this post and you wont need to do any additional work. Unfortunately the default method of displaying a post, which a lot of themes use, will first print this post and then also display a bunch of other recent posts.

Some themes will use the $wp_query objects via the have_posts() and get_post() methods. This will get the content for $post from $wp_query. We are going to have to override this variable if we want to stop the list of posts from displaying. The following code will override any arrays and variables that contain multiple objects.

This should be everything that you need to make themes display the posts correctly. If you find there are themes that need extra work please post in the comments and i will update the post.

The final thing to do, now that we have armed all of the global variables, is to call the default page.php to display our content. We want this to be dynamic so we will get the current theme directory from WordPress. All themes will contain a page.php, so we can be confident that this will work for us.

All of the code above will allow you to add a custom php page to WordPress using a plugin that is completely dynamic. You will not have to modify a theme at all, this code will handle everything through the plugin. Please post any issues or suggestions below so I can make this post as efficient as possible.

I found it quite odd to discover that there was no built in method to count the total amount of lines for a project in Visual Studio 2013. I did some searching and found that pretty much every guide was saying download this application or install this plugin in order to get the code metrics. This is a big waste when all you want to see is what the total lines of code in your solution is. Thankfully there is a really simple solution to find out how many lines of code there are in your project. Here is how to get the total line count in visual studio 2013.

Open up your project solution and press CTRL + SHIFT + F. This will open the typical find and replace / search window. Go down to the “Find Options” field and open the drop down. Go down and select the “Use Regular Expressions” box. This will allow you to perform a search using regex. In the “Find what:” box, enter the following value.

Click the “Find All” button and Visual Studio will begin counting all of the lines of code. The more lines the longer this will take, but once it has been completed the results will be displayed in the console window. Scroll down to the very bottom to see the results. The final line should look something like this.