Search code examples
phpurl-rewritingelgg

How to add a custom value to current URL


i use a CMS (elgg : http://www.elgg.org ) working with view, equivalent to theme/template. To change the theme i have to put inside URL the term ?view=mytheme, like :

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

If i don't add ?view=mytheme, elgg choose the view by default

So, i need help, for specific users, i want to redirect us to a custom view, and can't see the default view.

I make this in my header :

<?php 

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

I don't know how to take the current url, and simple add ?view=mytheme at the end ?

When a user have the type '2', and see the the url http://www.mydomain.com/index.php it have to be forward to > http://www.mydomain.com/index.php?view=mytheme

Thanks.


Solution

  • This is kinda dirty, but you could:

    // 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");
    }
    

    This is the most basic example I can think of to accomplish what you're asking. This won't quite do the job if you want the URL to support longer query strings so it needs some work.