PHP网址if语句无法正常工作($ _SERVER [' SERVER_NAME']和$ _SERVER [' REQUEST_URI'])

时间:2017-11-08 21:41:10

标签: php session

我是论坛的新手。在使用我的网址均衡$host时if语句中存在问题。我尝试了echo $_SERVER['SERVER_NAME']$_SERVER['REQUEST_URI'],两者都运行正常。同时删除$_SESSION['loggedin']) && $_SESSION['loggedin'] == true &&也没有帮助。我发现问题出在if声明中我无法解决。非常感谢任何提示。提前致谢。这是代码。

<?php
session_start();
include_once("db.php");

$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){

  echo "<li><a href='https://myURL' >Link_text</a></li>";
  echo "<li><a href='https://myURL' >Link_text</a></li>";

} else if($_SESSION['loggedin']) && $_SESSION['loggedin'] == true && $host == 'myURL'){

  echo "<li><a href='https://myURL'>Link_text</a></li>";

} else if($_SESSION['loggedin']) && $_SESSION['loggedin'] == true && $host == 'myURL'){

  echo "<li><a href='https://myURL'>Link_text</a></li>";
  echo "<li><a href='https://myURL' >Link_text</a></li>";
  echo "<li><a href='https://myURL' >Link_text</a></li>";

} else{
  echo "<li><a href='#sec1'>Link_text</a></li>";
  echo "<li><a href='#sec2'>Link_text</a></li>";
  echo "<li><a href='#sec3'>Link_text</a></li>";
  echo "<li><a href='#sec4'>Link_text</a></li>";
  echo "<li><a href='https://myURL'>Link_text</a></li>";
}
?>  

新的固定工作代码。谢谢大家。

<?php
session_start();
include_once("db.php");

$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){

    if($host == 'myURL/'){
        echo "<li><a href='https://myURL' >Link_text</a></li>";
        echo "<li><a href='https://myURL' >Link_text</a></li>";
    }else if($host == 'myURL'){
        echo "<li><a href='https://myURL'>Link_text</a></li>";
    }else if($host == 'myURL'){
        echo "<li><a href='https://myURL'>Link_text</a></li>";
        echo "<li><a href='https://myURL' >Link_text</a></li>";
    }else{
        echo "<li><a href='#sec1'>Link_text</a></li>";
        echo "<li><a href='#sec2'>Link_text</a></li>";
        echo "<li><a href='#sec3'>Link_text</a></li>";
        echo "<li><a href='#sec4'>Link_text</a></li>";
        echo "<li><a href='https://myURL'>Link_text</a></li>";
    }

}else{

    if($host == 'myURL/'){
        echo "<li><a href='#sec1'>Link_text</a></li>";
        echo "<li><a href='#sec2'>Link_text</a></li>";
        echo "<li><a href='#sec3'>Link_text</a></li>";
        echo "<li><a href='#sec4'>Link_text</a></li>";
        echo "<li><a href='https://myURL'>Link_text</a></li>";
    }else{
        echo "<li><a href='https://myURL'>Link_text</a></li>";
    }

}
?>

1 个答案:

答案 0 :(得分:0)

else if测试仅在if测试失败时执行。如果用户已登录,则将执行if块,因此它将永远不会执行任何else if测试。您需要将$host测试嵌套在该块中。

if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
    echo "<li><a href='https://myURL' >Link_text</a></li>";
    echo "<li><a href='https://myURL' >Link_text</a></li>";
    switch ($host) {
    case 'myURL1':
        echo "<li><a href='https://myURL'>Link_text</a></li>";
        break;
    case 'myURL2':
        echo "<li><a href='https://myURL'>Link_text</a></li>";
        echo "<li><a href='https://myURL' >Link_text</a></li>";
        echo "<li><a href='https://myURL' >Link_text</a></li>";
        break;
    }
} else {
    echo "<li><a href='#sec1'>Link_text</a></li>";
    echo "<li><a href='#sec2'>Link_text</a></li>";
    echo "<li><a href='#sec3'>Link_text</a></li>";
    echo "<li><a href='#sec4'>Link_text</a></li>";
    echo "<li><a href='https://myURL'>Link_text</a></li>";
}