Pages

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. }

Emmet

Basically, most text editors out there allow you to store and re-use commonly used code chunks, called “snippets”. While snippets are a good way to boost your productivity, all implementations have common pitfalls: you have to define the snippet first and you can’t extend them in runtime.

Emmet takes the snippets idea to a whole new level: you can type CSS-like expressions that can be dynamically parsed, and produce output depending on what you type in the abbreviation. Emmet is developed and optimised for web-developers whose workflow depends on HTML/XML and CSS, but can be used with programming languages too.

How Does It Work?
Writing HTML code takes time with all of those tags, attributes, quotes, braces, etc. With Emmet instantly expands simple abbreviations into complex code snippets.


For example:

type "ul>li.item$*5" in your editor and the result is below

<ul>
    <li class="item1"></li>
    <li class="item2"></li>
    <li class="item3"></li>
    <li class="item4"></li>
    <li class="item5"></li>
</ul>

type "h$[title=item$]{Header $}*3" in your editor and the result is below

<h1 title="item1">Header 1</h1>
<h2 title="item2">Header 2</h2>
<h3 title="item3">Header 3</h3>


For more example --> http://docs.emmet.io/cheat-sheet/

Sublime Text Extension






Extension include
:- All Autocomplete
:- AndroidSnippets
:- AngularJs
:- AutoFileName
:- AutoPrefixer
:- Bootstrap 3 Snippets
:- Brackets Color Scheme
:- Color Picker
:- DocBlockr
:- Emmet
:- Highlight Build Errors
:- HTML-CSS-JS Prettify
:- Ionic Framework Snippet
:- Jquery
:- Jquery Snippets Pack
:- Mou Markdown App
:- PackageControl
:- Brogrammer + SetUI
:- Sublimelinter


Download Link -> https://drive.google.com/file/d/0B8a5Z2UlJyvlNGlzSllnX2h5cEU/view?usp=sharing

Dynamically change background color randomly

Hexadecimal color values are supported in all major browsers.

A hexadecimal color is specified with: #RRGGBB, where the RR (red), GG (green) and BB (blue) hexadecimal integers specify the components of the color. All values must be between 00 and FF.

For example, the #0000FF value is rendered as blue, because the blue component is set to its highest value (FF) and the others are set to the lowest value (00).

Below is the script how to get randomly color.

Javascript Version

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
var color = "0123456789ABCEDF";

function changeColor() {
    var temp = "";
    for (var a = 0; a < 6; a++)
        temp += color[Math.floor(Math.random() * color.length)];


    document.body.style.backgroundColor = "#" + temp;
}

function start() {
    setInterval("changeColor()", 1000)
}

start();

Odd Even Caesar Cipher

Same concept as Caesar cipher but we do alternating even and odd shifting. For even, shift to the right and for the odd, shift it the left.

For example: SAYA MAKAN NASI

Cipher     : XVDV HFFFI IFNN


To decrypt it just start from odd shift. For decrypt without key, try from 1-26 shifting method. :)



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.util.*;
import java.io.*;

public class OddEvenCaesar{
    
    public static void main(String[] args){
      System.out.print(Encode("Saya makan nasi",5));
      System.out.print(Decode("Xvdv hfffi ifnn",5));
      DecodeNoKey("Xvdv hfffi ifnn");
    }
    
    public static String Encode(String enc, int offset) {
        offset = offset % 26;
        StringBuilder encoded = new StringBuilder();
        
        int count =0;
        
        for (char i : enc.toCharArray()) {
            if(count%2 == 0){
               if (Character.isLetter(i)) {
                   if (Character.isUpperCase(i)) {
                       encoded.append((char) ('A' + (i - 'A' + offset) % 26 ));
                   } else {
                       encoded.append((char) ('a' + (i - 'a' + offset) % 26 ));
                   }
               } else {
                   encoded.append(i);
               }
            }
            else if(count%2 == 1){
               if (Character.isLetter(i)) {
                   if (Character.isUpperCase(i)) {
                       encoded.append((char) ('A' + (i - 'A' + 26-offset) % 26 ));
                   } else {
                       encoded.append((char) ('a' + (i - 'a' + 26-offset) % 26 ));
                   }
               } else {
                   encoded.append(i);
               }
            }
            
            count++;
        }
        return encoded.toString();
    }
    
    public static String Decode(String enc, int offset) {
        offset = offset % 26;
        StringBuilder decoded = new StringBuilder();
        
        int count =0;
        
        for (char i : enc.toCharArray()) {
            if(count%2 == 1){
               if (Character.isLetter(i)) {
                   if (Character.isUpperCase(i)) {
                       decoded.append((char) ('A' + (i - 'A' + offset) % 26 ));
                   } else {
                       decoded.append((char) ('a' + (i - 'a' + offset) % 26 ));
                   }
               } else {
                   decoded.append(i);
               }
            }
            else if(count%2 == 0){
               if (Character.isLetter(i)) {
                   if (Character.isUpperCase(i)) {
                       decoded.append((char) ('A' + (i - 'A' + 26-offset) % 26 ));
                   } else {
                       decoded.append((char) ('a' + (i - 'a' + 26-offset) % 26 ));
                   }
               } else {
                   decoded.append(i);
               }
            }
            
            count++;
        }
        return decoded.toString();
    }
    
    public static void DecodeNoKey(String enc) {
        
        for(int x=0;x<26;x++){
           int offset = x % 26;
           StringBuilder decoded = new StringBuilder();
           
           int count =0;
           
           for (char i : enc.toCharArray()) {
               if(count%2 == 1){
                  if (Character.isLetter(i)) {
                      if (Character.isUpperCase(i)) {
                          decoded.append((char) ('A' + (i - 'A' + offset) % 26 ));
                      } else {
                          decoded.append((char) ('a' + (i - 'a' + offset) % 26 ));
                      }
                  } else {
                      decoded.append(i);
                  }
               }
               else if(count%2 == 0){
                  if (Character.isLetter(i)) {
                      if (Character.isUpperCase(i)) {
                          decoded.append((char) ('A' + (i - 'A' + 26-offset) % 26 ));
                      } else {
                          decoded.append((char) ('a' + (i - 'a' + 26-offset) % 26 ));
                      }
                  } else {
                      decoded.append(i);
                  }
               }
               
               count++;
           }
           System.out.println(decoded.toString());
       }
    }

}