Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to get a regex from a cookie

Posted on 2014-12-28
26
88 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
ID: 40520536
here is a parsing pattern:
([^=]*)=([^;]*);?

Open in new window

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 40520573
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
ID: 40520681
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40520702
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
ID: 40520732
I have nothing to post(show) :) no error
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40520967
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
ID: 40521218
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
ID: 40521338
@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
ID: 40521426
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
ID: 40521678
yes i use die(var_dump($_COOKIE));
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40522815
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
ID: 40523203
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
ID: 40523241
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
 
LVL 2

Author Comment

by:hexo dark
ID: 40523617
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
ID: 40524225
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 109

Expert Comment

by:Ray Paseur
ID: 40524697
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
ID: 40524737
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 109

Expert Comment

by:Ray Paseur
ID: 40524751
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
ID: 40524758
@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
ID: 40524823
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
ID: 40524897
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
ID: 40526084
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
ID: 40526119
Can you please clarify what value you're wanting to get out of the cookie?
0
 
LVL 2

Author Comment

by:hexo dark
ID: 40526299
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
ID: 40526394
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
ID: 40526697
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

840 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