Solved

how to get a regex from a cookie

Posted on 2014-12-28
26
80 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
0
Comment
Question by:hexo dark
  • 13
  • 6
  • 6
  • +1
26 Comments
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 167 total points
Comment Utility
here is a parsing pattern:
([^=]*)=([^;]*);?

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
Comment Utility
In PHP, you can find the cookie information in $_COOKIE.  This may be easier than trying to parse the raw data.  These links are on point:
http://php.net/manual/en/reserved.variables.cookies.php
http://php.net/manual/en/function.setcookie.php
http://php.net/manual/en/language.variables.external.php
0
 
LVL 2

Author Comment

by:hexo dark
Comment Utility
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 ?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

0
 
LVL 2

Author Comment

by:hexo dark
Comment Utility
I have nothing to post(show) :) no error
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
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

0
 
LVL 2

Author Comment

by:hexo dark
Comment Utility
@Terry Woods  

result
array(0) {
}

Open in new window


-----

@Ray Paseur
Thank you I go and look
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
Would you please confirm you actually ran:
die(var_dump($_COOKIE));

Open in new window

rather than my example code using $foo ?
0
 
LVL 2

Author Comment

by:hexo dark
Comment Utility
yes i use die(var_dump($_COOKIE));
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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.
0
 
LVL 2

Author Comment

by:hexo dark
Comment Utility
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

0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
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

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Author Comment

by:hexo dark
Comment Utility
with
var_dump($_COOKIE);
die();

Open in new window


résult
array(0) {
}

Open in new window

0
 
LVL 2

Author Comment

by:hexo dark
Comment Utility
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

0
 
LVL 2

Author Comment

by:hexo dark
Comment Utility
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

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
0
 
LVL 2

Author Comment

by:hexo dark
Comment Utility
@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
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
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.
0
 
LVL 2

Author Comment

by:hexo dark
Comment Utility
what information is needed for the pattern ? how to obtain this information on the website ?

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

Author Comment

by:hexo dark
Comment Utility
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

0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
Can you please clarify what value you're wanting to get out of the cookie?
0
 
LVL 2

Author Comment

by:hexo dark
Comment Utility
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
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 167 total points
Comment Utility
I don't have a clear understanding of what you're asking for help with. Could you please provide more explanation?
0
 
LVL 2

Author Closing Comment

by:hexo dark
Comment Utility
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
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now