Wednesday 21 January 2015

Hooks concept in codeigniter with simple examples

Hook feature can be useful for adding custom changes without changing the core code.  There may be instances, however, where you'd like to cause some action to take place at a particular stage in the execution process. For example, you might want to run a script right before your controllers get loaded, or right after, or you might want to trigger one of your own scripts in some other location.

Enabling Hooks

The hooks feature can be globally enabled/disabled by setting the 

following item in the application/config/config.php file:

$config['enable_hooks'] = TRUE;

Defining a Hook
Hooks are defined in application/config/hooks.php file. Each hook is specified as an array with this prototype:   

$hook['pre_controller'] = array(
                                'class'    => 'MyClass',
                                'function' => 'Myfunction',
                                'filename' => 'Myclass.php',
                                'filepath' => 'hooks',
                                'params'   => array('bread', 'wine', 'butter')
                                );
CLASS-The class that you wish to invoke, if it is procedural code leave it as blank.
FUNCTION-The function name you wish to call.
FILENAME-The file name containing your class/function.
FILEPATH-Location of the hook file.
PARAMS-Additional parameter if needed it is optional

Hook Points
The following is a list of available hook points.
  • pre_system
    Called very early during system execution. Only the benchmark and hooks class have been loaded at this point. No routing or other processes have happened.
  • pre_controller
    Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done.
  • post_controller_constructor
    Called immediately after your controller is instantiated, but prior to any method calls happening.
  • post_controller
    Called immediately after your controller is fully executed.
  • display_override
    Overrides the _display() function, used to send the finalized page to the web browser at the end of system execution. This permits you to use your own display methodology. Note that you will need to reference the CI superobject with $this->CI =& get_instance() and then the finalized data will be available by calling $this->CI->output->get_output()
  • cache_override
    Enables you to call your own function instead of the _display_cache() function in the output class. This permits you to use your own cache display mechanism.
  • post_system
    Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser.

Wednesday 19 November 2014

MySQL Backup and Restore Commands for Database Administration

How to Backup MySQL Database?

To take a backup of MySQL database or databases, the database must exist in the database server and you must have access to it. The format of the command would be.
# mysqldump -u [username] –p[password] [database_name] > [dump_file.sql]
The parameters of the said command as follows.
  1. [username] : A valid MySQL username.
  2. [password] : A valid MySQL password for the user.
  3. [database_name] : A valid Database name you want to take backup.
  4. [dump_file.sql] : The name of backup dump file you want to generate.

How to Backup a Single MySQL Database?

To take a backup of single database, use the command as follows. The command will dump database [rsyslog] structure with data on to a single dump file called rsyslog.sql.
# mysqldump -u root -ptecmint rsyslog > rsyslog.sql

How to Backup Multiple MySQL Databases?

If you want to take backup of multiple databases, run the following command. The following example command takes a backup of databases [rsyslogsyslog] structure and data in to a single file called rsyslog_syslog.sql.
# mysqldump -u root -ptecmint --databases rsyslog syslog > rsyslog_syslog.sql

How to Backup All MySQL Databases?

If you want to take backup of all databases, then use the following command with option –all-database. The following command takes the backup of all databases with their structure and data into a file called all-databases.sql.
# mysqldump -u root -ptecmint --all-databases > all-databases.sql

How to Backup MySQL Database Structure Only?

If you only want the backup of database structure without data, then use the option –no-data in the command. The below command exports database [rsyslogStructure into a filersyslog_structure.sql.
# mysqldump -u root -ptecmint -–no-data rsyslog > rsyslog_structure.sql

How to Backup MySQL Database Data Only?

To backup database Data only without structure, then use the option –no-create-info with the command. This command takes the database [rsyslogData  into a file rsyslog_data.sql.
# mysqldump -u root -ptecmint --no-create-db --no-create-info rsyslog > rsyslog_data.sql

How to Backup Single Table of Database?

With the below command you can take backup of single table or certain tables of your database. For example, the following command only take backup of wp_posts table from the databasewordpress.
# mysqldump -u root -ptecmint wordpress wp_posts > wordpress_posts.sql

How to Backup Multiple Tables of Database?

If you want to take backup of multiple or certain tables from the database, then separate each table with space.
# mysqldump -u root -ptecmint wordpress wp_posts wp_comments > wordpress_posts_comments.sql

How to Backup Remote MySQL Database

The below command takes the backup of remote server [172.16.25.126] database [gallery] into a local server.
# mysqldump -h 172.16.25.126 -u root -ptecmint gallery > gallery.sql

How to Restore MySQL Database?

In the above tutorial we have seen the how to take the backup of databases, tables, structures and data only, now we will see how to restore them using following format.
# # mysql -u [username] –p[password] [database_name] < [dump_file.sql]

How to Restore Single MySQL Database

To restore a database, you must create an empty database on the target machine and restore the database using msyql command. For example the following command will restore thersyslog.sql file to the rsyslog database.
# mysql -u root -ptecmint rsyslog < rsyslog.sql
If you want to restore a database that already exist on targeted machine, then you will need to use the mysqlimport command.
# mysqlimport -u root -ptecmint rsyslog < rsyslog.sql
In the same way you can also restore database tables, structures and data. If you liked this article, then do share it with your friends.

Thursday 25 September 2014

High Secure data Encryption Decryption in php

// Define a 32-byte (64 character) hexadecimal encryption key
// Note: The same encryption key used to encrypt the data must be used to decrypt the data
define('ENCRYPTION_KEY', 'd0a7e7997b6d5fcd55f4b5c32611b87cd923e88837b63bf2941ef819dc8ca282');

// Encrypt Function
function mc_encrypt($encrypt, $key){
$encrypt = serialize($encrypt);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND);
$key = pack('H*', $key);
$mac = hash_hmac('sha256', $encrypt, substr(bin2hex($key), -32));
$passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $encrypt.$mac, MCRYPT_MODE_CBC, $iv);
$encoded = base64_encode($passcrypt).'|'.base64_encode($iv);
return $encoded;
}

// Decrypt Function
function mc_decrypt($decrypt, $key){
$decrypt = explode('|', $decrypt);
$decoded = base64_decode($decrypt[0]);
$iv = base64_decode($decrypt[1]);
if(strlen($iv)!==mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)){ return false; }
$key = pack('H*', $key);
$decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_CBC, $iv));
$mac = substr($decrypted, -64);
$decrypted = substr($decrypted, 0, -64);
$calcmac = hash_hmac('sha256', $decrypted, substr(bin2hex($key), -32));
if($calcmac!==$mac){ return false; }
$decrypted = unserialize($decrypted);
return $decrypted;
}

echo '<h1>Rijndael 256-bit CBC Encryption Function</h1>';

$data = 'swargamm';
$encrypted_data = mc_encrypt($data, ENCRYPTION_KEY);
echo '<h2>Example #1: String Data</h2>';
echo 'Data to be Encrypted: ' . $data . '<br/>';
echo 'Encrypted Data: ' . $encrypted_data . '<br/>';
echo 'Decrypted Data: ' . mc_decrypt($encrypted_data, ENCRYPTION_KEY) . '</br>';

$data = array(1, 5, 8, 9, 22, 10, 61);
$encrypted_data = mc_encrypt($data, ENCRYPTION_KEY);
echo '<h2>Example #2: Non-String Data</h2>';
echo 'Data to be Encrypted: <pre>';
print_r($data);
echo '</pre><br/>';
echo 'Encrypted Data: ' . $encrypted_data . '<br/>';
echo 'Decrypted Data: <pre>';
print_r(mc_decrypt($encrypted_data, ENCRYPTION_KEY));
echo '</pre>';

Monday 1 September 2014

HTTP response codes

HTTP response codes



HTTP Response Codes indicate whether a specific HTTP requests has been successfully completed. Responses are grouped in five classes: informational responses, successful responses, redirections, client errors, and servers errors.
The following table lists them all, with their respective meanings:
Status codeStatus textDescriptionHTTP version
Informational responses
100ContinueThis interim response indicates that everything so far is OK and that the client should continue with the request or ignore it if it is already finished.HTTP/1.1 only
101Switching ProtocolThis code is sent in response to an Upgrade: request header by the client, and indicates that the protocol the server is switching too. It was introduced to allow migration to an incompatible protocol version, and is not in common use.HTTP/1.1 only
Successful responses
200
OK
The request has succeeded. The meaning of a success varies depending on the HTTP method:
  • GET: The resource has been fetched and is transmitted in the message body.
  • HEAD: The entity headers are in the message body.
  • POST: The resource describing the result of the action is transmitted in the message body.
  • TRACE: The message body contains the request message as received by the server
HTTP/0.9 and later
201CreatedThe request has succeeded and a new resource has been created as a result of it. This is typically the response sent after a PUT request.HTTP/0.9 and later
202AcceptedThe request has been received but not yet acted upon. It is non-committal, meaning that there is no way in HTTP to later send an asynchronous response indicating the outcome of processing the request. It is intended for cases where another process or server handles the request, or for batch processing.HTTP/0.9 and later
203Non-Authoritative InformationThis response code means returned meta-information set is not exact set as available from the origin server, but collected from a local or a third party copy. Except this condition, 200 OK response should be preferred instead of this response.HTTP/0.9 and 1.1
204No ContentThere is no content to send for this request, but the headers may be useful. The user-agent may update its cached headers for this resource with the new ones.HTTP/0.9 and later
205Reset ContentThis response code is sent after accomplishing request to tell user agent reset document view which sent this request.HTTP/1.1 only
206Partial ContentThis response code is used because of range header sent by the client to separate download into multiple streams.HTTP/1.1 only
Redirection messages
300Multiple ChoiceThe request has more than one possible responses. User-agent or user should choose one of them. There is no standardized way to choose one of the responses.HTTP/1.0 and later
301Moved PermanentlyThis response code means that URI of requested resource has been changed. Probably, new URI would be given in the response.HTTP/0.9 and later
302FoundThis response code means that URI of requested resource has been changed temporarily. New changes in the URI might be made in the future. Therefore, this same URI should be used by the client in future requests.HTTP/0.9 and later
303See OtherServer sent this response to directing client to get requested resource to another URI with an GET request.HTTP/0.9 and 1.1
304Not ModifiedThis is used for caching purposes. It is telling to client that response has not been modified. So, client can continue to use same cached version of response.HTTP/0.9 and later
305Use ProxyThis means requested response must be accessed by a proxy. This response code is not largely supported because security reasons.HTTP/1.1 only
306unusedThis response code is no longer used, it is just reserved currently. It was used in a previous version of the HTTP 1.1 specification.HTTP/1.1 only
307Temporary RedirectServer sent this response to directing client to get requested resource to another URI with same method that used prior request. This has the same semantic than the 302 Found HTTP response code, with the exception that the user agent must not change the HTTP method used: if aPOST was used in the first request, a POST must be used in the second request.HTTP/1.1 only
308Permanent Redirect
This means that the resource is now permanently located at another URI, specified by the Location: HTTP Response header. This has the same semantics as the 301 Moved Permanently HTTP response code, with the exception that the user agent must not change the HTTP method used: if a POST was used in the first request, a POST must be used in the second request.
Note: This is an experimental response code whose specification is currently in draft form.
draft-reschke-http-status-308
Client error responses
400Bad RequestThis response means that server could not understand the request due to invalid syntax.HTTP/0.9 and later
401UnauthorizedAuthentication is needed to get requested response. This is similar to 403, but in this case, authentication is possible.HTTP/0.9 and later
402Payment RequiredThis response code is reserved for future use. Initial aim for creating this code was using it for digital payment systems however this is not used currently.HTTP/0.9 and 1.1
403ForbiddenClient does not have access rights to the content so server is rejecting to give proper response.HTTP/0.9 and later
404Not FoundServer can not find requested resource. This response code probably is most famous one due to its frequency to occur in web.HTTP/0.9 and later
405Method Not AllowedThe request method is known by the server but has been disabled and cannot be used. The two mandatory methods, GET and HEAD, must never be disabled and should not return this error code.HTTP/1.1 only
406Not AcceptableThis response is sent when the web server, after performing server-driven content negotiation, doesn't find any content following the criteria given by the user agent.HTTP/1.1 only
407Proxy Authentication RequiredThis is similar to 401 but authentication is needed to be done by a proxy.HTTP/1.1 only
408Request TimeoutThis response is sent on an idle connection by some servers, even without any previous request by the client. It means that the server would like to shut down this unused connection. This response is used much more since some browsers, like Chrome or IE9, use HTTP pre-connection mechanisms to speed up surfing (see bug 634278, which tracks the future implementation of such a mechanism in Firefox). Also note that some servers merely shut down the connection without sending this message.HTTP/1.1 only
409ConflictThis response would be sent when a request conflict with current state of server.HTTP/1.1 only
410GoneThis response would be sent when requested content has been deleted from server.HTTP/1.1 only
411Length RequiredServer rejected the request because the Content-Length header field is not defined and the server requires it.HTTP/1.1 only
412Precondition FailedThe client has indicated preconditions in its headers which the server does not meet.HTTP/1.1 only
413Request Entity Too LargeRequest entity is larger than limits defined by server; the server might close the connection or return an Retry-After header field.HTTP/1.1 only
414Request-URI Too LongThe URI requested by the client is too long for the server to handle.HTTP/1.1 only
415Unsupported Media TypeThe media format of the requested data is not supported by the server, so the server is rejecting the request.HTTP/1.1 only
416Requested Range Not SatisfiableThe range specified by the Range header field in the request can't be fulfilled; it's possible that the range is outside the size of the target URI's data.HTTP/1.1 only
417Expectation FailedThis response code means the expectation indicated by the Expect request header field can't be met by the server.HTTP/1.1 only
Server error responses
500Internal Server ErrorThe server has encountered a situation it doesn't know how to handle.HTTP/0.9 and later
501Not ImplementedThe request method is not supported by the server and cannot be handled. The only methods that servers are required to support (and therefore that must not return this code) are GET and HEAD.HTTP/0.9 and later
502Bad GatewayThis error response means that the server, while working as a gateway to get a response needed to handle the request, got an invalid response.HTTP/0.9 and later
503Service UnavailableThe server is not ready to handle the request. Common causes are a server that is down for maintenance or that is overloaded. Note that together with this response, a user-friendly page explaining the problem should be sent. This responses should be used for temporary conditions and the Retry-After: HTTP header should, if possible, contain the estimated time before the recovery of the service. The webmaster must also take care about the caching-related headers that are sent along with this response, as these temporary condition responses should usually not be cached.HTTP/0.9 and later
504Gateway TimeoutThis error response is given when the server is acting as a gateway and cannot get a response in time.HTTP/1.1 only
505HTTP Version Not SupportedThe HTTP version used in the request is not supported by the server.HTTP/1.1 only

Wednesday 6 February 2013

Advanced php

Apache Module mod_cache

The following content will help you guys on how to enable caching on specific folders on your server.

There is module called cache_module in apache modules, first we need to enable it.

mod_cache implements an RFC 2616 compliant HTTP content cache that can be used to cache either local or proxied content. mod_cache requires the services of one or more storage management modules. Two storage management modules are included in the base Apache distribution:

mod_disk_cache
implements a disk based storage manager.
mod_mem_cache
implements a memory based storage manager. mod_mem_cache can be configured to operate in two modes: caching open file descriptors or caching objects in heap storage. mod_mem_cache can be used to cache locally generated content or to cache backend server content for mod_proxy when configured using ProxyPass (aka reverse proxy) 

Sample httpd.conf

#
# Sample Cache Configuration
#
LoadModule cache_module modules/mod_cache.so


#LoadModule disk_cache_module modules/mod_disk_cache.so
# If you want to use mod_disk_cache instead of mod_mem_cache,
# uncomment the line above and comment out the LoadModule line below.

CacheRoot c:/cacheroot
CacheEnable disk /
CacheDirLevels 5
CacheDirLength 3


LoadModule mem_cache_module modules/mod_mem_cache.so

CacheEnable mem /
MCacheSize 4096
MCacheMaxObjectCount 100
MCacheMinObjectSize 1
MCacheMaxObjectSize 2048


# When acting as a proxy, don't cache the list of security updates
CacheDisable http://security.update.server/update-list/



How to write Robots.txt file

In a text editor, open a file named robots.txt. Note that the name must be all lower case, even if your Web pages are hosted on a Windows Web server. You'll need to save this file to the root of your Web server. For example:

http://www.mydomain.com/robots.txt

The format of the robots.txt file is

User-agent: robot

Disallow: directories/folders or files #Google will not crawl the named files / folders here

You can use wildcards to indicate all robots, or all robots of a certain type. For example:
To specify all robots:

User-agent: * 

To specify all robots that start with the letter A:

User-agent: A*

The disallow lines can specify files or directories:
Don't allow robots to view any files on the site:

Disallow: /

Don't allow robots to view the index.html file

Disallow: /index.html

If you leave the Disallow blank, that means that all files can be retrieved, for example, you might want the Googlebot to see everything on your site:

User-agent: Googlebot

Disallow:

If you disallow a directory, then all files below it will be disallowed as well.

Disallow: /norobots/

You can also use multiple Disallows for one User-agent, to deny access to multiple areas:

User-agent: *

Disallow: /cgi-bin/

Disallow: /images/

You can include comments in your robots.txt file, by putting a pound-sign (#) at the front of the line to be commented:

# Allow Googlebot anywhere

User-agent: Googlebot

Disallow:

Robots follow the rules in order. For example, if you set googlebot specifically in one of your first directives, it will then ignore a directive lower down that is set to a wildcard.

# Allow Googlebot anywhere

User-agent: Googlebot

Disallow: 

# Allow no other bots on the site

User-agent: *

Disallow: /


Create virtual hosts in PHP with apache

Virtual hosts are mainly used to run different applications with in a single Apache server by creating different port numbers in Apache configuration.

The default port number that Apache will run is 80.

Now we will see the steps to create virtual hosts in both Windows and Linux.
Both the cases assume that we have installed wamp on D:/wamp
Creating virtual hosts Windows box 

1)Go to the directory: D:\wamp\bin\apache\Apache2.2.17\conf
2) There you will find apache.conf file, open it in any text editor.
3) In that file you will be having 2 lines
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

Now you need to include the virtual hosts file as like below.
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

4) Now Open file conf/extra/httpd-vhosts.conf in any text editor.
In that file you will be having lines like below
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

So name your virtual host below these line like below
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
NameVirtualHost *:8001

5) Then you need to set up directory of your new site. By default for virtual host 80 the following code will work, the code block follows below.

ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/httpd-2.2-x64/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common


Then we need to copy and paste the same block and make changes like below.

ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/httpd-2.2-x64/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common


ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "D:/wamp/www/"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/mydomain.com-error.log"
CustomLog "logs/mydomain.com-access.log" common


This process will finishes the creating of your first virtual host.

6) Restart your Apache server and try it in your browser like http://localhost:8001/

Creating virtual hosts Linux box


Writing a .htaccess file

In this post i am going to give examples of commonly used examples of .htaccess basics.

1) HTTP Authentication for Password protected directories 
AuthType Basic
AuthName "Password Required"
AuthUserFile /www/passwords/password.file
AuthGroupFile /www/passwords/group.file
Require Group admins

2) Change default directory page ( index page ) using htaccess
This directory indexing is useful when ever you wish to change the order of parsing file. example if you want to change the default parsing orders to index.php not index.html,, you can keep the following code in your .htaccess file.

DirectoryIndex index.php index.html index.shtml index.txt

If tou browser your site http://www.yourdomain.com/, then it will search for index.php, if index.php not exists then it will searches for index.html. 

Note: If no files(index.php index.html index.shtml index.txt) are existed then it will display the directory and its contents. In order to prevent this we can write a code as below.

DirectoryIndex index.html index.txt /cgi-bin/index.pl 

Would cause the CGI script /cgi-bin/index.pl to be executed if neither index.html or index.txt existed in a directory.

3) Allow/Deny Directory Browsing

When directory browsing is on, people accessing a URL from your site with no index page or no pages at all, will see a list of files and folders. To prevent such directory access, just place the following line in your .htaccess file.

IndexIgnore */*
OR
Options -Indexes

Many hosting companies, by default deny directory browsing and having said that, just in case you need to enable directory browsing, place the following line in your .htaccess file.

Options +Indexes

4) Redirect visitors from one page or directory to another

It’s quite simple. Look at the example lines below and place similar lines in your .htaccess file of the root folder and it will do the rest. [Remember to use permanent keyword in the line to tell the search engines that the old link has moved to the new link]. You can also setup multiple redirects using htaccess.

Syntax: Redirect permanent [old directory/file name][space][new directory/file name]

Redirect permanent /olddirectory /newdirectory
Redirect permanent /olddirectory /somedirectory/newdirectory
Redirect permanent /oldhtmlfile.htm /newhtmlfile.htm
Redirect permanent /oldhtmlfile.htm http://your-domain.com/newhtmlfile.htm

All the above lines are valid. Just remember to replace the file/directory names with actual ones.

301 Redirection
This example will redirects you from non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteRule (.*) http://www.mydomain/$1 [R=301,L]

5) Change the default index page of a directory or site

Almost every hosting company will have index.htm, index.html, index.php, index.asp, default.asp, default.html as the default index page names in their web server settings. So, in case your site or directory does not has a file name which matches a name from the list above, chances are that your visitors will either see a list of all the files and folders [through directory browsing] or will not see anything at all. To change the default index page’s name for a directory or the site, place the following line in the .htaccess file of the root folder or the particular directory for which you want to change the index page’s name.

DirectoryIndex homepage.htm
DirectoryIndex somepage.htm

To have more names, put a space between file names and it will take into considerations all those file names as possible index page names. Which means, if it finds a filename matching a list of names you supplied [in the given order] in .htaccess, then it will open that page as the index page for the directory. The below line, with multiple names, is also a valid usage:

DirectoryIndex homapage.html somepage.html myindexpage.html anything.html

6) Preventing hot linking of images from your website

If your website contains images which people from other websites are linking to and you get charged for the extra bandwidth, then placing the following lines will prevent any such image hot linking. Most of the hosting companies provide this feature in their control panel itself, such as CPanel. This trick requires mod_rewrite engine to be on in Apache on your web server.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?your-domain.com/.*$ [NC]
RewriteRule .(gif|jpg)$ – [F]

In the above code, replace [your-domain] with your actual domain name [without www], and instead of (www.\), use your actual subdomain name (sub-domain.\)

7) Prevent access to your .htaccess file (.htaccess security)

To prevent visitors from viewing your .htaccess file, place the following lines in your file. Of course, by default most Apache installations will not show .htaccess file but just in case.

<files .htaccess>
order allow,deny
deny from all
</Files>

8) Custom Error Pages

The .htaccess file will cover custom error pages. These will allow you to have your own, personal error pages (for example when a file is not found) instead of using your host's error pages or having no page. This will make your site seem much more professional in the unlikely event of an error. It will also allow you to create scripts to notify you if there is an error (for example I use a PHP script on Free Webmaster Help to automatically e-mail me when a page is not found).

You can use custom error pages for any error as long as you know its number (like 404 for page not found) by adding the following to your .htaccess file:

ErrorDocument errornumber /file.html

For example if I had the file notfound.html in the root direct
ory of my site and I wanted to use it for a 404 error I would use:

ErrorDocument 404 /notfound.html

If the file is not in the root directory of your site, you just need to put the path to it:

ErrorDocument 500 /errorpages/500.html

These are some of the most common errors:

401 - Authorization Required
400 - Bad request
403 - Forbidden
500 - Internal Server Error
404 - Wrong page

Then, all you need to do is to create a file to display when the error happens and upload it and the .htaccess file.

9) Deny/Allow Certian IP Addresses

In some situations, you may want to only allow people with specific IP addresses to access your site (for example, only allowing people using a particular ISP to get into a certian directory) or you may want to ban certian IP addresses (for example, keeping disruptive memembers out of your message boards). Of course, this will only work if you know the IP addresses you want to ban and, as most people on the internet now have a dynamic IP address, so this is not always the best way to limit usage.

You can block an IP address by using:

deny from 000.000.000.000

where 000.000.000.000 is the IP address. If you only specify 1 or 2 of the groups of numbers, you will block a whole range.

You can allow an IP address by using:

allow from 000.000.000.000

where 000.000.000.000 is the IP address. If you only specify 1 or 2 of the groups of numbers, you will allow a whole range.

If you want to deny everyone from accessing a directory, you can use:

deny from all

but this will still allow scripts to use the files in the directory.





------ Contineous............


Creating and Editing crontab Files

cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix , solaris. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.

The following example shows how to determine if an editor has been defined, and how to set up vi as the default.

$ which $EDITOR
$ EDITOR=vi
$ export EDITOR 


When you create a crontab file, it is automatically placed in the/var/spool/cron/crontabs directory and is given your user name. You can create or edit a crontab file for another user, or root, if you have superuser privileges. 

This document covers following aspects of Unix cron jobs
1. Crontab Restrictions
2. Crontab Commands
3. Crontab file – syntax
4. Crontab Example
5. Crontab Environment
6. Disable Email
7. Generate log file for crontab activity

1. Crontab Restrictions
You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use
crontab if your name does not appear in the file /usr/lib/cron/cron.deny.
If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.

2. Crontab Commands

export EDITOR=vi ;to specify a editor to open crontab file.

crontab -e Edit your crontab file, or create one if it doesn’t already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)

3. Crontab file
Crontab syntax :
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)

* in the value field above means all legal values as in braces for that column.
The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).
Notes
A. ) Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.

B.) The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .

4. Crontab Example
A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.

30 18 * * * rm /home/someuser/tmp/*

Changing the parameter values as below will cause this command to run at different time schedule below :
min hour day/month month day/week Execution time
30 0 1 1,6,12 * – 00:30 Hrs on 1st of Jan, June & Dec.
0 20 * 10 1-5 –8.00 PM every weekday (Mon-Fri) only in Oct.
0 0 1,10,15 * * – midnight on 1st ,10th & 15th of month
5,10 0 10 * 1 – At 12.05,12.10 every Monday & on 10th of every month
:

Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.

5. Crontab Environment
cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).
cron supplies a default environment for every shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh

Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.

6. Disable Email
By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .

>/dev/null 2>&1

7. Generate log file
To collect the cron execution execution log in a file :

30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log


How to enable mod_rewrite in apache web server

How to enable mod_rewrite in apache web server

In Order to enable the clean urls and .htaccess, you need to enable the mod_rewrite in Apache server.

Before enabling clean urls, the Druapl Admin will look like below.
http://www.sriniraj.com/?q=admin

After Enabling, the url looks like below
http://www.sriniraj.com/admin
In Linux
If you want to enable mod_rewrite, then open your terminal and type the following command

sudo a2enmod rewrite


After execution of above command, then restart your apache server.

Use the following command to restart your apache web server.

sudo /etc/init.d/apache2 restart


You can see what modules are loaded by using the following command
sudo apache2ctl -l / sudo apache2ctl -M

In Windows
1) Click on WAMP server which is in system tray.
2) In that drag your mouse to Apache-> Apache module
3) It will give you list of modules of Apache, click on rewrite_module and restart your Apache server.


How to take MySQL database dumps in Windows

If we want to take mysqldump in windows, then use the following syntax.

path to mysqldump execute file -h hostname -u username -ppassword > path to .sql file
For example if you installed WAMP/XAAMP server in E: drive then the mysqldump syntax will follows like below.

E:\wamp\bin\mysql\mysql5.1.36\bin>mysqldump -u root news_admin > E:/anji.sql

To restore mysql dump in the windows then use the following syntax.
Example:
E:\wamp\bin\mysql\mysql5.1.36\bin>mysql -u root newsd < E:/anji.sql


How to add Indexing to a Table in MySQL

Indexing a MySQL table is needed for fast search.
The below syntax is used to add index to a mysql table. Here i am assuming the table name is `product` and the `product_id` is a primary key in that mysql table.

alter table `product` add index `product_id`

How to change the autoincrement value in a mysql table

Initially when we create a new table with auto increment filed, the values start from 1. After some time if we want to set auto increment value to a different value, then we will use the following command in mysql.

alter table members  Engine=MyISAM checksum=1 auto_increment=1000 comment='' delay_key_write=1 row_format=dynamic charset=latin1 collate=latin1_swedish_ci


How to take MySQL database dumps in Terminal.

Use the following command to do MySQL dump of your database:
 mysqldump -h <hostname> -u <database user name> -p<Database Password> <database name> > <path><filename.sql>

How to restore MySQL dump into our current Database: 
mysql -h <hostname> -u<database  user name> -p<Database password> <Database Name> < <path/filename.sql>

How to take backup of only the table structure of MySQL Database:
 mysqldump -h <hostname> -u <database user name> -p<Database Password> <database name> --no-data > <path><filename.sql>

How to take backup of selected MySQL tables: mysqldump -h <hostname> -u <database user name> -p<Database Password> <database name> <tablename1> <tablename2> ..... <tablenameN>  > <path><filename.sql>