0

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>
Nahita
  • 1
  • 2
  • Does this answer your question? [how to get the cookies from a php curl into a variable](https://stackoverflow.com/questions/895786/how-to-get-the-cookies-from-a-php-curl-into-a-variable) – Vandalin Dec 15 '20 at 19:19
  • 1
    @Hankov, Nahita said that cookies are set by JavaScript, so this can't be solved with any PHP and cURL method I know. – Gander Dec 15 '20 at 19:59
  • @Hankov I used the codes in the link you showed. I received some of the cookies and some I did not. I probably couldn't get cookies created by js as Gander said. I added the javascript code I thought to be creating a cookie to the question. – Nahita Dec 15 '20 at 21:56

0 Answers0