0

我有以下三个可能的网址..

  • www.mydomain.com/445/loggedin/?status=empty
  • www.mydomain.com/445/loggedin/?status=complete
  • www.mydomain.com/445/loggedin/

www.mydomain.com/445 部分是动态生成的,并且每次都不同,所以我无法进行完全匹配,我该如何检测以下内容......

  • 如果 $url 包含登录但不包含 /?status=empty 或 /?status=complete

我尝试的一切都失败了,因为无论如何它总是会检测到登录的部分..

if(strpos($referrer, '?status=empty')) {
echo 'The status is empty';
}
elseif(strpos($referrer, '?status=complete')) {
echo 'The status is complete';
}
elseif(strpos($referrer, '/loggedin/')) {
echo 'The status is loggedin';
}
4

3 回答 3

1

将 URL 分割成段

$path = explode('/',$referrer);
$path = array_slice($path,1);

然后只需在该数组上使用您的逻辑,您包含的第一个 URL 将返回:

Array ( [0] => 445 [1] => loggedin [2] => ?status=empty )
于 2013-04-12T23:36:06.220 回答
1

你可以这样做:

$referrer = 'www.mydomain.com/445/loggedin/?status=empty';

// turn the referrer into an array, delimited by the /
$url = explode('/', $referrer);

// the statuses we check against as an array
$statuses = array('?status=complete', '?status=empty');

// If "loggedin" is found in the url, and count the array_intersect matches, if the matches = 0, none of the statuses you specified where found
if( in_array('loggedin', $url) && count(array_intersect($url, $statuses)) == 0 )
{
    echo 'The user is logged in';
}
// if the complete status exists in the url
else if( in_array('?status=complete', $url) )
{
    echo 'The status is complete';
}
// if the empty status exists in the url
else if( in_array('?status=empty', $url) )
{
    echo 'The status is empty';
}

推荐看看array_intersect,挺好用的。

希望它有所帮助,不确定这是否是最好的方法,但可能会激发您的想象力。

于 2013-04-12T23:45:14.063 回答
0

Strpos 可能不是您想要使用的。你可以用 stristr 做到这一点:

    if($test_str = stristr($referrer, '/loggedin/')) 
    {
        if(stristr($test_str, '?status=empty')) 
        {
            echo 'empty';
        }
        elseif (stristr($test_str, '?status=complete')) 
        {
            echo 'complete';
        } else {
            echo 'logged in';
        }
    }

但是使用正则表达式可能更容易/更好:

if(preg_match('/\/loggedin\/(\?status=(.+))?$/', $referrer, $match)) 
{
    if(count($match)==2) echo "The status is ".$match[2];
    else echo "The status is logged in";
}
于 2013-04-13T00:09:21.610 回答