Sabtu, 26 Desember 2015

Membuat User Mysql Di Centos Agar Bisa di Akses Dari server luar

Masuk menggunakan SSH :
Jalan kan perintah berikut untuk masuk ke mysql menggunakan SSH:
# mysql -h localhost -u root -p






Masukan Passsword yang di minta.












Login sukses lalu ketikan perintah berikut ini untuk membuat user yg dapat di akses dari semua IP:
CREATE USER 'namauser'@'%' IDENTIFIED BY 'mypassword';

Tambahan untuk server ubuntu

1. gunakan perintah ini untuk memilih db mysql
mysql>use mysql;
2. set agar user root bisa di akses dari luar
UPDATE mysql.user SET Host='%' WHERE Host='localhost' AND User='root';
FLUSH PRIVILEGES;
3. buka file ini
/etc/mysql/mysql.conf.d atau di sini /etc/mysql/my.cnf
ganti ini
bind-address = 127.0.0.1
menjadi ini
bind-address = 0.0.0.0

lalu restart service mysql
sudo /etc/init.d/mysql restart

Membuat Function RoundUP dengan PHP

<?php

function round_up($value, $places)

{

    $mult = pow(10, abs($places));

     return $places < 0 ?

    ceil($value / $mult) * $mult :

        ceil($value * $mult) / $mult;

}

echo round_up(20150,-3);
Pada variable $places kita masukan -3 di sini tujuan nya adalah 3 angka di belakang ug akan di round up, jadi jika hanya akan meroundup 2 angka di belakang masukan saja -2.

Rabu, 04 November 2015

Cara Instal ODBC Driver di Linux Centos 6.x

Tutorial ini bukan hanya instalsasi ODBC saja, tetapi juga termasuk cara koneksi manggunakan PHP
Saya asumsikan anda sudah menginstall Apache dam PHP di server anda.

Pertama Jalankan ini, step ini bisa di lewati jika di server anda sudah terinstal Apache/PHP
# yum install php php-odbc wget gcc php-pear php-pecl-apc php-xml php-xmlrpc php-intl php-tidy php-imap php-pecl-memcache
Selanjutnya Jalankan ini untuk download driver ODBC CentOS 5.x
# wget http://download.microsoft.com/download/6/A/B/6AB27E13-46AE-4CE9-AFFD-406367CADC1D/Linux5/sqlncli-11.0.1790.0.tar.gz
Untuk CentOS 6.x
# wget http://download.microsoft.com/download/6/A/B/6AB27E13-46AE-4CE9-AFFD-406367CADC1D/Linux6/sqlncli-11.0.1790.0.tar.gz
Selanjutnya Extact file :
# tar xvf sqlncli-11.0.1790.0.tar.gz
dan kemudian membangunnya dengan
# cd sqlncli-11.0.1790.0
# ./build_dm.sh
Setelah itu, jalankan ini
# cd /tmp/unixODBC.5996.21582.3453/unixODBC-2.3.0
# make install
# cd /ke_direktori_dimana_sql-client_di_download/sqlncli-11.0.1790.0
Jika pada saat menjalankan cd /tmp/unixODBC.5996.21582.3453/unixODBC-2.3.0 terdapat error, coba masuk ke folder /tmp dan cari folder yang berawalan unixODBC dan arahkan ke folder tsb.
Dan terakhir adalah:
# ./install.sh install --lib-dir=/usr/local/lib64 --accept-license
Selanjutnya kita dapat men setup ODBC dengan mencari file /etc/odbc.ini dan set seperti berikut ini:
[MyDSNName]
Driver=SQL Server Native Client 11.0
Description=My Test ODBC Database Connection
Trace=Yes
Server=[My SQL Server IP address]
Port=1433
Database=[my database name]
Untuk mengetest apakah setup tsb sudah bisa di gunakan , gunakan perintah berikut ini:
# isql -v MyDSNName MSSqlUser MSSqlUserPassword
Jika Terdapat Pesan Seperti di bawah ini :
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
Ok, berarti selanjutnya adalah kita akan mencoba koneksi menggunakan PHP, gunakan script di bawah :
<?php
    $dbconn = new PDO("odbc:MyDSNName", "MSSQLUserName", "MSSQLUserPassword");
    $sql = "SELECT * FROM MSSQL_DataBase;";
    $stmt = $dbconn->prepare($sql);
    $result-> $stmt->execute();
    while($row = $stmt->fetch())
    {
        echo "$row[0] - $row[1]";
        echo "<br>\n";
    }
?>

Jumat, 30 Oktober 2015

Download Backup Database Mysql Menggunakan PHP

Langsung aja gak pake basa basi, script di bawah ini di gunakan untuk backup database menggunakan PHP, filanya bisa langsung di download atau di save di hosting juga bisa.
<?php
error_reporting(E_ALL);

/* Define database parameters here */
define("DB_USER", 'root');
define("DB_PASSWORD", '');
define("DB_NAME", 'kwitansi');
define("DB_HOST", 'localhost');
define("OUTPUT_DIR", 'dbBackup'); // Folder / Directory Name
define("TABLES", '*');

/* Instantiate Backup_Database and perform backup */
$backupDatabase = new Backup_Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$status = $backupDatabase->backupTables(TABLES, OUTPUT_DIR) ? 'OK' : 'KO';
//echo "Backup result: " . $status . " - By Asefur Mukti";
//echo '<a href="'.$backupDatabase->filename.'">Download</a>';
/* The Backup_Database class */

class Backup_Database {
/* Host where database is located  */

  var $host = '';
  var $username = '';
  var $passwd = '';
  var $dbName = '';
  var $charset = '';

  /* Constructor initializes database */

  function Backup_Database($host, $username, $passwd, $dbName, $charset = 'utf8') {
    $this->host = $host;
    $this->username = $username;
    $this->passwd = $passwd;
    $this->dbName = $dbName;
    $this->charset = $charset;
    $this->initializeDatabase();
  }

  protected function initializeDatabase() {
    $conn = @mysql_connect($this->host, $this->username, $this->passwd); // Ik Added @ to Hide PDO Error Message
    mysql_select_db($this->dbName, $conn);
    if (!mysql_set_charset($this->charset, $conn)) {
      mysql_query('SET NAMES ' . $this->charset);
    }
  }

  /* Backup the whole database or just some tables Use '*' for whole database or 'table1 table2 table3...' @param string $tables  */

  public function backupTables($tables = '*', $outputDir = '.') {
    try {
      /* Tables to export  */
      if ($tables == '*') {
        $tables = array();
        $result = mysql_query('SHOW TABLES');
        while ($row = mysql_fetch_row($result)) {
          $tables[] = $row[0];
        }
      } else {
        $tables = is_array($tables) ? $tables : explode(',', $tables);
      }

      $sql = 'CREATE DATABASE IF NOT EXISTS ' . $this->dbName . ";\n\n";
      $sql .= 'USE ' . $this->dbName . ";\n\n";

  /* Iterate tables */
  foreach ($tables as $table) {
    //echo "Backing up " . $table . " table...";

    $result = mysql_query('SELECT * FROM ' . $table);
 
    $numFields = mysql_num_fields($result);

    $sql .= 'DROP TABLE IF EXISTS ' . $table . ';';
    $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE ' . $table));
    $sql.= "\n\n" . $row2[1] . ";\n\n";

    for ($i = 0; $i < $numFields; $i++) {
      while ($row = mysql_fetch_row($result)) {
        $sql .= 'INSERT INTO ' . $table . ' VALUES(';
        for ($j = 0; $j < $numFields; $j++) {
          $row[$j] = addslashes($row[$j]);
          // $row[$j] = ereg_replace("\n", "\\n", $row[$j]);
          if (isset($row[$j])) {
            $sql .= '"' . $row[$j] . '"';
          } else {
            $sql.= '""';
          }
          if ($j < ($numFields - 1)) {
            $sql .= ',';
          }
        }
        $sql.= ");\n";
      }
    }
    $sql.="\n\n\n";
    //echo " OK <br/><br/>" . "";
  }
} catch (Exception $e) {
  //var_dump($e->getMessage());
  return false;
 }

    return $this->saveFile($sql, $outputDir);
  }

  /* Save SQL to file @param string $sql */

  protected function saveFile(&$sql, $outputDir = '.') {
    if (!$sql)
      return false;

    try {
   $this->filename = 'db-backup-' . $this->dbName . '-' . date("Ymd-His", time()) . '.sql'; 
      //$handle  = fopen($this->filename, 'w+'); //UNTUK MEMBUAT FILE
      //fwrite($handle, $sql);     //SETELAH DI BUAT LALU DI TULIS KE DALAM FILE
   header("Content-type: application/octet-stream");
   header('Content-Disposition: attachment; filename='.$this->filename);
   echo $sql;
      //fclose($handle);       //LALU FILE DI TUTUP
    } catch (Exception $e) {
      //var_dump($e->getMessage());    //TAMPILKAN PESAN
      return false;
    }
    return true;
  }

}
?>

Kamis, 22 Oktober 2015

Cara zip folder di linux dengan command

Cek apakah zip sudah terinstal di linux menggunakan perintah di bawah ini:

# which zip   (It will show the absolute path of zip)

In CentOS

# rpm -q zip

In Debian or Ubuntu

$ sudo dpkg -l zip

Jika zip belum terinstall di system gunakan perintah di bawah ini:
In CentOS or Red hat
# yum install zip

In Debian or Ubuntu
$ sudo apt-get install zip
Setelah instalasi selesai gunakan command di bawah ini untuk mengkompresi folder :
zip -r GiveAnyName.zip  /path/of/Directory

for eg.
 We have a backup directory in /root/ (i.e /root/backup)

zip -r backup.zip /root/backup

Gunakan perintah ini untuk unzip file zip:
unzip backup.zip

Senin, 19 Oktober 2015

Perintah Dasar Linux

Melihat Space Hardisk
df -h

Copy Folder
cp -avr /var/www/html /var/www/html2
/var/www/html => ini adalah folder yg akan di copy
/var/www/html2 => ini adalah tujuan paste

Menghapus Folder yang ada isi nya
rm -rf namafolder

Caranya masuk ke folder sebelum folder yg akan di hapus misalkan kita punya folder /var/www/html/manual dan folder yg akan di hapus adalah folder manual maka masuk ke folder dengan perintah cd /var/www/html lalu hapus dengan perintah rm -rf manual

Menghapus History di Command
history -cw 
Mengetahui Unkuran Besar kecilnya Folder
du -sh *

membuka koneksi database ke server lain
Membuka koneksi mysq dari cenots ke server lain (EC2)
setsebool httpd_can_network_connect_db on
Cara mencari text di dalam file (Centos)
grep -rwl "cari-text-disini" /path/folder
Agar file web bisa di akses
chown www-data:www-data -R /var/www/dev.ivendor.co.id/public_html/*

Senin, 05 Oktober 2015

Disable Pembayaran Saat Check Out Woocommerce Berdasarkan Produk Tertentu

Sometimes, you don't want to offer a particular payment gateway in your WooCommerce checkout if a customer has a certain product in their cart.
Here's a code snippet that you can add to your theme's functions.php file to help!

/*
* Disable PayPal payment method in the checkout if certain
* products are present in the cart.
* Add this to your theme's functions.php file
*/
add_filter( 'woocommerce_available_payment_gateways', 'filter_gateways', 1);
function filter_gateways( $gateways ){
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
// store product IDs in array PayPal method is disabled at checkout
$nonPPproducts = array(1111,2222,3333); // LIST YOUR PRODUCTS HERE
if ( in_array( $values['product_id'], $nonPPproducts ) ) {
unset($gateways['paypal']);
// You can unset other gateways using the gateway ID e.g. "cod", "bacs", "stripe"
break;
}
}
return $gateways;
}

You simply have to add the product(s) by their ID to the array list. If you want to disable other payment methods, you can find their Gateway ID under WooCommerce > Settings > Checkout:


Jumat, 25 September 2015

Function Number Format Untuk Javascript

function number_format(user_input){
    var filtered_number = user_input.replace(/[^0-9]/gi, '');
    var length = filtered_number.length;
    var breakpoint = 1;
    var formated_number = '';
    for(i = 1; i <= length; i++){
        if(breakpoint > 3){
            breakpoint = 1;
            formated_number = ',' + formated_number;
        }
        var next_letter = i + 1;
        formated_number = filtered_number.substring(length - i, length - (i - 1)) + formated_number;
        breakpoint++;
    }
    return formated_number;
}

Kamis, 20 Agustus 2015

Disable Enter Pada saat submit form

Berikut code javascriptnya

$('form').bind("keypress", function(e) {
  if (e.keyCode == 13) {              
    e.preventDefault();
    return false;
  }
});

Sabtu, 27 Juni 2015

Mengaktifkan mod_rewrite untuk file .htaccess

Apabila file .htaccess yang sudah anda set tapi masih belum bekerja coba set parameter berikut ini
yang perlu di set adalah server apachenya dan disina saya menggunakan centos 7
masuk ke folder /etc/httpd/conf  buka file httpd.conf ganti semua AllowOverride None menjadi AllowOverride All.

Selasa, 23 Juni 2015

Redirect page mengunakan javascript

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

Menambahkan Global variable ke dalam class php

$var1 = 'something';
class myClass{
    function doSomething(){
        global $var1 ;
        echo $var1;
    }
}

$obj = new myClass();
$obj->doSomething();

Rabu, 10 Juni 2015

Tips dan trik mysql

Menhilangkan duplikat di dalam field
ALTER IGNORE TABLE nama_tabel ADD UNIQUE (nama_field1,nama_field2)
Menggabungkan String
SUBSTRING( string, start_position, length )

CONCAT ( string_value1, string_value2 [, string_valueN ] ) 

SELECT number,name, COUNT(number)
FROM phonebook
GROUP BY number
HAVING ( COUNT(number) > 1 )


UPDATE DAN REPLACE DATA
UPDATE nama_tabel SET field_yg_diupdate=replace(field_yg_diupdate,'string lama','string baru')

Selasa, 09 Juni 2015

Memutar Audio Lebih Dari Satu Dengan Javascript

Langsung saja silahkan di copas :

<script>
function play(audio, callback) {
audio.play();
if(callback)
{
    audio.onended = callback;
}
}
function queue_sounds(sounds){

    var index = 0;
    function recursive_play()
    {
      if(index+1 === sounds.length)
      {
        play(sounds[index],null);
      }
      else
      {
        play(sounds[index],function(){index++; recursive_play();});
      }
    }

recursive_play();  
}
queue_sounds([new Audio("baritone_note_blast_loud.mp3"),new Audio("beep11.mp3")]);
</script>

Cara Koneksi ke Database MySQL dengan PHP

<?php
$server = "localhost";
$username = "root"; // ganti dengan user mysql anda
$password = ""; // ganti dengan password mysql anda
$database = "c"; // Koneksi dan memilih database di server
mysql_connect($server,$username,$password) or die("Koneksi gagal");
mysql_select_db($database) or die("Database tidak bisa dibuka");

Confirm popup sebelum close tab di browser atau pindah halaman

Penampakanya akan seperti ini apabila anda menutup browser anda atau mau pindah halaman:
atau ini :



Okeh langsung ane kasih contoh simple nya ya, buat file html seperti di bawah ni :


dan silahkan di coba di browser Anda.
Berikut ini saya jelaskan sedikit penjelasaan dari script di atas per line :
Line 1. Berfungsi untuk memanggil script jquery yang akan di gunakan pada line 10.
Line 2 dan 3 tombol untuk pindah halaman ke yahoo atau google.
Line 6 sampai 8 berfungsi untuk menampilkan confirm popup apabila anda menutup browser atau mau pindah halaman.
Line 10 sampai 15 akan bekerja apabila link dengan id "pindah-google" (line 2) di klik dan akan menghilangkan confirm popup (line 12) serta akan langsung di direct ke google.com

Switch autofocus pada form dengan javascript


Gunakan :
document.getElementById('ID').focus();

gunakan script di atas pada saat event-event yang di lakukan.
jangan menggunakan ini :
document.getElementById("ID").autofocus;

karena tidak akan berjalan pada saat proses switch field.

Senin, 25 Mei 2015

Mengatasi "500 Internal Server Error"

Jika di webhosating anda WHM anda mengalami error seperti ini :

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@namadomain.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Apache/2.2.29 (Unix) mod_ssl/2.2.29 OpenSSL/1.0.1e-fips mod_bwlimited/1.4 Server at dev.jualelektronik.com Port 80

Cobalah ganti permisions direktori/file php di webseite anda menjadi 755. Anda bisa menggantinya melalui winscp atau dari SSH Client.

Rabu, 29 April 2015

Gunakan wget -O - agar saat Cron Jobs tidak menyimpan file

Setiap kali kita set di cron job dengan tujuan menjalankan url maka secara file dari url tersebut akan tersimpan di direktori /home/. untuk mengghindari hal ini gunakan wget -O - :

Contoh :

*/20 * *  *  * wget -O - http://namadomain.com/autoload-test.php
Atau bisa menggunakan curl
*/20 * *  *  * curl http://namadomain.com/autoload-test.php