Tampilkan postingan dengan label PHP. Tampilkan semua postingan
Tampilkan postingan dengan label PHP. Tampilkan semua postingan

Senin, 07 Oktober 2013

Menyembunyikan Kode JavaScript Yang Dipakai Menggunakan PHP

BalonCoding menggunakan jQuery dan turunannya,contohnya seperti script di bawah ini,


BalCod tidak perlu menunjukkan javascript yang dipakai, karena itu kita dapat menyembunyikan link javascript dengan cara yang paling gampang adalah menggantikan nama file dengan sesuka kita misalnya 1.js, 2.js, dan seterusnya. Namun BalCod memutuskan dengan menggunakan PHP, seperti di bawah ini
<?php 
  echo file_get_contents('js/jquery-1.10.2.min.js');
  echo file_get_contents('js/jumble.min.js'); ?>

Jumat, 04 Oktober 2013

PHP Fatal error: Maximum execution time of 30 seconds exceeded in

Hari ini, BalonCoding jumpa dengan pesan error di PHP script, yakni
Fatal error: Maximum execution time of 30 seconds exceeded in C:\Apache24\htdocs\server\getstat.php on line 32

max_execution_time = 30 => max_execution_time = 360

Kamis, 03 Oktober 2013

HTML Tidak Bisa Menampilkan PHP Script

Ternyata ada yang masih kurang di file konfigurasi Apache httpd.conf, yakni php hanya dijalankan oleh file .php, sehingga file .html tidak berhasil melakukan parse terhadap PHP script. Solusinya adalah mengedit, 
AddType application/x-httpd-php .php

menjadi
AddType application/x-httpd-php .php .html

PHP di Apache Tidak Menampilkan Error

Kesalahan yang seharusnya tidak terjadi, namun sering kali menyebabkan waktu yang seharusnya terpakai untuk hal yang lebih berguna menjadi sia-sia. Kejadian seperti ini sudah sering kali BalonCoding alami, namun masih saja terjadi #tepokjidat.

Kejadian kali ini adalah PHP script yang BalCod jalankan tidak menampilkan pesan kesalahan. Sehingga dieditlah php.ini, mengaktifkan parameter 
display_errors = On
masih belum bisa. Aktifkan lagi parameter 
display_startup_errors = On
error_reporting = E_ALL
html_errors = On
ternyata belum bisa juga. Copy - paste file php.ini ke folde c:\windows\ juga belum bisa. Nah, kesalahannya adalah bahwa sudah ada parameter display_errors = Off. Sehingga walaupun di atas file php.ini ada baris display_errors = On, namun karena di bawah display_errors = Off, maka yang berlaku adalah yang Off. Jadi solusinya hanya mengedit Off menjadi On, bukan menambahkan baris lagi. ^_^ Oh, ingat restart Apache!

Menghitung Waktu Proses PHP Script

<?php   
/**
  * fungsi untuk hitung waktu proses dari waktu mulai sampai selesai
  * dengan memanfaatkan fungsi microtime
*/

// letakkan pada awal script
$start = microtime(TRUE); 

// letakkan pada akhir script
$finish = microtime(TRUE);  

// hitung selisih waktu mulai dengan waktu akhir untuk mendapatkan perbedaan waktu dalam satuan detik
$totaltime = $finish - $start;  
  
// tampilkan hasil waktu proses PHP script
echo "This script took ".$totaltime." seconds to run"; 
?>

Cek Status Website Menggunakan PHP

Berikut coding php dengan memanfaatkan fungsi curl :

<?php 
/**
 * Function to check a website status
 * @param string url the url of the website to test
 * @return boolean true if the site is up, false otherwise
*/


function checkStatus($url){
     $agent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; pt-pt) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27";


     // initializes curl session
     $ch=curl_init();


     // sets the URL to fetch
     curl_setopt ($ch, CURLOPT_URL,$url );


     // sets the content of the User-Agent header
     curl_setopt($ch, CURLOPT_USERAGENT, $agent);


     // return the transfer as a string
     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);


     // disable output verbose information
     curl_setopt ($ch,CURLOPT_VERBOSE,false);


     // max number of seconds to allow cURL function to execute
     curl_setopt($ch, CURLOPT_TIMEOUT, 5);


     curl_exec($ch);


     // get HTTP response code
     $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);


     curl_close($ch);


     if($httpcode>=200 && $httpcode<300)
          return true;
     else
          return false;
     }


     // Place this at the very top of script 
     $start = microtime(TRUE);


     if(!function_exists('curl_init')) echo "cURL not installed. Trying something else...";


     $array = array("http://192.168.51.2", 
                    "http://192.168.51.5", 
                    "http://192.168.51.18", 
                    "http://192.168.51.27", 
                    "http://192.168.51.192",
                    "http://192.168.51.215"
                    );
 
     foreach ($array as $value) {
          if(checkStatus($value))
               echo "Website <img src='img/up.png' height=16 width=16> $value is <font color='blue'>up</font><br />\n";
          else
               echo "Website <img src='img/down.png' height=16 width=16> $value is <font color='red'>down</font><br />\n";
     }


     // Place this at the very bottom of script 
     $finish = microtime(TRUE);


     // Subtract the start time from the end time to get our difference in seconds 
     $totaltime = $finish - $start; 
 
     echo "<font size='1'>This script took ".$totaltime." seconds to run</font>"; 
?>