获取PHPSESSID值

时间:2022-09-13 12:30:00

I have a cookie with the following data:

我有一个带有以下数据的cookie:

.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
.somesite.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

我只想得到PHPSESSID的值在这里是:7ah4ppb3bcbubged8pejb05s35

How can I get that value using preg_match?

如何使用preg_match获得该值?

Update:

更新:

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

这是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);

然后我var_dump($ search_result);

array
  empty

Not sure what I am doing wrong...

不知道我做错了什么……

2 个解决方案

#1


2  

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

您可能只是不正确地使用preg_match。要用搜索结果填充变量,应该如下所示:

$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:

然后,为了引用特定的会话ID,您将引用$search_result[1],它将匹配括号中的内容(会话ID)。用这个例子,试试这个:

$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/';

#2


1  

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

如果分隔符是制表符,或空格,或两者都是多行字符串:

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

#1


2  

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

您可能只是不正确地使用preg_match。要用搜索结果填充变量,应该如下所示:

$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:

然后,为了引用特定的会话ID,您将引用$search_result[1],它将匹配括号中的内容(会话ID)。用这个例子,试试这个:

$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/';

#2


1  

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

如果分隔符是制表符,或空格,或两者都是多行字符串:

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