Tuesday, May 16, 2023

Captcha PHP

 <?php

session_start();

$code=rand(1000,9999);

$_SESSION["code"]=$code;

$im = imagecreatetruecolor(50, 24);

$bg = imagecolorallocate($im, 22, 86, 165); //background color blue

$fg = imagecolorallocate($im, 255, 255, 255);//text color white

imagefill($im, 0, 0, $bg);

imagestring($im, 5, 5, 5,  $code, $fg);

header("Cache-Control: no-cache, must-revalidate");

header('Content-type: image/png');

imagepng($im);

imagedestroy($im);

?>


Encrypt & Decript PHP

<?php

function encrypt( $q ) {

$cryptKey  = 'qJB0rGtIn5UB1xG03efyCp';

$qEncoded      = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), $q, MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ) );

return( $qEncoded );

}

function decrypt( $q ) {

$cryptKey  = 'qJB0rGtIn5UB1xG03efyCp';

$qDecoded      = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), base64_decode( $q ), MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ), "\0");

return( $qDecoded );

}

?>

----------------------------------------------------------------------------------------------------------------------------

<?php

 $id = "USR13";

$Encrypted = encrypt($id);

$Decrypted = decrypt($Encrypted);

?>

<a href="index.php?id=<?php echo $Encrypted?>">Enkrip</a>

<a href="index.php?id=<?php echo $Decrypted?>">Dekript</a>

?>


Sunday, May 14, 2023

Cek Posisi Mouse Di Browser Dengan Vanilla Javascript

1. Cek Posisi Mouse

var y = window.pageYOffset;

var x = window.pageXOffset;

window.addEventListener("click", (e) => {

    console.log(`mouse location = X: ${e.x}, Y: ${e.y}`)

  }) 

2. Set Default Offset Page

  function up() {

    window.pageYOffset = 0;

  }



Beberapa CaraSum Array Vanilla Javascript

 1. Cara Pertama

    const arr = [1, 2, 3, 4];

    sum = arr.reduce((a, b) => a + b, 0);

    console.log(sum)

2. Cara Kedua

var arr = [1, 2, 3, 4];

var total = 0;

for (var i in arr) {
  
      total += arr[i];

}

console.log(total)

Mencari Index dalam sebuah array vanilla javascript dan menjumlahkan sisi kiri dan sisi kanan array

1. Menjumlahkan array sisi kanan dan sisi kiri dari sebuah index

function findEvenIndex(arr)

{

  for(let i = 0; i < arr.length; i++) {

    let leftTotal = arr.slice(0, i).reduce((t, v) => t + v, 0);

    let rightTotal = arr.slice(i + 1).reduce((t, v) => t + v, 0);

   

    if (leftTotal === rightTotal) {

      return leftTotal; // find total left or right

    }

  }

  return -1;

}

console.log(findEvenIndex([1,2,3,4,5,4,3,2,1]))

2. Mencari sebuah index dari array 

function findEvenIndex(arr) {

  let index = -1;

  for (var i = 0; i < arr.length; i++) {

    let start = arr.slice(0, i+1).reduce((a, b) => a + b, 0);

    let end = arr.slice(i).reduce((a, b) => a + b, 0)

    if (start === end) {

      index = i

    }

  }

  return index;

}

console.log(findEvenIndex([1,2,3,4,3,2,1]))


 

Cek String Jika Tipe String Huruf Kecil Semua

 


  function XO(str) {

    var x = [];

    var o = [];

    for (var i = 0; i < str.length; i++) { 

        if (str[i].toLowerCase() === 'x') { 

            x.push(str[i]); 

        } else if (str[i].toLowerCase() === 'o') {

            o.push(str[i]);

        }

    }

    if (x.length == o.length) {

        return true;

    } else {

        return false;

    }

}

console.log(XO('xo'));

console.log(XO('xxo'));

console.log(XO('xoX'));

console.log(XO('xoOX'));


Membuat Huruf Depan Kapital dengan Vanila Javascript

function capitalize(str) {

  strVal = '';

  str = str.split(' ');

  for (var chr = 0; chr < str.length; chr++) {

    strVal += str[chr].substring(0, 1).toUpperCase() + str[chr].substring(1, str[chr].length) + ' '

  }

  return strVal;

}


console.log(capitalize('hello world post'));

 

Membaca Value Form Input Type Radio Javascript

 1. Form

 <input type="radio" name="usia" value="Gen X">

  <input type="radio" name="usia" value="Gen Z">

  <button onclick="check()">check</button>

2. Javascript

function check(){

var usia = document.querySelector('[name=usia]:checked').value;

console.log(usia)

}

Membaca Value Form Input Type Checkbox dengan Vanilla Javascript

 

1. Form

  <input type="checkbox" name="nama[]" value="robert">

  <input type="checkbox" name="nama[]" value="edward">

<button onclick="check()">Check</button>

2. javascript

 function check() {

    var list = [];

    var cboxes = document.getElementsByName('nama[]');

    var len = cboxes.length;

    for (var i = 0; i < len; i++) {

      if(cboxes[i].checked){

          list.push(cboxes[i].value)

      }

    }

    console.log(list.join(",").toString());

  }

Membuat Bilangan Prima Dengan Vanilla Javascript

 function isPrime(nums) {

  if(nums < 2 ){

      return false; 

  }  

  for(let i = 2; i < nums; i++){

      if(nums % i == 0){

        return false // modulus

      }

  } 

  return true; 

}

console.log(isPrime(2)) true

console.log(isPrime(4)) false

console.log(isPrime(5)) true

console.log(isPrime(6)) false

console.log(isPrime(7)) true

console.log(isPrime(9)) false

Menjadi Programmer Bukan Hanya Tentang Coding, Tapi Tentang Mempermudah Kehidupan

Semenjak menjadi seorang programmer, ada satu cara pandang yang perlahan berubah dalam diri saya. Saya mulai berpikir bahwa ilmu pemrograman...