Leave only letters and numbers of a cookie

Asked

Viewed 46 times

0

I have a cookie that returns to me

ct0=945d9ead4b3ce88d5aba8f09a4d78aee;

Using the explode I managed to leave it like this:

'945d9ead4b3ce88d5aba8f09a4d78aee; '

Notice you have one ;, period and space at the end, I want to leave only non special characters.

Here is my code:

$ct0 = explode('ct0=', $cookie[4]);

I think I solved it with this code:

$ct0 = explode('ct0=', $cookie[4]);
$ct0 = explode('; ', $ct0[1]);

If there’s any other way I’m grateful.

EDITED

<?php

class Login {

    private $_url;
    private $_cookieFile;

    public $_username = '';
    public $_password = '';

    public function __construct($url) {
        $this->_url = $url;
    }

    public function setCookieFile($cookieFile) {
        $this->_cookieFile = $cookieFile;
    }

    public function setUsername($username) {
        $this->_username = $username;
    }

    public function setPassword($password) {
        $this->_password = $password;
    }

    public function login($username, $password) {
        $request = curl_init();
        curl_setopt_array($request, [
                CURLOPT_URL                         => $this->_url,
                CURLOPT_CUSTOMREQUEST       => 'GET',
                CURLOPT_RETURNTRANSFER  => true,
                CURLOPT_SSL_VERIFYPEER  => false,
                CURLOPT_SSL_VERIFYHOST  => false,
                CURLOPT_HEADER                  => true,
                CURLOPT_COOKIEJAR               => $this->_cookieFile,
                CURLOPT_USERAGENT               => $_SERVER['HTTP_USER_AGENT'],
                CURLOPT_HEADERFUNCTION  => function($curl, $header) use (&$cookie) {
                    if (stripos($header, 'Set-Cookie:') === 0) {
                        if (preg_match('/^Set-Cookie: \s*([^;]*)/i', $header, $matches)) {
                            $cookie[] = $matches[1] . '; ';
                        }
                    }
                    return strlen($header);
                },
                CURLOPT_COOKIE                  => $cookie,
            ]
        );
        $response = curl_exec($request);

        preg_match('/value="(.*?)" name="authenticity_token"/', $response, $matches);

        $authenticity_token = $matches[1];

        $post_fields = http_build_query([
            'session' => [
                'username_or_email' => $username,
                'password'                  => $password
            ],
                'return_to_ssl'                 => true,
                'scribe_log'                        => '',
                'redirect_after_login'  => '/',
                'authenticity_token'        => $authenticity_token
            ]
        );

        curl_setopt_array($request, [
                CURLOPT_URL                         => $this->_url . '/sessions',
                CURLOPT_CUSTOMREQUEST       => 'POST',
                CURLOPT_POSTFIELDS          => $post_fields,
                CURLOPT_RETURNTRANSFER  => true,
                CURLOPT_SSL_VERIFYPEER  => false,
                CURLOPT_SSL_VERIFYHOST  => false,
                CURLOPT_HEADER                  => false,
                CURLOPT_FOLLOWLOCATION  => true,
                CURLOPT_COOKIE                  => $cookie[0] . $cookie[1] . $cookie[2] . $cookie[3] . $cookie[4],
                CURLOPT_USERAGENT               => $_SERVER['HTTP_USER_AGENT'],
                CURLOPT_HTTPHEADER          => [
                    'accept-language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4',
                    'content-type: application/x-www-form-urlencoded',
                    'origin: https://twitter.com',
                    'referer: https://twitter.com/login',
                ],
            ]
        );

        $response = curl_exec($request);
        curl_close($request);

        $ct0 = explode('ct0=', $cookie[4]);
        $ct0 = explode('; ', $ct0[1]);

        if ($response === '') {
            Session::set('ct0', $ct0);
            return true;
        } else {
            return false;
        }
    }
}
  • I returned this cookie from twitter.

  • I edited the question, with the solution,

  • Ready I edited posted the code, it’s working.

1 answer

0


Do so:

$cookie = 'ct0=945d9ead4b3ce88d5aba8f09a4d78aee;';

$ct0 = explode('ct0=', $cookie);

$ct0 = trim($ct0[1]); //Limpa qualquer espaçamento
$ct0 = trim($ct0, ';'); //Remove o `;`

Or more guaranteed to use preg_match (the preg_quote is to escape the characters that can interfere with regex), the match of the regex can be so:

chave=([\s\S]+?)(;|$)

The ([\s\S]+?) will pick up anything until you find the dot and comma and the (;|$) will make check if it is the end of the string or if it is has other cookies, should stay like this:

function getCookie($key, $cookie) {
    if (preg_match('#' . preg_quote($key) . '=([\s\S]+?)(;|$)#i', $cookie, $output)) {
        return $output[1];
    }
}

$cookie = 'ct0=945d9ead4b3ce88d5aba8f09a4d78aee;foobar=945d9ead4b3ce88d5ab&a8f09a4d78aee';

var_dump(getCookie('ct0', $cookie));
var_dump(getCookie('foobar', $cookie));

Example in IDEONE: https://ideone.com/ykoBfH

  • 1

    Ta but my solution is not simpler? $ct0 = explode('ct0=', $cookie[4]);&#xA; $ct0 = explode('; ', $ct0[1]);

  • 1

    Ta good I’ll do it this way from your grateful post, alias already copied and did. rsrs

  • 1

    I was going to give +1 but I don’t have rap, a basic question: the Twitter CT0 does not contain special characters only letters and numbers, it was the cookies that were returning me 'ct0=numeros&letras; '

  • 3

    Now it’s beautiful, but it’s not all cookies that I will need, but I have another site that needs it. You already said the other side of me, thanks.

Browser other questions tagged

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