1

I'm trying to create custom php function for redirect. Currently I have this. (I've created index.php file with this function for testing)

function redirect($pageName, ...$parameters)
{
    $host  = filter_input(INPUT_SERVER, 'HTTP_HOST');
    $uri   = rtrim(dirname(filter_input(INPUT_SERVER, 'PHP_SELF')), '/\\');
    foreach ($parameters as $parameter)
        $pageName .= "/".$parameter;
    header("Location: http://{$host}{$uri}/{$pageName}");
}

Function works as it should but I have little problem.

I need to make this function work dynamically in root folder of web/subfolder of web/subdomain

Web is meant as that index.file right now

  • If I have web in root folder of website, function works without any problems. (Function redirect("page") redirects me for example to domain.com/page
  • If I have web in subfolder of website, function works again good (Function redirect("page") redirects me for example to domain.com/folder/page)
  • If I have web in subdomain(what is practically subfolder on website) there is problem, for example, I'm on subdomain.domain.com . I will use in index.php redirect("page"), the function redirects me to subdomain.domain.com/folderwhereiscontentofsubdomainlocated/page.

I need to make this function working on all 3 options, so If user will place website anywhere, it will work without problems.

I've spent few hours of looking for solution, but I didn't find anything.

Sorry for my little bit bad English, I hope you will understand :) Have a nice day

Jopast
  • 11
  • 2
  • Maybe this could help http://stackoverflow.com/questions/2679618/get-domain-name-not-subdomain-in-php – Vaibhav Bhanushali Aug 07 '16 at 08:49
  • Note: As long as you don't validate `HTTP_HOST` and check if it matches your host (domain), it can be changed by the user in anyway he wants. `HTTP_HOST` comes from the HTTP request header `Host`. On my server I always make sure that `HTTP_HOST` matches one domain (and redirect the user if it doesn't match), so I can use it without having to fear that one changed it, as the webserver makes sure it is the correct one. – Charlotte Dunois Aug 07 '16 at 09:19
  • Thanks for note :) – Jopast Aug 07 '16 at 09:39

0 Answers0