How to Send AJAX request with CSRF token in CodeIgniter 3

Cross-Site Request Forgery (CSRF) is a way to trick the server that a request sent to it is legitimate while it actually is an unauthorized attempt.

In CodeIgniter, CSRF protection is not enabled by default.

If it is been enabled then CodeIgniter generates a hash for each active user and this is used to verify the request.

Require to send the hash with the AJAX request otherwise, it gives an error – “The action you have requested is not allowed.”.

In this tutorial, I show how you can enable CSRF protection and regenerate hash for the next AJAX request and pass hash in AJAX request in the CodeIgniter project.

How to Send AJAX request with CSRF token in CodeIgniter 3

Read more

Create Treeview with jsTree plugin and PHP

If you already managing the relationship between parent and child items using a separate field then it is easier to create a hierarchical structure.

With the use of a jsTree plugin, this will be created in less time.

jsTree is a jQuery plugin that creates a hierarchical data structure using JSON data.

It allows setting icon of a node, by default open, disable a node and set it selected while initializing, etc.

In this tutorial, I show how you can make a tree structure of MySQL data using jsTree plugin and PHP.

Create treeview with jsTree plugin and PHP

Read more

Check Username Availability with AJAX PHP and PDO

On the website, there must be a unique identifier for each user. It can be the username, email-id or any string using which user can log in to the website and access it.

Before adding the user using the registration form it must be check if the username is available or not.

This can either done after submitting the form or using jQuery AJAX to display availability status before submitting the form.

In this tutorial, I show how you can check username already exists or not in the MySQL database table using jQuery AJAX PHP and PDO.

Check username availability with AJAX PHP and PDO

Read more

Login page with Remember me using PDO and PHP

The login page is the basic requirement for membership-based websites.

User needs to login to the website using its username or email and password to access the website. If the user has not logged out but the SESSION is destroyed then it needs to again login to the website.

By adding remember me on the login form the user needs to checked the remember me checkbox and login to the website. Now if the user has not logged out but the SESSION is destroyed.

When the user again accesses the website then it does not need to login and the SESSION is initiated.

In this tutorial, I show how you create a login page with remember me functionality with PDO and PHP.

Login page with Remember me using PDO and PHP

Read more