Valid Url Format Checking Inwards Codeigniter 2.X Using Custom/Extended Bird Valdiation Library

CodeIgniter lacks amongst URL validation. The solely i that it provides is called prep_url which does cypher to a greater extent than thence prepend "http://" to URLs if missing.

Create a file inward the application/library directory called MY_Form_validation.php. Make the cast refer MY_Form_validation every bit well. Be certain that it extends CI_Form_validation, as well as that it calls the nurture constructor. This volition permit y'all to extend the built-in validation library as well as add together additional validators to it.
Then inward your code, y'all tin merely add together additional validators like:
 $rules['URL']  = 'required|prep_url|valid_url_format|url_exists'; 

The ii novel validators are valid_url_format which checks for a properly formatted URL as well as url_exists which checks for a valid server. You should prefix the ii amongst prep_url thence that if they don’t larn into a system the other validators won’t fail.
 if (!defined('BASEPATH')) exit('No straight script access allowed');   /**  * Additional validations for URL format.  *  * @package      Module Creator  * @subpackage  ThirdParty  * @category    Libraries  * @author  Anup Shakya   * @created 01/10/2013  */   cast MY_Form_validation extends CI_Form_validation{          world business office __construct()    {      parent::__construct();     }                                 /**      * Validate URL format      *      * @access  world      * @param   string      * @return  string      */     business office valid_url_format($str){         $pattern = "|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i";         if (!preg_match($pattern, $str)){             $this->set_message('valid_url_format', 'The URL y'all entered is non correctly formatted.');             render FALSE;         }           render TRUE;     }              // --------------------------------------------------------------------             /**      * Validates that a URL is accessible. Also takes ports into consideration.       * Note: If y'all come across "php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or non known"       *          thence y'all are having DNS resolution issues as well as postulate to ready Apache      *      * @access  world      * @param   string      * @return  string      */     business office url_exists($url){                                            $url_data = parse_url($url); // scheme, host, port, path, enquiry         if(!fsockopen($url_data['host'], isset($url_data['port']) ? $url_data['port'] : 80)){             $this->set_message('url_exists', 'The URL y'all entered is non accessible.');             render FALSE;         }                                  render TRUE;     }   } // END Form Validation Class  /* End of file My_Form_validation.php */ /* Location: ./application/libraries/My_Form_validation.php */ 
In this way, y'all tin extend the Form_validation library without touching nitty-gritty library. Ref: http://brianistech.wordpress.com/2010/11/19/url-validation-in-codeigniter/
Sumber http://developer-paradize.blogspot.com

Comments

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?