Blog

  • PHP: Get Page URLs / Directoy Listings

    A simple function to easily process a website or file and return an array of all the URLs found. The main purpose I created this was to make it easy to process “Directory Listings Pages” which is a page which shows all the files and folders in a websites directory if there inst a default/index page or the directory listings is enabled.

    I have used it to find images on another website (which has directory listings enabled) and display the found URLs / Images on another website. It it up to you how you use it but I am not sure what its limit are in terms of URLs per page.

  • PHP: Compare floats & why its different to a normal comparison

    Comparing two bits of data in PHP is quite simple but there are special occasions when a simple comparison of data needs to be slightly different. One of the common issues is comparing float numbers, a common mistake is comparing two float numbers the same way in which you would compare two integers or two strings.

    What does float, double or real number look like?
    1.234
    1.2e4
    7E-10

    This is due to how PHP stores float numbers (also known as doubles or real numbers) which gives this type of data a very limited precision resulting in numbers being slightly off, for a more in-depth explanation please read the following article http://php.net/manual/en/language.types.float.php.

    This issue only occurs once a calculation is performed on a float number.

    The best way of comparing two floats to get an accurate comparison is to convert the value to an alternative data type such as a integer, string or use “bccomp” which allows you to compare 2 float data types together up to the precision you specify (bccomp can also be used to determine which of the two floats is higher or lower).

    Read up on bccomp: http://php.net/manual/en/function.bccomp.php

    Is Match Comparison

    Greater or Lower Comparison

  • SU (Switch User) and Screen workaround

    Screen is useful tool but it does have some slight bugs such as when you use the SU command to switch to another user and attempt to use screen you will most probably receive a error message similar to the following. (more…)

  • Pinning Eclipse to the taskbar with Windows 7

    Eclipse is a brilliant programming interface for many languages but when using a Windows 7 based PC you may have issues when pinning the shortcut to the task bar. As you can see below when I launch eclipse via the shortcut instead of using the pinned icon it creates a new one.

    If you right click the active icon you do not get a “Pin this program to taskbar” option like you would with other programs. Eclipse on the left and Microsoft Word on the right.

    All we need to do is add 2 simple lines to our Eclipse configuration file so that Java is detected correctly which then enables us to pin the application to the task bar. First we need to go to the directory where we have placed the Eclipse files, in my case this is “C:/Program Files/eclipse” but this will differ between everyone.

    If you can’t find the files try right clicking any shortcuts you have and select properties that will tell you where the actual files are.

    There is a file called “eclipse.ini” open it in Notepad it should look something like this.

    Add the following to the top of the file and save it. The second line needs to point to your Java installation bin directory; this may differ on your computer.

    You can normally find all the Java installations in the following location “C:\Program Files\Java\”, if you have a older or new version of java than me or using the JDK version then the name may differ from”jre7”.

    You can now start Eclipse and right click the icon on the task bar where you will now see the “Pin this program to taskbar” and when you open Eclipse it will no longer create a new icon.

    If you receive the following error when trying to start Eclipse then you have provided a invalid file location for Java. Try the other locations or in my case it was due to me pointing it to the 32bit version instead of the 64bit version (Program Files (x86) instead of Program Files).

  • Redirect requests for files & folders which dont exist with .htaccess

    Normally if a visitor requests a file or folder which doesn’t exist your web server will respond with a 404 file not found error message, this isn’t the best looking error message and so it’s common to setup a redirect so instead of the web server showing the default page you can use a custom page which normally fits in with your websites template.

    The htaccess is a powerful file and any modifications can stop a website from working correctly. If you receive 500 error messages after modifications you have done something wrong and should revert to a backup.

    For example if you go to the following URL on my website you will receive a 404 error message but it is within the normally style of my website, if the redirect wasn’t present you would receive an empty white page with text telling you the file wasn’t found.

    http://shanerutter.co.uk/moo

    We will be using the .htaccess file which is part of the Apache web server to setup this redirect. You can read about this file here.

    http://shanerutter.co.uk/2011/07/31/apache-htaccess-file/

    HTaccess

    The follow code needs to be placed into your .htaccess file there is two versions of this code please see the differences below. These redirects will only occur if the requests file or folder doesn’t exist. Simply replace http://newdomain.com with the domain you want to redirect to.

    Example 1

    Domain: http://origionaldomain.com/moo
    Redirects to: http://newdomain.com/moo

    Example 2

    Domain: http://origionaldomain.com/moo
    Redirects to: http://newdomain.com

    Extra

    The two examples above require specifying the domain you wish to redirect to but in some occurrences you may not want this especially if you’re installing on multiple domains as the two examples above will need adjusted on a per domain basis to work correctly. The following is the same as example 2 but it redirects to the websites root directory and so you don’t have to specify a domain.

    For example this will redirect any requests to http://domain.com/my_fake_file.txt to http://domain.com/ as long as my_fake_file.txt doesn’t exist.

    Remove Query String

    It has come to my attention that the redirects will still contain the query string so a request to http://mydomain.com/fake_file.php?action=123 would redirect to http://mydomain.com/action=123, to remove the query string simply add a ? to the end of the redirect location. Below is an example using the example code above.

  • CSS & Javascript Minify / Compression

    Optimization is a key part of any website we need to keep it loading quickly for the sake of keeping visitors and also for lowering server stress. With internet connections becoming faster most developers skip optimization but mobile broadband and phones are growing rapidly which still have a relatively slow connection when compared to the common household internet connection.

    Even if a website has a quick loading speed why not try to shave of half a second loading speeds it might not seem allot but it is noticeable especially on a mobile connection.

    Minify (also known as compacting or compression but its not true compression) is a method used to lower the file size of text based files by removing unneeded white space, line breaks, comments and placing the code as compact together without disruption functionality. This can turn those 100KB files into 2KB files and it’s the simplest thing to do it could take your 5 minutes.

    CSS Compression

    I use the following website to compress my CSS files its simple and quick to use.

    http://www.csscompressor.com/

    You simply copy your CSS into the big text field and selection the options you want for best compression I would suggest ticking all boxes and changing compression mode to greatest. Here is a comparison of my websites main style.css file on the left we have the uncompressed version and then the compressed version.

    Its only 12,310 bytes difference but that’s now 12,310 bytes less each of my visitors need to download, 12,310 bytes less bandwidth I use each time then download this file and a speed increase especially for mobile devices.

    JavaScript Compression

    I use the following website to compress all my JavaScript files its simple and quick to use just like the css compression website.

    http://javascriptcompressor.com/

    This works very similar to the CSS compression website you simply paste your JavaScript into the top text field click compress and then your new code appears bellow. There isn’t any options but it does exactly what I need which is strip all white spaces, line breaks, comments and compact it together. Here is an example of one of my Javascript files, uncompressed on the left and compressed on the right.

    As you can see there is a 12,000 bytes difference.

    Issues

    The only issues I have found is due to programming errors such as missing “;” characters, due to all the code being compressed onto one line you must make sure that at the end of all CSS and JavaScript lines you have a “;” character which you should be doing already.

    You should always keep a copy of the uncompressed file but for those who lose this file you can always uncompress it using the following websites.

    http://www.javascriptbeautifier.com/

    http://mabblog.com/cssoptimizer/uncompress.html

  • Capitalise the first letter of a group of words quickly & easily

    Sometimes as a web developer I have to capitalise the first letter of each word normally for title and header tags but I don’t want to have to sit there manually capitalising each letter. HTML and CSS don’t have a native feature which allows you to quickly capitalise the first letter automatically so we have to look at using JavaScript to achieve this.

    This example uses jQuery a JavaScript library which extends the capabilities of JavaScript.

    It is best to place this code in the <head></head> tags after the jQuery library include link.

    The following code will capitalise the first letter of each word which is in a header tag. A header tag looks like the following h1, h2, h3, h4, h5 and h6.

    You can change the elements which are capitalised by adjusting the following line.

    If you want to capitalise the first letter of each word within a “h1” tag change it to the following.

    If you want to capitalise the first letter of each word which is within a element with a class of “title” change it to the following.

  • jQuery: Using prototype and jquery together

    JavaScript is a powerful tool for a website but to extend the capabilities and functionality extra libraries can be included, the most commonly used libraries are jQuery and Prototype. Normally a website will choose either of these libraries but sometimes both libraries are required, if this is the case a small change needs to be performed to stop the two libraries conflicting.

    Both jQuery and Prototype try to bind to the $ character for easy and quick referencing but due to this bind both libraries conflict leading to one loading incorrectly.

    Resolving the conflict

    Jquery has a noConflicts() function which when used stops it binding to the normal “$” character we can still access jQuery simply by replacing “$” with jQuery. This is how we call the noConflicts() function.

    It must be placed straight after the line of code which includes the jQuery library. After that the following code will no longer work due to jQuery not binding to “$” but this will resolve all compatibility issues between the two libraries.

    The above can still be achieved by using the following instead.

    You can read into this more from the official documentation found at:
    http://api.jquery.com/jQuery.noConflict/

    Bind jQuery to another character

    Typing jQuery every time we want to reference jQuery can be long winded so we can rebind jQuery to another variable for quick and easy referencing. The following will bind jQuery to “$j”.

    Now we can type the commands used above like so

    It has come to my attention that if call the noConflicts function and assign the output to a variable it will perform both the unbinding and new binding in one step.

    The example above will unbind from the normal “$” character and rebind to the new character which in this case is “j”.

    I would recommend that when you include the jQuery library files that you called the noConflicts() function immediately after and then perform your binding.

    Extra / Advanced

    Unbinding and rebinding is probably the easiest but it’s still possible to use the “$” character even when you have unbound jQuery using the noConflict function. JQuery offers the ability to continue using the “$” character in specific instances normally with in a function or call back, this allows us to continue using “$” for all are jQuery referencing at a specific point but it will not cause conflicts between other libraries. Below is an example of code which allow you to continue using “$”.

    By providing the “$” character in the function arguments we tell jQuery that we want to temporally bind jQuery to “$” but only for the code during this function and straight after jQuery lets go of the binding and the other libraries doesn’t even know. Here is more examples.

    Conclusion

    It may not be the solution you’re looking for but it works, I prefer jQuery so would like to rebind Prototype and leave jQuery bound to “$” but Prototype doesn’t have this ability so the only solution to using both of the libraries is to rebind jQuery.

    After writing this I found out about the ability to temporarily continue using jQuery alias “$” within certain instances, I have update this post and you can read these changes in the Extra / Advanced section. Using these methods gives you all your normal operation of jQuery if used correctly.

  • MySQL: Insert else Update

    Ever had to check if a record exists using SELECT then perform an INSERT or UPDATE based on the result? Why not combine both the INSERT and UPDATE into one command and remove the SELECT query checking with one easy MySQL feature. Instead of performing two or three queries we can perform just one which will check and perform an INSERT or UPDATE.

    Remember performing one query is normally faster than performing multiple queries which is a common mistake allot of programmers make. MySQL is heavily optimised for data processing so it’s best to do as much processing on the database side as possible as you will see a significant performance increase.

    Query

    The example above will try to insert a new user (Shane) into the users table, the username field is a UNIQUE field and so only one occurrence of the username “Shane” can be used. If there is already a username which matches “Shane” it will skip the INSERT and attempt to UPDATE the field “last_login” with the current date/time where the username is “Shane”.

  • Apache: Domain rewrite / redirect with mod_rewrite

    There are multiple ways to redirect a domain to another domain but in this post we will be using mod_rewrite rules which are normally specified in the .htaccess file (you can learn about this file in this post). There are other redirect methods but these are past the scope of this post and will be covered in future posts.

    Mod_Rewrite is an Apache module and must be installed and activated for these commands to work; specifying these rules without this module will lead to a 500 server error message being displayed.

    Rule 1 (Is Match)
    This rule will check if the HTTP_HOST matches and then redirect if required, for example the first will redirect any requests to the following URLS to http://shanerutter.co.uk/.

    • shanerutter.com
    • www.shanerutter.com
    • shanerutter.net
    • www.shanerutter.net
    • shanerutter.co.cc
    • www.shanerutter.co.cc

    Let’s take this example Bob enters the following URL into his browser http://shanerutter.com/music/index.html the web server will detect that Bob’s request matches a rule and will redirect Bob to http://shanerutter.co.uk/music/index.html. But if Bob was to enter shanerutter.org the domain will stay as .org as there is no rule / condition specified for that domain.

    Rule 2 (Is Not Match)
    The following will check the HTTP_HOST to see if it doesn’t match the required URL the main difference to the above redirect is that this condition will redirect all requests to http://shanerutter.co.uk if the entered URL doesn’t match the condition URL which in this case is http://shanerutter.co.uk.

    All we do is simply place a ! character at the beginning of the condition which means IS NOT, so instead of the condition checking if the provided domains IS a match it now checks if it IS NOT a match.