Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Make a slug on codeigniter

     The term Slug it meant by shortening the title link of a post. For example we make the postings under the heading 'Make a slug on Codeigniter'. In the title the post URL length must also like 'http://www.codexlist.net/2016/02/make-a-slug-on-codeigniter'. But if we make use of the functions of the slug that had been long URLs can be such a short 'http://www.codexlist.net/2016/02/slug'. So the function of the slug in the wordpress.com is a url shortening for the post title too long, let the url we can easily on the detection by search engines such as google, yahoo, bing and others.

     To make slug is actually very easy. In principle the slug will replace the ID. So first we need to add a slug column in the table of our news. Then create a function that will convert slug headlines into a slug. To make the functions of the slug, make a file slug_helper.php in the application folder/helper. The following is the content of the slug_helper.php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
function slug($text)
{
    // replace non letter or digits by -
    $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
 
    // trim
    $text = trim($text, '-');
 
    // transliterate
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
 
    // lowercase
    $text = strtolower($text);
 
    // remove unwanted characters
    $text = preg_replace('~[^-\w]+~', '', $text);
 
    if (empty($text))
    {
        return 'n-a';
    }
 
    return $text;
}
 
How to call him very easily. You simply load the slug helper in config/autoload.php 

$autoload['helper'] = array('slug');

After you load the helper, you can directly use it like the following example.

public function create_action()
    {
        $this->_rules();
 
        if ($this->form_validation->run() == FALSE) {
            $this->create();
        } else {
            $data = array(
'news_title' => $this->input->post('news_title',TRUE),
'news_content' => $this->input->post('news_content',TRUE),
'slug_news' => slug($this->input->post('news_title',TRUE)),
    );
 
            $this->news_model->insert($data);
            $this->session->set_flashdata('message', 'Create Record Success');
            redirect(site_url('admin/berita'));
        }
    }
   
     In the above example, we add a slug before saving function to slug_news clogs. To form need not be shown because slug are taken directly from the input news_title. so we add the title of the news 'this is the news title' then the slug will be a ' this-is-news-title'. 
     To prevent the same slug, you need to set the column being unique or you can perform the validation. For example for an existing slug can be added behind him increments.
     So short way makes the slug that will improve your website SEO score. May be useful. If you have any questions please write in the comments field is Yes. Thank you.

Setting the CSRF Protection in Codeigniter 3.0

     CSRF (Cross-site Request Forgery) is one of the types of security attacks web to get or send a request to be executed upon the victim, without the authority he wants. CSRF attacks can occur because there is no protection mechanism security token (token request) on a website, so an attacker can send a request (for example: submit a form) illegally IE not via the form on the website directly. If the victim is not careful, the attack can be successful CSRF is done by forcing the user to do the request change data such as personal profile, email addresses, even more dangerous conduct transactions transfer funds.



     For it is important to apply the CSRF protection mechanism (CSRF Protection) on our website. This time we will discuss setting the CSRF Protection in web-based application Codeigniter 3.0. The mechanism of the CSRF Protection is actually already in the Codeigniter framework since version 2. x. x, but there are several version of Codeigniter 3.0 additional settings that are provided to enhance the level of security.

Configuration.  

     To enable CSRF Protection in Codeigniter 3.0 we need to change the settings in the file/application/config/config.php


    
$config['csrf_protection'] = TRUE;    // SET TRUE TO ENABLE CSRF PROTECTION
$config['csrf_token_name'] = 'csrf_test_name';        // NAME CSRF TOKEN TO BE USED
$config['csrf_cookie_name'] = 'csrf_cookie_name';    // NAME CSRF COOKIE TO BE USED
$config['csrf_expire'] = 7200;        // CSRF TOKEN EXPIRED TIME PERIOD (IN SECONDS)
$config['csrf_regenerate'] = TRUE;    // IF TRUE THEN THE CSRF TOKEN WILL BE REPEATED TO GENERATE EACH IN REQUEST
$config['csrf_exclude_uris'] = array();    // THE ARRAY CONTAINS A LIST OF URIS THAT DO NOT USE CSRF PROTECTION

    The above settings are the settings of the CSRF Protection in Codeigniter 3.0. There are some additional configuration that does not exist in Codeigniter 2. x. x as $config [' csrf_regenerate '] and $config [' csrf_exclude_uris ']. With additional settings that are provided in the Codeigniter 3.0 we can easily set up any Action in a Controller that does not need to apply a CSRF Protection, for example, Action in the form of AJAX. As well as additional configuration $config [' csrf_regenerate '] which allows the CSRF Token is generated for every request that is made, so that the applications become more secure.

Form.  

    With enable CSRF Protection in Codeigniter 3.0 we need to modify the form that we created using the form helper from Codeigniter. This is necessary because Codeigniter will add automatically the input token on any form that we make.

 
echo form_open(site_url('register/index'), array('class' => 'form-horizontal', 'role' => 'form');
. . . . .
echo form_close();

    May be quite troublesome if it should change the form we have made using the form helper. Another alternative that can be done is to add a hidden input fruit that contains a CSRF Token in every form we have made.
  
. . . .

AJAX Request. 

    With enable CSRF Protection then for all POST request must include the CSRF token in order to request votes valid. So too for the POST request in the form of AJAX. There are two ways that can be done to address this. The first of these we can add the action or function the controller in the form of an AJAX request into the Configuration $config [' csrf_exclude_uris '], so the CSRF Protection will be on leave for the action or function. The second one we might add CSRF Token for all AJAX POST request we will do. If AJAX request we do using jquery, we can use the function $. ajaxSetup to automatically include the CSRF Token in all AJAX requests that we do.
  
var csfrData = {};
csfrData['security->get_csrf_token_name(); ?>'] = 'security->get_csrf_hash(); ?>';
$.ajaxSetup({
data: csfrData
});
Or we could also add CSRF Tokens one by one in the AJAX request we do.
 $.ajax({
    url: '',
    type: 'POST',
    dataType: 'json',
    data: {
        data: 'data', 
        security->get_csrf_token_name(); ?>: 'security->get_csrf_hash(); ?>'
    },
});