Tuesday 20 September 2016

Compress and Uncompress Data in PHP

Compress string and data in php. Some long strings has to be compressed in-order to pass the data. Use this below function to compress data in php and to uncompress data in php.

Compress Data :
gzcompress($string);

Uncompress Data :
gzuncompress($compressed_string);

Monday 19 September 2016

PHP Function to List Files From Directory

PHP function to list the files in the given path. List files from a directory with this php functions as link, so that you can open the files in new tabs.

function listFiles($path) {
    if(is_dir($path)) {
          if($handle = opendir($path)) {
              while(($file = readdir($handle)) !== false) {
                  if($file != "." && $file != ".." && $file != "Thumbs.db") {
                      echo '<a target="_blank" href="'.$path.$file.'">'.$file.'</a><br>'."\n";
                  }
              }
              closedir($handle);
          }
    }
}

Sunday 18 September 2016

PHP Validation Function for Maximum String Length

Useful PHP form validation function to validate the length of the string against the maximum allowed string length which in default has a maximum length of 10. Use this validation function to validate the string length of a string.

function validateMaxLength($allowed_length, $allowed_length=10) {
    return (strlen($allowed_length) <= (int)$allowed_length);
}

Saturday 17 September 2016

PHP Function to Validate URLs

Usage of simple URL validation function in php. This simple function helps in validation of URL for correct format, this also validates for mailto links in the content.

function validateURL($url) {
    return (bool)preg_match("^((((https?|ftps?|gopher|telnet|nntp)://)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';/?:@&=+$,A-Za-z0-9])+)([).!';/?:,][[:blank:]])?$",$url);
}

Friday 16 September 2016

Check for Script Tags in String using PHP Functions

In order to make hacking attempts fail we need to make our input contents safe and secure. Here to avoid unwanted issue due to script injection we make the content/string safe of script tags. Thus script hacking cannot be done in our contents.

function validateJsScriptSafe($value) {
    return (bool)(!preg_match("/<script[^>]*>[srn]*(<!--)?|(-->)?[srn]*</script>/",$value));
}

Thursday 15 September 2016

PHP Validation Function for HTML Safe

Simple php function to validate html tags in given content. In some cases we need to check if the given string contains html tags in order to avoid hacking attends and also to avoid unwanted elements changing the layout of our site.

function validateHtmlSafe($value) {
    return (bool)(!preg_match("/<(.*)>.*<$1>/", $value));
}

Wednesday 14 September 2016

Submit URL to Search Engines for Indexing

In order to generate organic traffic to our website, we need our site links to be indexed in search engines. For this, we need to make search engines crawl our URLs. This function helps in submitting our URLs and sitemap XML links to major search engines so that our links gets indexed easily. We need to pass the URL as a parameter for the function is all we need to do.


function submitUrl2SearchEngine($site_url) {
    // Eg: $site_url = http://www.demowebsite.com/sitemap.xml
    @file_get_contents("http://www.google.com/webmasters/sitemaps/ping?sitemap=$site_url");
    @file_get_contents("http://www.bing.com/webmaster/ping.aspx?siteMap=$site_url");
    @file_get_contents("http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=YahooDemo&url=$site_url");
    @file_get_contents("http://submissions.ask.com/ping?sitemap=$site_url");  
}