Leave only letters and numbers of a cookie

1

I have a cookie that returns me

  

ct0 = 945d9ead4b3ce88d5aba8f09a4d78aee;

Using explode I was able to leave it like this:

  

'945d9ead4b3ce88d5aba8f09a4d78aee; '

Note that it has a ; , period, and space at the end, I want to leave only non-special characters.

Here's my code:

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

I think I've solved this code:

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

If you have another way to do this, 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;
        }
    }
}
    
asked by anonymous 06.10.2017 / 18:50

1 answer

1

Do this:

$cookie = 'ct0=945d9ead4b3ce88d5aba8f09a4d78aee;';

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

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

Or more secure if you use preg_match ( preg_quote is to escape the characters that might interfere with regex ), regex match might look like this:

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

The ([\s\S]+?) will catch anything until you find the semicolon and (;|$) will check if it is the end of the string or if it has other cookies, it should look 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: link

    
06.10.2017 / 19:00