Kendala yang saya temui sewaktu mengakses website yang otomatis menggenerate token CSRF setiap halaman website tersebut dibuka, proteksi ini digunakan di web yang menggunakan framework (laravel, codeigniter, dll).

Untuk solusi masalah diatas, kita akan menggunakan cURL dan PHP. Tahapannya dibagi menjadi 2, pertama mengambil token, kedua mengirim data POST tanpa membuka ulang websitenya

// tahap pertama
$url = 'https://URL-WEBSITE';
$POSTDATA = 'APA-YANG-MAU-DI-KIRIM'
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_close ($ch);
 
// tahap kedua
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTDATA);
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt'); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$content = curl_exec ($ch);
print_r($content);
curl_close ($ch);

file cookies di simpan di /tmp/cookie.txt, ganti menjadi cookie.txt bila anda menggunakan Windows.

Join the Conversation

1 Comment

Your email address will not be published. Required fields are marked *