0

I use Xampp. Here is my directory:

\xampp
    \htdocs
        \myweb
            index.php 
            \classes
                 autoloader.php
                 classname.php

And here is the URL:

http://localhost/myweb/classname/methodname/idNum?arg=val

And I want to get this part: (everything until index.php layer)

http://localhost/myweb

How can I do that?

Note1: $_SERVER['HTTP_HOST'] doesn't return expected thing. It just returns localhost.

Note2: I can get the whole of URL and then parse it by regex to grab expected result .. But I don't want to do that because sometimes myweb folder goes away.

stack
  • 10,280
  • 19
  • 65
  • 117
  • 1
    Possible duplicate of [How to get URL of current page in PHP](http://stackoverflow.com/questions/1283327/how-to-get-url-of-current-page-in-php) – Winter Aug 19 '16 at 22:15
  • @Winter Did you even read the question? – stack Aug 19 '16 at 22:16
  • 1
    It's all about getting the url and removing a part. $_SERVER['REQUEST_URI'] will return the full url and you just have to cut it. – Winter Aug 19 '16 at 22:16
  • $_SERVER['REQUEST_URI']); and then explode it and use what u need – Oncodeeater Aug 19 '16 at 22:17
  • 1
    When processing that URL, how is it supposed to know where the `index.php` layer is? – Barmar Aug 19 '16 at 22:32
  • @Barmar There is this variable `$_GET['rt']` in the *index.php* which contains the rest of URL *(in this case `classname/methodname/idNum?arg=val`)*. I need to get everything which is before the value of that variable. – stack Aug 19 '16 at 22:35
  • 1
    Use `str_replace()` to remove that from `$_SERVER['REQUEST_URI']`. – Barmar Aug 19 '16 at 22:36
  • 1
    Or use `strpos()` to find the position, and `substr()` to return everything before that position. – Barmar Aug 19 '16 at 22:37
  • 1
    These are basic string operations, nothing to do with a URL at all. – Barmar Aug 19 '16 at 22:37
  • @Barmar You know, `$_SERVER['REQUEST_URI']` doesn't contain `http://localhost` *(I mean hostname)* – stack Aug 19 '16 at 22:38
  • 1
    use whatever variable contains the original URL that you want to extract the prefix from. – Barmar Aug 19 '16 at 22:41
  • @stack how about `'http://' + $_SERVER['HTTP_HOST'] + $_SERVER['REQUEST_URI'] - $_GET['rt']` ? – Cave Johnson Aug 19 '16 at 23:06

0 Answers0