We help IT Professionals succeed at work.

how to get a regex from a cookie

154 Views
Last Modified: 2015-01-01
hello

how to get the regex from the cookies ?

exemple cookie

_ga=GA1.2.1143421136.1419395036; 16acd9b63ecbf50de0b8c010c2b7289f=779l2132g7802l2fb7vkpqlh53; _gat=1; YwRuLWF1dGhfNTE4NF8xMjE3NDU=549f9d4038f6c274024048

Open in new window


ty
Comment
Watch Question

aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
hello ty for your help :)

i try and résult work juste one probleme on this part of code i have a (#mi) It is what. ?

function getCookie($data)
{                                   
    preg_match_all('#Set-Cookie: ([^=]*)=([^;]*);?#mi', $data, $match);

    return (empty($match[1]) ? null : implode(';', $match[1]) . '; ');
}

Open in new window

oder cookie
1143421136.1419395036; YWRuLWF1dGhfNTE4NF8xMjE3NDU=54a05ccfc6336342291705; 18acd9b63ecbf50de0b8c010c2b7289f=jn8mg8uroajlsjbobmqg4mmth7; _gat=1; YWRuLWF1dGhfNTUxN17xMjE3NDU=54a05fcd28c4e036210541

Open in new window


i get a cookie  just one part ( 18acd9b63ecbf50de0b7c010c2b7289f; )
But my script manages always not to work nevertheless you have an idea ?
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Please add this one line to the top of your PHP script and post the output here, thanks.
var_dump($_COOKIE);

Open in new window

Author

Commented:
I have nothing to post(show) :) no error
Most Valuable Expert 2011
Author of the Year 2014

Commented:
If you have a PHP script that is started by a request that includes a cookie, there will be a value in $_COOKIE.  If you have no value in $_COOKIE, there will still be some output from the PHP function var_dump().  It is never silent.  

You might also want to look into Chrome Developer Tools if you're unsure about the contents of the request variables.  The are really useful when you're trying to write web applications!
https://developer.chrome.com/devtools
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
You may need to add a die(); command after the var_dump(); command... I think you can even do
die(var_dump($foo));

Open in new window

Author

Commented:
@Terry Woods  

result
array(0) {
}

Open in new window


-----

@Ray Paseur
Thank you I go and look
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Would you please confirm you actually ran:
die(var_dump($_COOKIE));

Open in new window

rather than my example code using $foo ?

Author

Commented:
yes i use die(var_dump($_COOKIE));
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Recommend you use var_dump($_COOKIE) rather than die(var_dump($_COOKIE)).  The former would be expected to print out the value of $_COOKIE.  The latter would be expected to print out the return value from the var_dump() function.  The var_dump() function is documented here:
http://php.net/manual/en/function.var-dump.php

As you can see, it returns "void."  I'm not sure what would appear with that return value!  And it might be caught up in output buffering or something like that.  In Laravel, there is a "dd()" function that runs var_dump() and die() in succession.

If you use var_dump($_COOKIE) and you get output like this...

array(0) { }

... there was no matching cookie returned by the browser.  The browser considered the URL, as well as the protocol (HTTP vs HTTPS) and the sub-domain in deciding whether or not to return the cookie.

Author

Commented:
var_dump($_COOKIE)
not work for me

i try this

file_put_contents('match'.time().'.txt', print_r($match, true));

Open in new window


résult

Array
(
    [0] => Array
        (
            [0] => Set-Cookie: 18acd9b62ecbf50de0b8c010c2b7289f=ftsdvqalsivpv5hb4kf3ucj1r5;
        )

    [1] => Array
        (
            [0] => 18acd9b63ecbf50de0b7c010c2b7289f=ftsdvqalsivpv6hb5kf3ucj1r5
        )

    [2] => Array
        (
            [0] => ;
        )

)

Open in new window

Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Thanks Ray; I was going by something I read, rather than something I've used, so I'm sure you're correct.

It's worth mentioning the alternative should generally still have the die immediately after it though, for ease of use, otherwise the output may get lost if the output is complex:
var_dump($_COOKIE);
die();

Open in new window

Author

Commented:
with
var_dump($_COOKIE);
die();

Open in new window


résult
array(0) {
}

Open in new window

Author

Commented:
my full code

http://pastebin.com/kFYMY7vL

my script stop on this part
 function getSecurityTokens()
    {
        global $cookie, $videoLink, $videoId;
        $old = $cookie;
    file_put_contents('y'.time().'.txt', print_r($get, true));
        get(SITE_URL . $videoLink);
    file_put_contents('p'.time().'.txt', print_r($cookie, true));
 
        if ($cookie == $old)
        {
            die('Impossible to get  the tokens of the video');

Open in new window


the script stop with error

Impossible to get back the tokens of the video

Open in new window


I think that it is a change in cookies but not sure have you an idea
Most Valuable Expert 2011
Author of the Year 2014

Commented:
I'm not sure how to help at this point, so I've just annotated the code snippet with comments.  I think it will help you to follow the logic.

function getSecurityTokens()
{
    // MAKE THESE THREE FUNCTION VARIABLES AVAILABLE IN THE GLOBAL SCOPE, ALSO OUTSIDE OF THIS FUNCTION
    global $cookie, $videoLink, $videoId;
    
    // COPY THE VALUE IN $cookie INTO $old (THE TWO ARE NOW EQUAL)
    $old = $cookie;
    
    // WRITE SOME INFORMATION INTO A FILE -- BUT WHAT?  THE VALUE OF $get IS UNDEFINED IN THIS FUNCTION
    file_put_contents('y'.time().'.txt', print_r($get, true));
    
    // DON'T KNOW WHAT THIS DOES
    get(SITE_URL . $videoLink);
    
    // WRITE THE VALUE OF $cookie (SAME AS THE VALUE OF $old) INTO A FILE
    file_put_contents('p'.time().'.txt', print_r($cookie, true));
 
    // THIS WILL ALWAYS BE TRUE BECAUSE OF THE ASSIGNMENT ON LINE 7
    if ($cookie == $old)
    {
        die('Impossible to get  the tokens of the video');
        
    // THE SCRIPT FAILS BECAUSE OF A PARSE ERROR HERE - THE IF AND FUNCTION STATEMENTS ARE UNTERMINATED

Open in new window

Author

Commented:
Which information we would need to find the source of the problem

i use this line for test variable

file_put_contents('y'.time().'.txt', print_r($get, true))

Open in new window

Most Valuable Expert 2011
Author of the Year 2014

Commented:
I don't know -- I do not understand the application requirements so I'll sign off now.  Good luck and I hope one of our other members can help!

Happy New Year, ~Ray

Author

Commented:
@Ray Paseur
I do not understand the application requirements
I am going to try to explain at best all this to give you the real context of the script my to ask to a friend to be translated correctly
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Back when you posted the code:
function getCookie($data)
{                                   
    preg_match_all('#Set-Cookie: ([^=]*)=([^;]*);?#mi', $data, $match);

    return (empty($match[1]) ? null : implode(';', $match[1]) . '; ');
}

Open in new window

you requested to know the meaning of the #mi

The two # characters are the delimiters for the regex pattern, so the regex pattern is:
Set-Cookie: ([^=]*)=([^;]*);?

Open in new window

And the "mi" characters are pattern modifiers. "m" means multiline so that the wildcard . can match a newline (\n) character (eg so that .* will match multiple lines rather than just up to the next \n character), and "i" means ignore case (eg so that [a-z] would also match A-Z

When you use the getCookie function, do you have the data you need in the $data parameter? If so, is the regular expression working the way you want it to? I can help with updating the pattern if it's not working.

Author

Commented:
what information is needed for the pattern ? how to obtain this information on the website ?

the full code is here
http://pastebin.com/kFYMY7vL

Author

Commented:
i got this  cookie.js

http://pastebin.com/S7Sge2Fq

the full code is here  (sorry  The previous code expired)
http://pastebin.com/LFD19Rp6

te cookies
website.fr	FALSE	/	FALSE	18acd9b63ecbf50de0b8c010c2b7289f	j2ata6mfajhoiggfhrjc65je07
website.fr	FALSE	/	FALSE	1420015619	YWRuLWF1dGhfNDU4Ml7w	54a38dd201ca6961978262
website.fr	FALSE	/	FALSE	1420015644	YWRuLWF1dGhfNDU4Ml7xMjE3NDU	54a38dec74248260696300
.website.fr	TRUE	/	FALSE	1483076842	_ga	GA1.2.545715106.1420004811
.website.fr	TRUE	/	FALSE	1420005410	_gat	1

Open in new window

Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Can you please clarify what value you're wanting to get out of the cookie?

Author

Commented:
Re hello I found how to use this command line

var_dump($cookie, $old)

Open in new window



résult

string(61) "17acd9b63ecbf50de0b8c010c2b7289f=eh8bnl4pcl6go0f67ituitr6m2; " string(61) "17acd9b63ecbf50de0b8c010c2b7289f=eh8bnl4pcl6go0f67ituitr6m2; "

Open in new window


and $data  is a page html

sorry is not your question just I do not know what to look what to give you I am one begin who tries to understand and to learn I know not which information to give you I'm an inexperienced with php
Web Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Too hard to get the real problem and thus I am going to wait to have more information for re to post

I give you your points for the effort
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.