Note that there are some explanatory texts on larger screens.

plurals
  1. POGet the URL of a file included by PHP
    primarykey
    data
    text
    <p>I have a class that is included in a bunch of other scripts in different directories. Occasionally, this class will need to issue a redirect to another file that is located in the same directory. I am having trouble getting this to work in a consistent manner.</p> <p>For instance:</p> <pre><code>// file.php require('../class/include.php'); // include.php if(/* some condition */) { $mydir = '...'; // should be '../class' in this example header("Location: $mydir/setup.php"); die(); } </code></pre> <p>I have done the following with promising results, but it doesn't seem to always work and I am not sure if I am using the server variables wrong or if the configuration isn't correct for the instances that don't work.</p> <pre><code>$len = strlen($_SERVER['DOCUMENT_ROOT']); $mydir = str_replace('\\', '/', __FILE__); // switch slashes for Windows systems $mydir = substr($mydir, $len, strrpos($mydir, '/') - $len + 1); </code></pre> <p><strong>NOTE</strong> I want the URL, the path doesn't help because I can't redirect to <code>/var/wwwroot/some/directory</code> or <code>C:\domains\mydomain\some\directory</code></p> <p>This may be more a question of <code>$_SERVER['DOCUMENT_ROOT']</code>. Notice I am stripping off the length of <code>$_SERVER['DOCUMENT_ROOT']</code> from <code>__FILE__</code> to get the web accessible portion.</p> <p><strong>EDIT</strong></p> <p>To make things a little simpler, is there a way to get the equivalent of <code>$_SERVER['REQUEST_URI']</code> or <code>$_SERVER['PHP_SELF']</code> for the included file rather than the parent file?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload