0

我使用 CMS(elgg:http ://www.elgg.org )处理视图,相当于主题/模板。要更改主题,我必须在 URL 中输入术语 ?view=mytheme,例如:

http://www.mydomain.com/index.php?view=mytheme

如果我不添加 ?view=mytheme,elgg 默认选择视图

所以,我需要帮助,对于特定用户,我想将我们重定向到自定义视图,并且看不到默认视图。

我在我的标题中做了这个:

<?php 

  if ((string) $_SESSION['user']->type === '2') {
    // ???
  } else {
    echo 'do nothing';
  }

我不知道如何获取当前的 url,并在末尾简单地添加 ?view=mytheme ?

当用户的类型为“2”时,看到 URL http://www.mydomain.com/index.php 它必须转发到 > http://www.mydomain.com/index.php?view =mytheme

谢谢。

4

1 回答 1

1

这有点脏,但你可以:

// Stole this from here: http://webcheatsheet.com/php/get_current_page_url.php
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;
}

$useCustomView = ((string)$_SESSION['user']->type) === '2';
$usingCustomView = isset($_GET["view"]);
// Redirect to custom view if user type is 2 and view is not already set
if ($useCustomView && !$usingCustomView) {
        $newurl = curPageURL() . '?view=mytheme';
        header("Location: $newurl");
}

这是我能想到的完成你所要求的最基本的例子。如果您希望 URL 支持更长的查询字符串,这将无法完成工作,因此需要一些工作。

于 2012-01-31T12:32:46.483 回答