I want to login to a site with curl. However, when the login form page is accessed for the first time, cookies are assigned with javascript. I first want to connect with curl and save this cookie, then connect with curl to post form information to the URL where the login form is post. However, I guess I cannot save the cookie information in the first session. Var_dump ($ _ COOKIE) output comes as blank. I could not cope with the curl code samples I found on the internet. I would appreciate if you could share a working code sample. I'm sorry my English is not good. Note: Site names are representative.
I added the javascript code I thought to be creating a cookie to the question.
<?php
define('USERNAME', 'user');
define('PASSWORD', 'mypassword');
define('USER_AGENT', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36');
define('COOKIE_FILE', 'cookie.txt');
define('LOGIN_FORM_URL', 'https://login.site.co/login.jsp');
define('LOGIN_ACTION_URL', 'https://login.site.co/TokenVerify');
$postValues = array(
'username' => USERNAME
);
//GET
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, LOGIN_FORM_URL);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, LOGIN_FORM_URL);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
//run
curl_exec($curl);
//error control
if(curl_errno($curl)){
throw new Exception(curl_error($curl));
}
//POST
curl_setopt($curl, CURLOPT_URL, 'LOGIN_ACTION_URL');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postValues));
curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
echo curl_exec($curl);
//javascript code
<script type="text/javascript">
$(document).ready(function () {
$('.lpCaptchaRefresher1').click(function () {
var data = $('.lpCaptchaPicture').attr('src');
var arr = data.split('?');
var cp_src = arr[0] + '?t=';
$('.lpCaptchaPicture').attr('src', cp_src + new Date().getTime());
return false;
});
});
</script>
<script type="text/javascript">
function captcha()
{
$(this).attr('disabled', true);
var data = $('.captcha').attr('src');
var arr = data.split('?');
var cp_src = arr[0] + '?captchaId=';
var cid = Math.floor(100000000 + Math.random() * 900000000);
$('.captcha').attr('src', cp_src + cid);
var data1 = document.getElementById('s1').src;
var arr1 = data1.split('?');
var cp_src1 = arr1[0] + '?captchaId=';
var data2 = document.getElementById('s2').src;
var arr2 = data2.split('?');
var cp_src2 = arr2[0] + '?captchaId=';
document.getElementById("cid").setAttribute('value', cid);
document.getElementById('s1').src = cp_src1 + cid;
document.getElementById('s2').src = cp_src2 + cid;
document.getElementById('aud').load();
};
</script>
<script>
function setLocationCookie(name, value, loc) {
setCookie(name, value);
location.href = loc;
}
function setCookie(name, value)
{
var expires = "";
var date = new Date();
date.setTime(date.getTime() + (60 * 2000));
expires = "; expires=" + date.toUTCString();
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
</script>