Recover Cookies after logging in

Asked

Viewed 39 times

0

I have the following code:

<?php
if (isset($_POST['ttrSignin'])) {
  $ttrUsername = trim(filter_input(INPUT_POST, 'ttrUsername'));
  $ttrPassword = trim(filter_input(INPUT_POST, 'ttrPassword'));

  if (empty($ttrUsername)) {
    $error[] = 'Insira seu nome de usuário do Twitter.';
  } elseif (empty($ttrPassword)) {
    $error[] = 'Insira sua senha do Twitter.';
  } elseif (!preg_match('/^[a-zA-Z0-9]+/', $ttrUsername)) {
    $error[] = 'Caracteres especiais detectados, se tiver usando <strong>@</strong>, remova-o.';
  } else {
    $ch = curl_init();

    $sTarget = "https://twitter.com";

    curl_setopt($ch, CURLOPT_URL, $sTarget);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_COOKIEFILE, ROOT . SEPARATOR . 'app' . SEPARATOR . 'cookies' . SEPARATOR. md5($ttrUsername) . '.txt');
    curl_setopt($ch, CURLOPT_COOKIEJAR, ROOT . SEPARATOR . 'app' . SEPARATOR . 'cookies' . SEPARATOR. md5($ttrUsername) . '.txt');
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_REFERER, $sTarget);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);

    $html = curl_exec($ch);

    if(curl_errno($ch)) {
       echo 'error:' . curl_error($c);
    }

    preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $match);

    $authenticity_token = $match[1];

    if ($authenticity_token == "") {       
      preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $matchprima);   
      $authenticity_token = $matchprima[1];
    }


    $username = $ttrUsername;
    $password = $ttrPassword;

    $sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token";

    $sTarget = "https://twitter.com/sessions";  

    curl_setopt($ch, CURLOPT_URL, $sTarget);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    # display server response
    $htmldos = curl_exec($ch);

    preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $htmldos, $matches);

    $cookies = array();

    foreach($matches[1] as $item) {
        parse_str($item, $cookie);
        $cookies = array_merge($cookies, $cookie);
    }

    //var_dump($cookies);

    if(curl_errno($ch)) {
       echo 'error:' . curl_error($ch);
    }

    if (empty($cookies['auth_token'])) {
      $error[] = 'Não foi possível conectar-se ao Twitter.';
    } else {
      $_SESSION['twitter_oauth_token'] = $ttrUsername;
      header('Refresh:2;url=' . URL_BASE . '/follow/?welcome=true');
      $success[] = 'Conectado com sucesso, estamos te redirecionando!';
    }
  }
}
?>

It generates the cookie, saved in a folder, but after logging in, there is no way to recover these cookies, someone has idea how to make cookies continue on the page of logged in users?

  • I’ve done it before It didn’t work...

  • I’m trying to .... Let’s see how it goes...

  • Solved with this answer in another very well explained question: https://answall.com/questions/30721/como-acessar-o-cookie-em-outra-p%C3%A1gina? Rq=1

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.