Sunday, July 8, 2012

SEO Tips : Page Renaming - Wordpress

When changing the name of a page via wordpress, keep in mind that the earlier name page URL will not offer a 404 not found request...

This is to be avoided as this completely kills the SEO achieved on the previous URL... crawlers will keep reporting back that the page is down...

3 steps need to be done to prevent any problems:

Step 1: Report within Google Webmaster Tools that the page names have changed

Step 2: Prevent robots/crawlers from finding the problematic page:

This can be done via 2 ways:

1. Use an appropriate WordPress Plugin to ensure that when page names are changed, the Plugin will ensure that WordPress itself will offer the correct pages...

2. Write the appropriate lines in the .htaccess file to ensure that a 301 redirect is offered from the earlier specified URL to the newer URL...


Step 3: Create a custom 404 page which is user friendly and offers an alternative user navigation . The 404 page may say:

We're sorry you didnt find this page:  But you can read about 
A --> Here
B --> Here
C --> Here
etc

Where the links and topics should be the core topics of the business (or SEO chosen keywords)

Friday, July 6, 2012

SSL in Wamp Server

SSL Encryption can be added to your Wamp Server package to make it more secure as well as to test the SSL environment before taking your application to production. 

Note 1: Example given in case the folder in which apache is installed is apache2.2.6  where 2.2.6 is the version of apache installed. In case of another version being installed, say apache 2.2.22, this folder would change to apache2.2.22

Note 2: the foldername appName used below should be changed to whatever is the foldername where your application is placed which should be served to users connecting via https://

1. Create SSL Certificate and Key

Open the DOS command window and change directory to bin directory of wamp apache directory by using the DOS command

cd c:\wamp\bin\apache\apache2.2.6\bin

Now the DOS prompt should look like: C:\wamp\bin\apache\apache2.2.6\bin>

To create a server key with 1024 bits encryption, enter this command:

openssl genrsa -des3 -out server.key 1024

It’ll ask you a pass phrase, enter a passphrase of your choice which you will remember

Remove the pass phrase from the RSA private key (while keeping a backup copy of the original file). Enter these commands:

copy server.key server.key.org
openssl rsa -in server.key.org -out server.key

It’ll ask you the pass phrase you entered earlier, enter it

Create a self-signed Certificate (X509 structure) with the RSA key you just created. Enter the command:

openssl req -new -x509 -nodes -sha1 -days 365 -key server.key -out server.crt -config C:\wamp\bin\apache\apache2.2.6\conf\openssl.cnf


You'll be asked to fill in information after entering this command.

2. Copy the server.key and server.crt files


In the conf folder of apache2.2.6 folder, create two folders named as ssl.key and ssl.crt

Copy the server.key file to ssl.key folder and server.crt file to ssl.crt

3. Edit the httpd.conf file and php.ini


In httpd.conf file, remove the comment definer # at the line which says:
LoadModule ssl_module modules/mod_ssl.so

In httpd.conf, remove the comment definer # at the line which says:
Include conf/extra/httpd_ssl.conf

Then move that line after this block <IfModule ssl_module>…. </IfModule>

Open the php.ini file located in apache2.2.6/bin folder, remove the comment definer; at the line which says:
extension=php_openssl.dll

4. Edit the httpd-ssl.conf file


The httpd-ssl.conf file is located at C:\wamp\bin\apache\Apache2.2.6\conf\extra

This should be edited as follows

Find the line which says "SSLMutex …." and change it to SSLMutex default without quotes

Find the line which says: <VirtualHost _default_:443>.
Right after it, change the line which says DocumentRoot … to DocumentRoot "C:/wamp/www/appname" with quotes.  
Change the line "ErrorLog…." to Errorlog C:/wamp/logs/sslerror.log without quotes
Change the line "TransferLog …." to TransferLog C:/wamp/logs/sslaccess.log without quotes

Specify the SSL crt file:  Change the line "SSLCertificateFile …." to SSLCertificateFile "C:/wamp/bin/apache/apache2.2.6/conf/ssl.crt/server.crt"

Specify the SSL key file: Change the line "SSLCertificateKeyFile …." to SSLCertificateKeyFile " C:/wamp/bin/apache/apache2.2.6/conf/ssl.key/server.key"

Change the line which says <Directory "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin"> to <Directory "C:/wamp/www/">
and add the following lines inside those <Directory … >…</Directory> tags:

Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

Make sure the line CustomLog "C:/wamp/logs/ssl_request.log" \  is uncommented (remove the #).

In the DOS Command window, enter httpd -t . If it displays Syntax is OK, then proceed, else you have made a syntactical error and will need to troubleshoot

Go to C:/wamp/logs and create the following 3  files
                                i.            sslerror.log
                              ii.            ssl_request.log
                             iii.            sslaccess.log

5. Starting the HTTPS Server and Putting it Online


Restart the Apache server

If restart is successful, then open the browser and enter https://localhost

If you want to allow put your HTTPS secure server online then in the httpd_ssl.conf file, change the line which says "ServerName localhost:443" to "ServerName www.yourwebsitename.com:443" or  "ServerName X.Y.Z.A:443" without quotes where yourwebsitename is your registered internet domain name and/or  X.Y.Z.A is your WAN IP Address

The DocumentRoot you modified in Step 4 points to the correct website folder on your server.

If your server is connected to a router, setup the router to allow port 443 forwarding to your server.

If your server has a firewall enabled or behind a network firewall, set up the firewall to allow incoming port 443 connection

Monday, June 18, 2012

PHP to handle inputs copy pasted from MS Word

A common problem faced is that the user wants to copy & paste their article or part of it from Microsoft Word, directly into a textarea on a page. The problem is word uses non UTF-8 characters. Once the page is submitted, PHP gets it and the characters are encoded differently and they display weirdly when displayed back via an echo. 

A Commonly available solution for this is to create a function which parses the input and cleans it up either by removing the non standard characters or converting them into standard UTF-8 characters

Here is an example of one such

function fWordCharacterConverter($str)
{
$invalid = array('Š'=>'S', 'š'=>'s', 'Ð'=>'Dj', 'd'=>'dj', 'Ž'=>'Z', 'ž'=>'z',
'C'=>'C', 'c'=>'c', 'C'=>'C', 'c'=>'c', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A',
'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E',
'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y',
'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a',
'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e',  'ë'=>'e', 'ì'=>'i', 'í'=>'i',
'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y',  'ý'=>'y', 'þ'=>'b',
'ÿ'=>'y', 'R'=>'R', 'r'=>'r', "`" => "'", "´" => "'", "„" => ",", "`" => "'",
"´" => "'", "“" => "\"", "”" => "\"", "´" => "'", "&acirc;€™" => "'", "{" => "",
"~" => "", "–" => "-", "’" => "'");

$str = str_replace(array_keys($invalid), array_values($invalid), $str);

return $str;
}


Another solution which is much less known but works well is to use the php function iconv

$str= iconv('UTF-8', 'ASCII//TRANSLIT', $str);    

Details from the PHP Manual:


string iconv ( string $in_charset , string $out_charset , string $str )
Performs a character set conversion on the string str from in_charset to out_charset

Parameters

in_charset
The input charset.
out_charset
The output charset.
If you append the string //TRANSLIT to out_charset transliteration is activated. This means that when a character can't be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character and an E_NOTICE is generated.
str
The string to be converted.

Return Values

Returns the converted string or FALSE on failure.


Saturday, May 5, 2012

array_pop and array_push in PHP


array_push  Push one or more elements onto the end of array

array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed.

Has the same effect as:

<?php
$array[] = $var;
?>
repeated for each var.





array_pop  Pop the element off the end of array

array_pop() pops and returns the last value of the array, shortening the array by one element. If array is empty (or is not an array), NULL will be returned. Will additionally produce a Warning when called on a non-array.

Note: This function will reset() the array pointer of the input array after use.


 


Saturday, March 17, 2012

Redirecting based on the URL string using PHP

In many cases, one ends up hosting multiple sites on a single server, shared or VPS.

Also in many other cases a company has multiple domains and multiple page addresses to service clients and prospects from different countries, demographics and walks of life.

And the need of the hour is that one wants to send a user to a different folder on your web site, based on what domain they use? Here we will explain a simple way to do this using PHP.

For instance on my VPS, I have 2 domains pointing: one domain called sampleno1.com and the other one called sampleno2.com. Both point to the IP of the VPS.
We have an index.php file in the root of the web server on the VPS containing the following code.

<?php

if (($_SERVER['HTTP_HOST'] == “sampleno1.com”) || ($_SERVER['HTTP_HOST'] == “www.sampleno1.com”))
{
    header("location: folderforsampleno1");
}

if (($_SERVER['HTTP_HOST'] == “sampleno2.com”) || ($_SERVER['HTTP_HOST'] == “www.sampleno2.com”))
{
    header("location: folderforsampleno2");
}

?>


The code asks the PHP server to see what the HTTP_HOST header is. If it is sampleno1.com or www.sampleno1.com, then it redirects to the folder on the server which contains the code for the site sampleno1.

If it is sampleno2.com or www.sampleno2.com, then it redirects to the folder on the server which contains the code for the site sampleno2

Saturday, March 3, 2012

Find a File containing a specific text string in Ubuntu

Find a File containing a specific text string in Ubuntu can be done using the grep command.

The format is

grep “text string to search” directory-path

Some Examples

Simple String Search in a directory:
Search for a string called "star wars" in all text files located in /home/movies/*.txt directory, use

$ grep "star wars" /home/movies/*.txt

Search for a string in all files in all contained folders

For this, you can search for a text string all files under each directory, recursively with -r option:

$ grep -r "star wars" /home/movies/

Monday, February 6, 2012

Determine file size with du

The du command ( disk usage ) gather and summarize about how much your disk space being used by your file and the disk space being use by directory in the Linux system The du command can be use to find the size of file and the size of directory in Linux system.

du takes a single argument, specifying a pathname for du to work; if it is not specified, the current directory is used. The SUS mandates for du the following options:
-a, display an entry for each file (and not directory) contained in the current directory
-H, calculate disk usage for link references specified on the command line
-k, show sizes as multiples of 1024 bytes, not 512-byte
-L, calculate disk usage for link references anywhere
-s, report only the sum of the usage in the current directory, not for each file
-x, only traverse files and directories on the device on which the pathname argument is specified.
du is flexible and offers a variety of options. For example, it allows you to determine the size of the current directory and all subdirectories:
$ du -sh

Using du by itself displays the size of every subdirectory under the current directory--it summarizes and reports the data in human-readable format, such as 402 MB instead of 411012 bytes. You can also see the size of every file in every subdirectory by incorporating the "-a" option:
$ du -ah

In addition, you can use du in combination with grep to find a particular file size. If you're searching in a directory and suspect a file is growing extremely large, reduce the output of du to all files and directories 1 GB in size or larger with this command:
$ du -ah|grep -e '[0-9]G'

Examples 

Sum of directories in kilobytes:

 $ du -sk *
 152304  directoryOne
 1856548 directoryTwo
Sum of directories in human-readable format (Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte):
 $ du -sh *
 149M directoryOne
 1.8G directoryTwo
disk usage of all subdirectories and files including hidden files within the current directory (sorted by filesize) :
 $ du -sk .[!.]* *| sort -n
disk usage of all subdirectories and files including hidden files within the current directory (sorted by reverse filesize) :
 $ du -sk .[!.]* *| sort -nr
The weight of directories:
 $ du -d 1 -c -h

More information on du command:
# info du
# man du
# du --help

Thursday, February 2, 2012

node-static for Serving Static Files with Node.js

node-static can be installed via npm with the following command:
npm install node-static
To use it: include the module in your script via the require function, create a server, pass it your preferred options, and tell it to serve files:
var static = require('node-static'),
  http = require('http'),
  util = require('util');
var webroot = './public',
  port = 8181;
var file = new(static.Server)(webroot, {
  cache: 900,
  headers: { 'X-Powered-By': 'node-static' }
});
http.createServer(function(req, res) {
  req.addListener('end', function() {
    file.serve(req, res, function(err, result) {
      if (err) {
        console.error('Error serving %s - %s', req.url, err.message);
        if (err.status === 404 || err.status === 500) {
          file.serveFile(util.format('/%d.html', err.status), err.status, {}, req, res);
        } else {
          res.writeHead(err.status, err.headers);
          res.end();
        }
      } else {
        console.log('%s - %s', req.url, res.message);
      }
    });
  });
}).listen(port);
console.log('node-static running at http://localhost:%d', port);
Here, when we create a new instance of a node-static server, we pass it:
  • the directory we want it to serve files from by way of the `webroot` variable (defaults to serving files from the current directory unless you tell it otherwise)
  • a cache value of 900, so each file served will be cached for 15 minutes (default value for cache is 3600, meaning files are cached for 1 hour)
  • a custom ‘X-Powered-By’ header (I’ve used X-Powered-By solely as an example – you may, or may not, want to disclose the software you’re server is running)
We then use the http module’s createServer function, and add an event handler for the request’s end event where we use our instance of node-static to serve the files.
When we call file.serve, we pass it an optional callback function that let’s us customise how we handle any errors:
  • we check err.status, and serve either 404.html or 500.html accordingly
  • if there’s an error and the status code is something other than 404 or 500, we send the status code and the headers back to the client explicitly – when you pass a callback function to file.serve and there is an error, node-static will not respond to the client by itself.

Tuesday, January 17, 2012

Working with MySQL Events

MySQL events were added in MySQL 5.1.6 and offer an alternative to scheduled tasks and cron jobs. Events can be used to create backups, delete stale records, aggregate data for reports, and so on. Unlike standard triggers which execute given a certain condition, an event is an object that is triggered by the passage of time and is sometimes referred to as a temporal trigger. You can schedule events to run either once or at a recurring interval when you know your server traffic will be low.
In this article I’ll explain what you need to know to get started using events: starting the event scheduler, adding events to run once or multiple times, viewing existing events, and altering events. I’ll also share with how you might use MySQL events using scheduled blog posts as a practical example.

Starting the Event Scheduler

The MySQL event scheduler is a process that runs in the background and constantly looks for events to execute. Before you can create or schedule an event, you need to first turn on the scheduler, which is done by issuing the following command:
mysql> SET GLOBAL event_scheduler = ON;
Likewise, to turn all events off you would use:
mysql> SET GLOBAL event_scheduler = OFF;
Once the event scheduler is started, you can view its status in MySQL’s process list.
mysql> SHOW PROCESSLIST\G
...
     Id: 79
   User: event_scheduler
   Host: localhost
     db: NULL
Command: Daemon
   Time: 12
  State: Waiting on empty queue
   Info: NULL

Working with Events

It’s important to note that when an event is created it can only perform actions for which the MySQL user that created the event has privileges to perform. Some additional restrictions include:
  • Event names are restricted to a length of 64 characters.
  • As of MySQL 5.1.8, event names are not case-sensitive; each event name should be unique regardless of case.
  • Events cannot be created, altered, or dropped by another event.
You cannot reference a stored function or user-defined function when setting the event schedule.

Creating Events

The following example creates an event:
01DELIMITER |
02 
03CREATE EVENT myevent
04    ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
05    DO
06      BEGIN
07        UPDATE mytable SET mycol = mycol + 1;
08      END |
09 
10DELIMITER ;
This event will run once, one hour from the time it was created. The BEGIN and END statements surround one or multiple queries which will be executed at the specified time. Because the semicolon is needed to terminate the UPDATE statement, you’ll need to switch delimiters before you issue the CREATE EVENT statement and then switch back afterwards if you’re working through a client.
You can view a list of all existing events with SHOW EVENTS.
mysql> SHOW EVENTS\G
********************** 1. row **********************
                  Db: mysql
                Name: myevent
             Definer: dbuser@localhost
           Time zone: SYSTEM
                Type: ONE TIME
          Execute At: 2011-10-26 20:24:19
      Interval Value: NULL
      Interval Field: NULL
              Starts: NULL
                Ends: NULL
              Status: ENABLED
          Originator: 0
character_set_client: utf8
collation_connection: utf8_general_ci
After an event has expired it will be automatically deleted unless you explicitly stated otherwise with an ON COMPLETION clause, for example:
1CREATE EVENT myevent
2    ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
3    ON COMPLETION PRESERVE
4    DO
5      BEGIN
6        UPDATE mytable SET mycol = mycol + 1;
7      END |
In this example, even though the event has expired it will still be retained in the database which will allow you to alter and run it again later, or perhaps you’d just like to keep it for reference.
To permanently delete an event yourself, you can use DROP EVENT:
1DROP EVENT myevent;
To specify a recurring event, you would use the EVERY clause:
1CREATE EVENT myevent
2    ON SCHEDULE EVERY 1 HOUR
3    DO
4      BEGIN
5        UPDATE mytable SET mycol = mycol + 1;
6      END |
And rather than having an event that just runs once or forever, you can also schedule a reoccurring event that is valid only within a specific time period, using START and END clauses:
1CREATE EVENT myevent
2    ON SCHEDULE EVERY 1 HOUR
3    STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
4    ENDS CURRENT_TIMESTAMP + INTERVAL 1 YEAR
5    DO
6      BEGIN
7        UPDATE mytable SET mycol = mycol + 1;
8      END |
In this example, the reoccurring event would start tomorrow and continue to run every hour for a full year.
With regard to timing, the interval specified can be YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, orSECOND. Keep in mind that keywords are given as singular forms; writing something like INTERVAL 5 MINUTE may seem awkward to you, but it is perfectly correct to MySQL.

Updating Events

If you want to change an existing event’s behavior rather than deleting it and recreating it, you can use ALTER EVENT. For example, to change the schedule of the previous event to run every month, starting at some date in the future at 1 o’clock in the morning, you would use the following:
1ALTER EVENT myevent
2    ON SCHEDULE EVERY 1 MONTH
3    STARTS '2011-12-01 01:00:00' |
To update the event with a different set of queries, you would use:
1ALTER EVENT myevent
2    DO
3      BEGIN
4        INSERT INTO mystats (total)
5          SELECT COUNT(*) FROM sessions;
6        TRUNCATE sessions;
7      END |
To rename an event, you would specify a RENAME clause:
1ALTER EVENT myevent
2    RENAME TO yourevent;

Blog Post Scheduling

So that I can show you a practical example, let’s say you have a blog and you want the option to schedule posts to be published at some time in the future. One way to achieve this is to add a timestamp and published flag to the database records. A cron script would execute once every minute to check the timestamps and flip the flag for any posts that should be published. But this doesn’t seem very efficient. Another way to achieve this is by using MySQL events that will fire when you want publish the post.
Your blog entry form might have a checkbox that, when checked, indicates this is a scheduled post. Additionally, the form would have input fields for you to enter the date and time of when the post should be published. The receiving script would be responsible for adding the blog entry to the database and managing the events to schedule it if it’s not an immediate post. The relevant code looks like the following:
01<?php
02// establish database connection and filter incoming data
03// ...
04 
05// insert blog post with pending status, get id assigned to post
06$query = "INSERT INTO blog_posts (id, title, post_text, status)
07    VALUES (NULL, :title, :postText, 'pending')";
08$stm = $db->prepare($query);
09$stm->execute(array(":title" => $title, ":postText" => $text));
10$id = $db->lastInsertId();
11 
12// is this a future post?
13if (isset($_POST["schedule"], $_POST["time"])) {
14    $scheduleDate = strtotime($_POST["time"]);
15 
16    $query = "CREATE EVENT publish_:id
17    ON SCHEDULE AT FROM_UNIXTIME(:scheduleDate)
18    DO
19      BEGIN
20        UPDATE blog_posts SET status = 'published' WHERE id = :id;
21      END";
22    $stm = $db->prepare($query);
23    $stm->execute(array(":id" => $id, ":scheduleDate" =>$scheduleDate));
24}
25// this is not a future post, publish now
26else {
27    $query = "UPDATE blog_posts SET status = 'published' WHERE id = :id";
28    $stm = $db->prepare($query);
29    $stm->execute(array(":id" => $id));
30}
When the post is stored in the database it is saved with a pending status. This gives you the chance to schedule the event if it’s a scheduled post, otherwise the status can be immediately updated to published.
If you were to edit the post at a later time, you can delete the event with DROP EVENT IF EXISTSand re-add it with the new scheduled time.