如何知道php中当前的url是什么

时间:2015-07-08 03:37:13

标签: php

那么,检查某个href url的条件逻辑是什么?例如,有三个特定页面。

然后我写什么来检查当前页面是否等于特定网址:

 <?php if () { ?> <!--if the current page is equal to `page_1` -->   
   <span>Show Page 1</span>
 <?php } elseif () { ?> <!--if the current page is equal to `page_2` -->
   <span>Show Page 2</span>
<?php } elseif () { ?> <!--if the current page is equal to `page_3` -->
   <span>Show Page 3</span>
<?php } else { ?> <!--if the current page is not equal to any -->
    <span>Show Random</span>
<?php } ?>

3 个答案:

答案 0 :(得分:1)

您可以使用以下功能:

if(CurrentPageURL()=='http://example.com/page_1'){
 /*Your code*/
}
function CurrentPageURL()
{
    $pageURL = 'http';
    if (isset($_SERVER["HTTPS"]) && $_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;
}

答案 1 :(得分:1)

你可以用这个

$getlink=explode("/",$_SERVER['REQUEST_URI']);
//this will return an array of the url.
$1st = $getlink[1];
//get the page_* then use this var for your condition
if($1st == 'page_1'){
  echo '<span>'.$showpage1.'</span>';
}// and go on...

希望这有助于:)

答案 2 :(得分:1)

explode url with ".com/" then check the condition.check below code.
`enter code here`
 1. <?php  $url = "http://example.com/page_1";
       $url_ex = explode(".com/",$url);
          print_r($url_ex);
 2. Result = Array ( [0] => http://example [1] => page_1 )
 <?php if ($url_ex[1] == 'page_1') { ?> <!--if the current page is equal to `page_1` -->   
   <span>Show Page 1</span>
 <?php } elseif ($url_ex[1] == 'page_2') { ?> <!--if the current page is equal to `page_2` -->
   <span>Show Page 2</span>
<?php } elseif ($url_ex[1] == 'page_3') { ?> <!--if the current page is equal to `page_3` -->
   <span>Show Page 3</span>
<?php } else { ?> <!--if the current page is not equal to any -->
    <span>Show Random</span>
<?php } ?>