Batch delete temporary files in Windows
As the name suggests temporary files are created by software application for storing data for a short time or perform a specific function and then delete it after its use. Generally, these files are auto deleted by the software that had created it but many times such files are not deleted either due to application glitch or due to logging purposes. Such temporary files keep accumulating on your Windows operating system and consume unnecessary disk space.
Mainly temporary files are stored either in Windows temp folder that is located at C:\Windows\temp or in your user profile temp folder at C:\Documents and Settings\Username\Local Settings\Temp where Username is your windows login username.
We will now use a MS-DOS Batch file to automate our task of clearing up the temporary files and a command line tool called Deltree (Delete Tree) that deletes a directory and all of the files and folders contained within it.
deltree /Y "%SystemRoot%\temp\*.*" deltree /Y "%USERPROFILE%\locals~1\temp\*.*"
There are many files and folders on Windows system where program specific temporary files are stored. If you also wish to delete those files and directories then here are the command lines that you need to add to the above batch file.
REM - Delete Recent Documents deltree /Y "%USERPROFILE%\recent\*.*" REM - Delete Temporary Internet Files deltree /Y "%USERPROFILE%\locals~1\tempor~1\*.*"
Download
File: Delete Temporary Files
Downloads: 232
Note: Deltree was only available on MS-DOS based Windows Operating Systems (95, 98, Me) and is not present in Windows NT based systems (NT, 2000, XP, Vista). Therefore, the download file contains the Deltree command tool along with the batch file.
Usage
Manual Cleanup: You can run the batch file manually by double clicking on it and it will delete all the temporary files on your windows system.
Automated/Scheduled Cleanup: You can use Schedule Task to automate the deleting of the temporary files on your system. Just set the program path to the batch file and then schedule it according to your needs. For standard users the recommended schedule setting is "When my computer starts".
References:
Convert string to Title Case in JavaScript
Capitalization is generally used in styling titles and headlines for catching attention to those important parts of the site contents. Title Case is a form of capitalization where first letter of word is written in upper case letter and remaining letters in lower case letters.
JavaScript do not have any native function to convert string into Title Case. Therefore, we will use toUpperCase and toLowerCase functions along with Regular Expression to create a custom function that will convert a given string to Title Case string.
function toTitleCase(str)
{
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
Usage
You can use this custom Title Case function in converting string values from form fields, titles, headlines, etc.
document.write(toTitleCase('this is a title case string'));
// Output: This Is A Title Case String
form.fieldname.value = toTitleCase(form.fieldname.value);
Calculate time difference between timestamps in PHP
There are instances when you need to calculate and show some time difference on your web application like how long the user has been on site, time since last visit, session timeouts, birth date differences, etc.
Here is a PHP time calculation function that will calculate the difference between two time values in UNIX timestamps. The first parameter is the start time and the second parameter is the end time, which is optional and defaults to current time if not specified.
function timeDifference($startTime, $endTime = false)
{
$endTime = $endTime ? $endTime : time();
if ($endTime > $startTime)
{
$diff = $endTime - $startTime;
$hours = floor($diff/3600);
$diff = $diff % 3600;
$minutes = floor($diff/60);
$diff = $diff % 60;
$seconds = $diff;
return str_pad($hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($minutes, 2, '0', STR_PAD_LEFT) . ':' . str_pad($seconds, 2, '0', STR_PAD_LEFT);
}
else
{
return 'Error: Start time should be less than end time!';
}
}
Usage
If you do not have your start and end time values in timestamp format then you can use strtotime to convert any textual time description into UNIX timestamp.
print timeDifference(strtotime('-1 hour 2 minutes 30 seconds'));
// Output: 00:57:30
print timeDifference(strtotime('10:45'), strtotime('22:30'));
// Output: 11:45:00
print timeDifference('1195924617', '1195925357');
// Output: 00:12:20
References:
Apache mod_rewrite logging with RewriteLog
mod_rewrite is one of the most powerful modules of Apache web server and if not used correctly then it can cause havoc on your website and harm your search engine rankings. A simple looking rewrite rule goes through a lot of complicated rewrite engine that does the ruleset processing on your rewrite conditions and regular expressions against the requested URL.
There is a directive named RewriteLog that can help us to understand how all this process is been done and we can interpret it to effectively use our rewrite rules and regular expressions in mod_rewrite.
Enabling RewriteLog
Warning: Please note that this directive is only allowed in server configuration file (httpd.conf). That means you need to have a root access to your web server to edit this file that is only available in VPS and dedicated servers. You cannot place this directive in .htaccess file for shared hosting accounts. It is also advisable to disable this directive as soon as you are done troubleshooting with it as it can create very huge log files on disk and a drastic server load on Apache web server.
To enable RewriteLog directive open your httpd.conf file and locate the VirtualHost container for the specified domain on which you want to enable this directive and add the following lines to it.
<VirtualHost 192.168.1.1>
...
RewriteLog "logs/rewrite.txt"
RewriteLogLevel 9
</VirtualHost>
RewriteLog directive sets the log file name to which the server logs all the rewrite actions and the RewriteLogLevel sets the verbosity level of that log file. If the log file name does not start with "/" then it is assumed to be relative to the Server Root directory. The maximum log level is "9″ which will log almost all the rewriting actions and setting it to "0″ will simply disable the rewrite logging actions.
After enabling RewriteLog directive, it will start logging all the internal rewrite actions to the specified log file including direct and sub requests. Here are some sample log entries from the log file hosting a WordPress blog.
[localhost.com/sid#6b6ff8][rid#acd0c8/initial] (3) [per-dir C:/wordpress/] add path info postfix: C:/wordpress/about -> C:/wordpress/about/ [localhost.com/sid#6b6ff8][rid#acd0c8/initial] (3) [per-dir C:/wordpress/] strip per-dir prefix: C:/wordpress/about/ -> about/ [localhost.com/sid#6b6ff8][rid#acd0c8/initial] (3) [per-dir C:/wordpress/] applying pattern '(^blog/downloads/[^/]+$)' to uri 'about/' [localhost.com/sid#6b6ff8][rid#acd0c8/initial] (3) [per-dir C:/wordpress/] add path info postfix: C:/wordpress/about -> C:/wordpress/about/ [localhost.com/sid#6b6ff8][rid#acd0c8/initial] (3) [per-dir C:/wordpress/] strip per-dir prefix: C:/wordpress/about/ -> about/ [localhost.com/sid#6b6ff8][rid#acd0c8/initial] (3) [per-dir C:/wordpress/] applying pattern '.' to uri 'about/' [localhost.com/sid#6b6ff8][rid#acd0c8/initial] (4) RewriteCond: input='C:/wordpress/about' pattern='!-f' => matched [localhost.com/sid#6b6ff8][rid#acd0c8/initial] (4) RewriteCond: input='C:/wordpress/about' pattern='!-d' => matched [localhost.com/sid#6b6ff8][rid#acd0c8/initial] (2) [per-dir C:/wordpress/] rewrite about/ -> /index.php [localhost.com/sid#6b6ff8][rid#acd0c8/initial] (2) [per-dir C:/wordpress/] trying to replace prefix C:/wordpress/ with / [localhost.com/sid#6b6ff8][rid#acd0c8/initial] (1) [per-dir C:/wordpress/] internal redirect with /index.php [INTERNAL REDIRECT] [localhost.com/sid#6b6ff8][rid#ad6eb8/initial/redir#1] (3) [per-dir C:/wordpress/] strip per-dir prefix: C:/wordpress/index.php -> index.php [localhost.com/sid#6b6ff8][rid#ad6eb8/initial/redir#1] (3) [per-dir C:/wordpress/] applying pattern '(^blog/downloads/[^/]+$)' to uri 'index.php' [localhost.com/sid#6b6ff8][rid#ad6eb8/initial/redir#1] (3) [per-dir C:/wordpress/] strip per-dir prefix: C:/wordpress/index.php -> index.php [localhost.com/sid#6b6ff8][rid#ad6eb8/initial/redir#1] (3) [per-dir C:/wordpress/] applying pattern '.' to uri 'index.php' [localhost.com/sid#6b6ff8][rid#ad6eb8/initial/redir#1] (4) RewriteCond: input='C:/wordpress/index.php' pattern='!-f' => not-matched [localhost.com/sid#6b6ff8][rid#ad6eb8/initial/redir#1] (1) [per-dir C:/wordpress/] pass through C:/wordpress/index.php
Here you can clearly see how much internal work is been done silently by the mod_rewrite engine for a single file request.
Now you have learnt how to log the internal processing actions of mod_rewrite module with the help of RewriteLog directive I am sure it will help you solve many rewrite rule and condition patterns.
Convert date format MySQL YYYY-MM-DD to DD-MM-YYYY
If you have worked on a web application with PHP/MySQL then you must be knowing how annoying it is to manipulate dates when you are using British style date format. While MySQL stores date in YYYY-MM-DD format, PHP uses DD-MM-YYYY for date timestamp.
Here is a simple function that will convert MySQL type YYYY-MM-DD date format to DD-MM-YYYY format. Optionally, you can also provide date separators for input and output format.
function formatDate($date, $in = '-', $out = '-')
{
if ($date)
{
return implode($out, array_reverse(explode($in, $date)));
}
}
Usage
print formatDate('1979-01-18');
// Output: 18-01-1979
print formatDate('1979-01-18', '-', '/');
// Output: 18/01/1979
References:
ET Remove Comment Author Info - WordPress Plugin
Comments are important part of the blog content and we should always encourage our readers to leave comments on our articles. However, many times commenter is reluctant to write comments because WordPress stores the comment author info into a cookie and this information is displayed in the comment form on each article. Actually, it is a usability add-on for some who thinks that they need not to put their name and email every time they want to post a comment. Whereas other commenter who access internet from cyber cafes, public places or from shared computers thinks that it is a privacy issue as anyone can see their personal information on those pages on which they post a comment.
So, here is a WordPress plugin that will insert a link in the comment form that will allow the comment author to delete their personal info from the comment form. Only unregistered users will be provided with the delete link if they already had a previously posted comment.
Screenshots
Author delete link on Comment Form

Download
Plugin: ET Remove Comment Author Info
Version: 1.0
Downloads: 36
Install
- Download the zip archive and extract the folder/files.
- Upload the extracted folder/files to your WordPress plugins directory (/wp-content/plugins/).
- From Administration Panels go to Plugins and click "Activate" for "ET Remove Comment Author Info" plugin.
Well done, you have just installed ET Remove Comment Author Info WordPress plugin.
Uninstall
- From Administration Panels go to Plugins and click "Deactivate" for "ET Remove Comment Author Info" plugin.
Similar Plugins:
PHP Browser Detection and User Agent Sniffer
Many times, you require server side browser detection technique to serve browser-based data and display separate designs for different type of user agents like Internet Explorer, Opera, Firefox, etc. In PHP, you can simply test the browser's user agent string and then parse it to get browser type and its version.
Browser Detection
There is a predefined variable in PHP called $_SERVER which is an array containing server information. One of the element of $_SERVER variable is "HTTP_USER_AGENT" which is a string containing the User-Agent header. As an example, your user agent string is:
CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
Now we will use this user agent string to identify what browser the user is using to view the requested page. There are four major browsers namely Internet Explorer, Opera, Firefox and Safari that accumulates to 99% of the user agents being used worldwide. Hence, we will detect only those four browser names and their respective versions. The code for this sniffing is given below.
$UA = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ";
$SF = strstr($UA, 'Safari') ? true : false;
$OP = strstr($UA, 'Opera') ? true : false;
$OPV = $OP ? preg_split('/opera\//i', $UA) : false;
$OPV = $OPV ? floatval($OPV[1]) : false;
$FF = !$OP && strstr($UA, 'Firefox') ? true : false;
$FFV = $FF ? preg_split('/firefox\//i', $UA) : false;
$FFV = $FFV ? floatval($FFV[1]) : false;
$IE = !$OP && !$FF && strstr($UA, 'MSIE') ? true : false;
$IEV = $IE ? preg_split('/msie/i', $UA) : false;
$IEV = $IEV ? floatval($IEV[1]) : false;
For usability purpose, each browser has been given a short hand variable name i.e.
$SF: Safari
$OP: Opera
$OPV: Opera Version
$FF: Firefox
$FFV: Firefox Version
$IE: Internet Explorer
$IEV: Internet Explorer Version
Usage
Practically there are innumerous ways you can use this browser detection functionality in your web development environment and it is totally up to you to decide how to use it. However, here are some useful examples for your reference.
Inline Styles: You can declare inline styles for different browsers.
if ($IE && $IEV <= 5.5)
{
print '#footer { font-size: xx-small; }';
}
External Stylesheet: Include different stylesheets based on browser type.
if ($OP || $FF)
{
print '<link rel="stylesheet" type="text/css" href="nonie.css">';
}
External JavaScript: For compatibility issues you can include separate JavaScript files for different browser agents.
if ($IEV == 6)
{
print '<script type="text/javascript" src="dom.js"></script>';
}
Include Files: You can also include different files for various user agents.
if ($SF)
{
require_once 'mac.php';
}
Similar Scripts:
Now you have this PHP browser detection script, go and simplify your web design and capability with different browsers for better usability.
ET SEO Title and Description - WordPress Plugin
This plugin will optimize your WordPress Title and Description tags for search engine optimization. It has been released in conjunction with WordPress SEO Title and Description tag article.
Download
Plugin: ET SEO Title and Description
Version: 1.0
Downloads: 151
Install
- Download the zip archive and extract the folder/files.
- Upload the extracted folder/files to your WordPress plugins directory (/wp-content/plugins/).
- From Administration Panels go to Plugins and click "Activate" for "ET SEO Title and Description" plugin.
- Go to Presentation -> Theme Editor and click on "Header" from theme files list.
- Comment out the whole title and description tag, if any, and then click "Update File".
<!-- <title>...</title> --> <!-- <meta name="description" ... /> -->
Well done, you have just installed ET SEO Title and Description WordPress plugin.
Uninstall
- From Administration Panels go to Plugins and click "Deactivate" for "ET SEO Title and Description" plugin.
- Go to Presentation -> Theme Editor and click on "Header" from theme files list.
- Uncomment the whole title and description tag and then click "Update File".
- Delete the uploaded folder/files from your WordPress plugins directory (/wp-content/plugins/).
Similar Plugins:
WordPress SEO Title and Description tag
Title tag is one of the most important on page optimization feature because the end user will first see the title of your page in SERP (Search Engine Result Page). It is also well known that search engines give more weight to titles when ranking your site for the searched keywords. That means your title tag must be well optimized and smart enough to justify your page contents.
Moreover, the same can be tell about the description tag as most of the time the search engines will also show your description tag in SERP, if all your pages have unique and on topic description tags.
Now we will see how to automate the process of writing this search engine optimized (SEO) title and description tags in WordPress.
WordPress Title Tag Optimization
Home Page Title: It is best to keep the blog's name as home page title as it will look more professional and enhances your brand name. If you wish, you can add some extra keywords along with the blog name.
Single Post Title: It is recommended to keep your post's title as single post title because those are the entry pages from the search engine traffic and so it is desirable to keep the title up to the point in context to page content. If you feel the need of branding then you can add your blog's name to the end with a separator like dash or pipe symbol in between (e.g. Hello World! - Blog Name).
Page Title: Most of the pages are static ones with less content and more of site supplements like about, disclaimer, privacy policy and contact details. Therefore, it is best to keep your page title to the combination of blog's name and the page title itself with a separator in between (e.g. Blog Name - About Us).
Category Title: The category title should be the category name with the words "Archive" or "Category" added to the end. If the current category is a sub category, then the title will be a combination of the parent and the sub category name (e.g. Parent Category Name Sub Category Name). It is advisable to disallow search engine spiders to index your category pages to avoid duplicate content penalties, so there is not much optimization in category title.
Search Result Page Title: This title should be optimized for your users as they are going to use this feature the most. The title should contain your blog's name along with the searched keyword (e.g. Blog Name - Search results for "keyword").
Here is the code for WordPress SEO Title tag that you need to replace in your header.php theme file that is usually located inside "wp-content/themes" folder.
<title><?php
if ( is_single() ) { single_post_title(); }
elseif ( is_page() ) { bloginfo('name'); single_post_title(' - '); }
elseif ( is_category() ) { print trim(get_category_parents($cat, '', ' ')) . 'Category'; pageGetPageNo(); }
elseif ( is_search() ) { bloginfo('name'); print ' - Search results for ' . wp_specialchars($s); pageGetPageNo(); }
else { bloginfo('name'); pageGetPageNo(); }
?></title>
It is a good idea to add the page numbers to the title tag for better usability. You will notice a custom function named "pageGetPageNo" in the above code that will accomplish this functionality by taking the "paged" argument from the URL and adding that to the title tag for page numbers (e.g. Blog Name - Page 2, Category Name - Page 2). Here is the code for this function:
function pageGetPageNo()
{
if (get_query_var('paged'))
{
print ' - Page ' . get_query_var('paged');
}
}
You can copy this code and put it inside your function.php theme file.
WordPress Description Tag Optimization
Home Page Description: Generally, the blog's tagline is used in home page description as it is targeted towards the whole site instead of individual post or categories.
Single Post and Page Description: You should always write short, unique and to the point post excerpts for each post and the same should be kept as your single post and page description. If you are not comfortable with this process then you should add the first 20 words from your post content in the description tag.
Category Description: The category description should be either the category description from the category table or the category name.
Here is the code for WordPress SEO Description tag that you need to replace in your header.php theme file.
<meta name="description" content="<?php
if ( is_single() || is_page() ) { pageDescription(); }
elseif ( is_category() ) { print strip_tags(trim(category_description())); }
elseif ( is_search() ) { bloginfo('name'); print ' - Search results for ' . wp_specialchars($s); }
else { bloginfo('description'); }
?>" />
For post and page description there is a custom function named "pageDescription" which will first check if post excerpt is available, if not then it will pull first 20 words from your actual post content and will use it in description tag. Here is the code for this function:
function pageDescription()
{
global $post;
if (empty($post->post_excerpt))
{
$pageDesc = str_replace(array('\r\n', '\r', '\n', ' '), ' ', strip_tags($post->post_content));
$pageDesc = str_replace(array('"', "'"), array('"', '''), $pageDesc);
$pageDesc = preg_replace('/\s{2,}/', ' ', $pageDesc);
$pageDesc = preg_match('/(\w+\W+){0,20}/', $pageDesc, $wordMatches);
print trim($wordMatches[0]);
}
else
{
$pageDesc = $post->post_excerpt;
$pageDesc = str_replace(array('"', "'"), array('"', '''), $pageDesc);
print $pageDesc;
}
}
You can copy this code and put it inside your function.php theme file.
WordPress Plugin
You can also download and install this WordPress plugin called ET SEO Title and Description that will provide the same functionality as mentioned in this article.
References:
After implementing these search engine optimization techniques for Title and Description tags be rest assured for some improvement in your search engine rankings.
