开发者

Regex Cookie To Get PHPSESSID value

开发者 https://www.devze.com 2023-03-27 19:35 出处:网络
I have a cookie with the following data: .somesite.comTRUE/FALSE1315605660langen .somesite.comTRUE/FALSE1314223260rolepremium

I have a cookie with the following data:

.somesite.com   TRUE    /   FALSE   1315605660  lang    en
.somesite.com   TRUE    /   FALSE   1314223260  role    premium
.somesite.com   TRUE    /   FALSE   1314223260  PHPSESSID   7ah4ppb3bcbubged8pejb05s35
.somesite.com   TRUE    /   FALSE   1314223260  rememberMe  1
.somesite.com   TRUE    /   FALSE   1314223260  email   me@email.me
.s开发者_开发问答omesite.com   TRUE    /   FALSE   1281477659  nickname    deleted
.somesite.com   TRUE    /   FALSE   1281477659  isAffiliate deleted
.somesite.com   TRUE    /   FALSE   0   messagingUnread 0
.somesite.com   TRUE    /   FALSE   0   messagingUnreadLastcheck    1313013660

I only want to get the value of the PHPSESSID which in this case is: 7ah4ppb3bcbubged8pejb05s35

How can I get that value using preg_match?

Update:

So here is the result of var_dump($cookie);

string '# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

.somesite.com   TRUE    /   FALSE   1315612275  lang    en
.somesite.com   TRUE    /   FALSE   1314229875  role    premium
.somesite.com   TRUE    /   FALSE   1314229875  PHPSESSID   j8v4ifqfpvbfr1cjp49e8del55
.somesite.com   TRUE    /   FALSE   1314229875  rememberMe  1
.somesite.com   TRUE    /   FALSE   1314229875  email   me%40email.me
.somesite.com   TRUE    /   FALSE   1281484274  nickname    deleted
.somesite.com   TRUE    /   FALSE   1281484274  isAffiliate deleted
.somesite.com   TRUE    /   FALSE   0   messagingUnread 0
.somesite.com   TRUE    /   FALSE   0   messagingUnreadLastcheck    1313020275
' (length=647)

I tried this:

$search_expression = '/PHPSESSID\s+(.*)$/';
preg_match($search_expression, $cookie, $search_result);

And then I var_dump($search_result);

array
  empty

Not sure what I am doing wrong...


You may simply be using preg_match incorrectly. To populate a variable with the results of a search, it would look something like this:

$search_expression = '/PHPSESSID\s+(.*)$/';
preg_match($search_expression, $cookie, $search_result);

Then to reference the specific session ID you would reference $search_result[1], which will match just what is in parentheses (the session ID). So to use this example, try this:

$search_expression = '/PHPSESSID\s+(.*)$/';
if (preg_match($search_expression, $cookie, $search_result)) {
  echo "PHP Session ID found.";
  $phpsessionid = $search_result[1];
} else {
  echo "PHP Session ID not found.";
}

This will help narrow down the problem. If nothing is still returning, then the problem is with the search expression. You might alternately try this if that is the case:

$search_expression = '/PHPSESSID\s+(.*)\s+\.some/';

And if that still doesn't work, try this search expression:

$search_expression = '/PHPSESSID\s+([a-z0-9]+)\s+\.some/';


If the separator is a tab, or a space or both in a multi-line string:

~[ \t]PHPSESSID[ \t]+(.*)$~m
0

精彩评论

暂无评论...
验证码 换一张
取 消