Block bad IP addresses from accessing your website with .htaccess

If you want to block a bad IP address from accessing your website, and you have the IP address, host name or you know ISP of the visitor then you can easily block the visitor accessing your website with .htaaccess file. You can even secure a part of the website from accessing by placing a .htaccess file in the folder and the code into it.

A small tips: If you have WordPress website in which you want placing your code to block users then i would recommend that always place code after or before # BEGIN WordPress  and # END WordPress lines.

htaccess code to ban an IP


To ban a single IP place the below code into your .htaccess file and upload it to the root folder on your server.
order allow,deny
deny from 106.223.197.90
allow from all

In above example, I have blocked this IP to accessing my website and now this IP cannot visit any part of my website. However, in future if I want to give this IP acccess again, then I can remove the line from my htaccess and the IP can access the website again. The syntax to block a visitor is deny from the IP/ISP.


How to ban multiple IP addresses?

If you want to ban multiple IPs from accessing your website then also the syntax is same and you will need to add them in new line as below:
order allow,deny
deny from 106.223.197.90
deny from 106.223.197.91
deny from 106.223.197.92
allow from all

Ban a range of IPs

If you want to ban a full range of  IPs for example 106.223.197.1 to 106.223.197.225, you can easily ban as below:
order allow,deny
deny from 106.223.197.
allow from all

Ban an ISP in htaccess

Blocking an ISP is same as blocking an IP, you will need to replace the IP with the ISP. See example:

order allow,deny
deny from airtel.com
allow from all

Add custom class to WordPress menu using wp_nav_menu function

WordPress has a very good functionality of creating menu by dragging and dropping to required places and creating child or sub child menu in same way, that helps a layman to create menu very easily within a minute or two. However; if you are a developer and you want to go deeper in the functionality such as if you want to add a class to the parent UL or you want to add an ID to the parent UL tag then you will need to edit the source code.

Generally, we call wp_nav_menu() as following:
<?php wp_nav_menu( array('theme_location' => 'primary','menu_id' => 'mainHdrMenuId','menu_class' => 'hdrnavLst pull-right', )); ?>

In Above example, I have used 'primary' navigation in theme_location parameter that i already have created a menu in WordPress admin and also assigned 'mainHdrMenuId' as menu id and added two class in the menu_class parameter However; the classes and ids will be applied to the parent div section and not in UL tag.

If you want to add the class in UL section then you must have to define ul as a container in wp_nav_menu() function and then the defined id and classes will applied only to ul tag not to the parent div that WordPress by default add. See the example below:
<?php wp_nav_menu( array('theme_location' => 'primary','container'=> 'ul', 'menu_id' => 'mainHdrMenuId','menu_class' => 'hdrnavLst pull-right', )); ?>

I Hope this trick will help you. Please share your feedback in the comment box below.

How to get last insert ID in Codeigniter

I assume, you must be creating a website where you are looking to get last insert id of executed INSERT query in parent table and then you want to store the last inserted ID in your child table as a foreign key.

In this post, i am going to share with you guys a small Codeigniter method that will help you to get last inserted id.

Here it is:
$this->db->insert_id();

Run this peace of code after insert query and this method will return you last inserted id of the executed query.

I hope this post will help.

How to get file name from a URL using PHP

Are you creating an application in which you require a functionality that will copy the file from a given URL and then store into a directory and while coping the file you want to get filename of from given URL and then you want rename that according to your requirement. Am I right? Here, I an going to write a code that will help us to get file name from a URL using PHP inbuilt function basename().


Here is the code:
<?php
echo basename("http://example.com/image.png");
?>

Above code will output the file name as 'image.png'.

How to download image from a URL using PHP

Are you creating an application where a candidate can register his profile and can upload his profile picture or you are creating an application where your system will download the file from given URL? if yes then you are at right place because here in this article i am going to writing a code that will copy the file from given URL and will store in a specified directory. So without wasting time, lets do it.

In this example, i am going use PHP inbuilt function copy() to copy the file from URL.
<?php
copy('http://example.com/akhilesh.jpg', 'local/folder/local.jpg');
?>

In above code, I have used two path first one for server path (a file URL which you want be copied) and second one is your local server directory path in which you want the file to be stored.

I hope this article will help you a lot.

How to remove query string parameter using jQuery

In this article we will talk about How to remove query string parameters using jQuery. First of all, we will understand why we use query string in a URL. We use query string in a URL to get required parameter value to execute in the current loaded page or to track the source of direction. However; URLs with lots of query strings looks ugly, a normal visitor cannot memorize that and now days search engines are also not preferring this technique also is more secure.


So, here is the code that will help you in removing this dirty query strings from your page URL:
<script type="text/javascript">
$(document).ready(function(){
    if (window.location.href.indexOf('?') > -1) {
        history.pushState('', document.title, window.location.pathname);
    }
});
</script>

Please note that, above code will remove the query strings from the page URL and the page will not be reloaded and some functionalities running on the page can be stopped. I recommend you before using this code, please check this code is not affecting running functionalities on your page/module etc.

Please feel free to share your feedback in the comment box below or if you have any question, you can ask too. Best regards, Akhilesh 

Start jQuery After Page Has Fully Loaded

Assume that, you are creating an application using jQuery with any server side language for example PHP and you want to run an AJAX code when your page is fully loaded and then fire your code. In this case using $(document).ready() is not going to fulfill your expectations and you will wish to find a better solution that can help you out from this. This tutorial is going to show a method to you to run your code when your page is fully loaded.

If you want to run your code when the page is fully loaded then you can use following code:

<script type="text/javascript">
jQuery(window).on("load", function () {
alert('hi akhilesh');
});
</script>
The window load event execute when the page is fully loaded means, all the images, js files, css files and other objects.

However, if you want to run fire your jQuery code when the HTML is loaded and the DOM is ready then use document ready event. See example below:

<script type="text/javascript">
$(document).ready(function() {
alert('hello akhilesh');
});
</script>


How to count number of files in a directory using PHP

If you are creating a gallery page or creating a job portal where a candidate can upload their CVs and other files in different formats and suddenly your boss asked you to tell him how many profiles have been created with the CVs, in this situation you have two options either you count the files one by one or you just simply run a PHP code that will return you number of files uploaded. Even, you can create a module in your application that will tell you how many profiles are created till date who have uploaded their CVs. So, here in this post i am going to share with you a small but useful PHP code that will help you in counting number of files uploaded in a directory.
$countfiles = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
printf("There were %d CVs", iterator_count($countfiles));

In above code, i have used PHP inbuilt class FilesystemIterator.

Please note that, this code will only work if you server have installed PHP 5.3 or higher.