1

I am trying to make a login php post but its not posting correctly... i need the following sent to the Curl but its just not sending

//Set the post parameters 
curl_setopt($ch, CURLOPT_POSTFIELDS, 'j_username='.$username.'&j_password='.$password.'&tk_trp ='.$tk_trp);

Is what i am currently trying to use and its just not taking is there something anyone notices that i am doing wrong? Any advice would be great thanks!

I have been stuck on this a good while now and its just not coming together for me so thought i would reach out :)

  <?php
    $token = GetStringBetween(getURL("tokenlocatonurl"),"start'", "'");
    ini_set('display_errors', 1);
    error_reporting(E_ALL ^ E_NOTICE);
    $username = 'myuser';
    $password = 'mypass';
    $tk_trp = '$token';
    $loginUrl = 'lognurlhere';

        function getURL($u){
            $u = file_get_contents("http://{$u}");
            return $u != false ? $u : "";
        }
        function GetStringBetween($string, $start, $finish){
            $string = " ".$string;
            $position = strpos($string, $start);
            if ($position == 0) return "";
            $position += strlen($start);
            $length = strpos($string, $finish, $position) - $position;
            return substr($string, $position, $length);
        }

    //init curl
    $ch = curl_init();

    //Set the URL to work with
    curl_setopt($ch, CURLOPT_URL, $loginUrl);

    // ENABLE HTTP POST
    curl_setopt($ch, CURLOPT_POST, 1);

    //Set the post parameters  curl_setopt($ch, CURLOPT_POSTFIELDS, 'j_username='.$username'&j_password='.$password'&tk_trp='.$tk_trp');

    //Handle cookies for the login
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

    //Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
    //not to print out the results of its query.
    //Instead, it will return the results as a string return value
    //from curl_exec() instead of the usual true/false.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //execute the request (the login)
    $store = curl_exec($ch);

    //the login is now done and you can continue to get the
    //protected content.

    //set the URL to the protected file
    curl_setopt($ch, CURLOPT_URL, 'url to grab content from');

    //execute the request
    $content = curl_exec($ch);



    ?>

2 Answers2

1

Problems I see with your code are:

  • You should use http_build_query to encode your data.
  • You should enable CURLOPT_COOKIESESSION.
  • You should disable CURLOPT_POST AFTER you logged in.
  • It's better to use a user agent because some sites block suspicious user agents.
  • Use both CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR and use '-' to store it in memory.

Here is final code:

<?php
$data = [
    'j_username' => $username,
    'j_password' => $password,
    'tk_trp' => $tk_trp
];
$ch = curl_init("http://example.com/login");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, '-');
curl_setopt($ch, CURLOPT_COOKIEJAR, '-');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "http://example.com/dashboard");
curl_setopt($ch, CURLOPT_POST, 0);
$x = curl_exec($ch);
echo $x;
Sky
  • 4,244
  • 7
  • 54
  • 83
  • I added the full code to my question how would i make them go together? –  Jun 24 '18 at 05:52
  • Still isnt working but im not sure im combining my code with your data code correctly –  Jun 24 '18 at 16:18
  • This has me soooo stressed out –  Jun 25 '18 at 05:11
  • The code should work okay. I tested it on another page. Check this code too: https://stackoverflow.com/a/10307956/2570054 – Sky Jun 25 '18 at 06:03
  • Its not logging in or storing cookies tho its outputting the page in non logged in form –  Jun 25 '18 at 06:11
  • @JohnSmith I updated my answer. Check the new code see if it works. – Sky Jun 25 '18 at 06:23
  • No luck with that either Hmm –  Jun 25 '18 at 06:29
  • Maybe there is some kind of protection on your login page. Like captcha or invisible recaptcha etc because the code above should work. – Sky Jun 25 '18 at 06:37
  • You can also provide the login url so I can check. – Sky Jun 25 '18 at 06:37
  • there is a tk_trp token on the page that needs passed thats why i made the $tk_trp = '$stream'; and $stream = GetStringBetween(getURL("url"),"login.authToken = '", "'"); part Hmm im new to Stack is there a way to DM? ill send you the login and everything if there is –  Jun 25 '18 at 06:43
  • I don't think there is any DM feature in here. But in line 7 of your code I saw `$tk_trp = '$token';` which is wrong. It doesn't pass `$token` data to `$tk_trp`. Please fix it and try again. Change it to `$tk_trp = $token;` (remove `'`). – Sky Jun 25 '18 at 06:50
  • Yeah its doing the same thing im sure im just doing something very small thats breaking my code lol –  Jun 25 '18 at 06:55
  • Okay I just emailed. – Sky Jun 25 '18 at 07:00
  • 1
    Thanks so much! you have got it working your the best 5 star! –  Jun 25 '18 at 07:55
0

Could it be that extra space in &tk_trp and = ? curl_setopt($ch, CURLOPT_POSTFIELDS, 'j_username='.$username.'&j_password='.$password.'&tk_trp ='.$tk_trp);

Izek
  • 46
  • 3
  • What about CURLOPT_POSTFIELDS? Should it be $CURLOPT_POSTFIELDS (unless it's been defined) and have an int value? Are you getting any php errors? – Izek Jun 24 '18 at 05:24
  • Try checking if this page helps? https://stackoverflow.com/questions/5224790/curl-post-format-for-curlopt-postfields – Izek Jun 24 '18 at 05:30