1

I am trying to get matched parts by comparing

$_SERVER['DOCUMENT_ROOT'] and $_SERVER['PHP_SELF'];

Like if $_SERVER['DOCUMENT_ROOT'] returned: var/www/subdir

and $_SERVER['PHP_SELF']; returned /subdir/index.php/somepage

so how do i match $_SERVER['DOCUMENT_ROOT'] from right

and $_SERVER['PHP_SELF']; from left and then get the matched part

So that I will get this in output: subdir

Thanks for help

user007
  • 3,203
  • 13
  • 46
  • 77
  • >I am trying get base url of a application by comparing $_SERVER['DOCUMENT_ROOT'] and $_SERVER['PHP_SELF']; Like if $_SERVER['DOCUMENT_ROOT'] returned: var/www/subdir and $_SERVER['PHP_SELF']; returned /subdir/index.php/somepage so how do i match $_SERVER['DOCUMENT_ROOT'] from right and $_SERVER['PHP_SELF']; from left and return matched. please can u clear what u exactly wanting...didnot understand – RbG Jun 03 '14 at 08:14

2 Answers2

1

i will advice you to use preg_match. using it you can get the string before first / of $_SERVER['PHP_SELF'] and string after last '/' of $_SERVER['DOCUMENT_ROOT'] and then compare them when a match is found then it will be subdir in your case

preg_match documentations ::

  1. php.net/preg_match
  2. tutorialspoint.com/php/php_preg_match.htm
  3. preg_match EXAMPLES

for additional information you can also get your root directory using

define('DIR_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR); in your php file which is kept in your root directory(i.e. index.php,hopefully)... just saying if it helps you

RbG
  • 3,181
  • 3
  • 32
  • 43
0

If you want the base url, than you are trying the wrong thing. You need to try this

<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>

This question is already answered here :

How to get base URL with PHP?

OR refer to the PHP documentation here

Community
  • 1
  • 1
almaruf
  • 750
  • 7
  • 21
  • NO this will not work for me, cuz if I try to get `REQUESTED_URI` from sub directory then this will give current directory. – user007 Jun 03 '14 at 08:25
  • If I am not wrong you are trying this ? $ss = 'var/www/subdir'; $ps = '/subdir/index.php/'; $ss = end(explode('/', $ss)); $ps = explode('/', $ps); $ps = $ps[1]; $foo = null; if($ps == $ss){ $foo = $ps; } echo $foo; – almaruf Jun 03 '14 at 08:46