Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Wednesday, December 30, 2015

Random alphabet in PHP

Change Snippet Background Color
          
            
               function RandomAlpha($len){
                  $key = "";
                  for($x=0;$x<$len;$x++)
                     $key .= chr(ord('A') rand(0,25));
                 
                  return $key;
               }
               function RandomAlphaNoDuplicate($len){
                  $key = "";
                   while(strlen($key) < $len){
                     $key .= chr(ord('A') rand(0,25));
                     $key = implode('',array_unique(str_split($key)));
                   }
                  return $key;
               }
               
                echo RandomAlphaNoDuplicate(12);
            
          
        

Thursday, November 12, 2015

Easiest way to generate a random password in PHP

In many web app, there are alot of them use random password generator for their user such as 2 step validation, phone verification, email verification and more. By using str_shuffle() in PHP become more handy to create random password:

 * note that..this is not a secure random password.. please use other encryption method or use java randomLib
  1. <?php
  2. function characterSuffle($len = 6) {
  3.     $list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
  4.     $str = substr(str_shuffle($list), 0, $len );
  5.     return $str;
  6. }
  7. echo characterSuffle();
  8. echo characterSuffle(8);
  9. echo characterSuffle(16);
  10. echo characterSuffle(32);
  11. ?>

Saturday, November 7, 2015

How to detect IE in php and Javascript




In PHP

  1. $chrome = 'https://www.google.com/chrome/browser/desktop';
  2. if (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) ||
  3.        (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false) ||
  4.        (strpos($_SERVER['HTTP_USER_AGENT'], "Edge") == true)) {
  5.         header('Location:'.$chrome);
  6. }
  7. else{
  8.     // goto your page
  9. }

In Javascript



  1. // detect IE ersion
  2. var IEversion = detectIE();
  3. if (IEversion !== false) {
  4.     alert('You are using IE'+IEversion);
  5.     document.location.href = "https://www.google.com/chrome/browser/desktop/";
  6. }
  7. function detectIE() {
  8.     var getAgent = window.navigator.userAgent;
  9.     var msie = getAgent.indexOf('MSIE ');
  10.     if (msie > 0) {
  11.         return parseInt(getAgent.substring(msie+5, getAgent.indexOf('.', msie)), 10);
  12.     }
  13.     var trident = getAgent.indexOf('Trident/');
  14.     if (trident > 0) {
  15.         var rv = getAgent.indexOf('rv:');
  16.         return parseInt(getAgent.substring(rv+3, getAgent.indexOf('.', rv)), 10);
  17.     }
  18.     var edge = getAgent.indexOf('Edge/');
  19.     if (edge > 0) {
  20.         return parseInt(getAgent.substring(edge+5, getAgent.indexOf('.', edge)), 10);
  21.     }
  22.     return false;
  23. }