简体   繁体   中英

If current page URL equals X OR X?

I found this code on the internetz, it checks the current page url;

function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}

So now I can do something like this;

elseif (curPageURL() == "http://www.example.com/pageexample") {
<meta tags here>
}

Great. But I would also like to use this for pagination pages. Those URLs look like this:

http://www.example.com/pageexample?start=30&groep=0
http://www.example.com/pageexample?start=60&groep=0
http://www.example.com/pageexample?start=90&groep=0
[....]
http://www.example.com/pageexample?start=270&groep=0

I could use a if statement for every of those links.. but I would much rather like to use one. Is it possible to add a wildcard or something? Like this I guess (notice the * )

elseif (curPageURL() == "http://www.example.com/pageexample" OR curPageURL() == "http://www.example.com/pageexample?start=*&groep=0") {

edit : I would like to do this for all those URLs because I want to give them the same meta description , <title> and <link rel="canonical" . I could do this manually by doing an if-statement for every page (10+ atm) but I figured there was a better way.

Why not just use the parse_url() function? From the manual page:

<?php

$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));

?>

// The above would print
Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)

For your particular case, you could then just check against the host and path variables.

Sounds a lot like regex problem:

if (preg_match("#^http://www.example.com/pageexample(\?start=[^&]*&groep=0)?#", curPageURL())) {
    // it matches
}

The expression [^&]* acts like your * . ; to match non-empty items, use ; to match non-empty items, use [^&]+`. It matches these:

http://www.example.com/pageexample
http://www.example.com/pageexample?start=30&groep=0

Update

It's not entirely clear why you need to compare against the full canonical URL, unless you have multiple domains point to the same code base.

You should use a string comparison function

if (strstr(curPageURL(), 'http://www.example.com/')) !== FALSE) {
  // curPageURL() contains http://www.example.com/
}

or

if (preg_match('/^http\:\/\/www\.example\.com\//', curPageURL()) { 
  // curPageURL() starts with http://www.example.com/
}

There's lots of ways to do it

You could wrap this

elseif (curPageURL() == "http://www.example.com/pageexample" OR curPageURL() == "http://www.example.com/pageexample?start=*&groep=0") {

in a while loop that adds 30 to a variable where you have your wild card on each iteration.

did you try regex?

if (preg_match('/http:\/\/www\.example\.com\/pageexample\?start=[0-9]+&groep\=0/i', "http://www.example.com/pageexample?start=34&groep=0")) {
   echo "A match was found.";
else {
   echo "A match was not found.";
}

If you don't use the query_string element from the $_SERVER array all your paginated URLs will return the same URL: http://www.example.com/pageexample , you can check with

echo $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] ;

vs

echo $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].'?'.$_SERVER["QUERY_STRING"] ;

You'll see that in the first case you don't receive GET parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM