1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do I determine the name of a PHP file which is currently executing?

Discussion in 'PHP' started by HenryCan, May 29, 2017.

  1. #1
    Let's say that I'm executing a PHP program named foo.php. Is there any way that PHP can determine for itself what file is executing? I'm picturing a function that returns "foo.php" (or, even better, "/usr/A1234/foo.php".

    I would have thought such a function would already exist in PHP but I can't find anything like this in the PHP manual; maybe I just looked in the wrong place?
     
    HenryCan, May 29, 2017 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    ThePHPMaster, May 29, 2017 IP
  3. HenryCan

    HenryCan Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Excellent! Thanks PHPMaster!!
     
    HenryCan, May 29, 2017 IP
  4. Harshal shah

    Harshal shah Active Member

    Messages:
    126
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    65
    #4
    $_SERVER['PHP_SELF'],$_SERVER['SCRIPT_NAME'] and $SERVER['ORIG_PATH_INFO'] any of one will return you your currently executing file name



    here are small program which will explain you in detail that what is use of $_SERVER.



    CODE

    ----

    <?php

    $indicesServer = array('PHP_SELF',

    'argv',

    'argc',

    'GATEWAY_INTERFACE',

    'SERVER_ADDR',

    'SERVER_NAME',

    'SERVER_SOFTWARE',

    'SERVER_PROTOCOL',

    'REQUEST_METHOD',

    'REQUEST_TIME',

    'REQUEST_TIME_FLOAT',

    'QUERY_STRING',

    'DOCUMENT_ROOT',

    'HTTP_ACCEPT',

    'HTTP_ACCEPT_CHARSET',

    'HTTP_ACCEPT_ENCODING',

    'HTTP_ACCEPT_LANGUAGE',

    'HTTP_CONNECTION',

    'HTTP_HOST',

    'HTTP_REFERER',

    'HTTP_USER_AGENT',

    'HTTPS',

    'REMOTE_ADDR',

    'REMOTE_HOST',

    'REMOTE_PORT',

    'REMOTE_USER',

    'REDIRECT_REMOTE_USER',

    'SCRIPT_FILENAME',

    'SERVER_ADMIN',

    'SERVER_PORT',

    'SERVER_SIGNATURE',

    'PATH_TRANSLATED',

    'SCRIPT_NAME',

    'REQUEST_URI',

    'PHP_AUTH_DIGEST',

    'PHP_AUTH_USER',

    'PHP_AUTH_PW',

    'AUTH_TYPE',

    'PATH_INFO',

    'ORIG_PATH_INFO') ;



    echo '<table cellpadding="10">' ;

    foreach ($indicesServer as $arg) {

    if (isset($_SERVER[$arg])) {

    echo '<tr><td>'.$arg.'</td><td>' . $_SERVER[$arg] . '</td></tr>' ;

    }

    else {

    echo '<tr><td>'.$arg.'</td><td>-</td></tr>' ;

    }

    }

    echo '</table>' ;

    ?>



    OUTPUT :



    PHP_SELF /index.php

    argv Array

    argc 0

    GATEWAY_INTERFACE CGI/1.1

    SERVER_ADDR 198.71.62.81

    SERVER_NAME writephponline.com

    SERVER_SOFTWARE Apache

    SERVER_PROTOCOL HTTP/1.1

    REQUEST_METHOD POST

    REQUEST_TIME 1496221964

    REQUEST_TIME_FLOAT 1496221964.9166

    QUERY_STRING

    DOCUMENT_ROOT /kunden/homepages/10/d586858977/htdocs/phponline.com

    HTTP_ACCEPT text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

    HTTP_ACCEPT_CHARSET -

    HTTP_ACCEPT_ENCODING gzip, deflate

    HTTP_ACCEPT_LANGUAGE en-US,en;q=0.8

    HTTP_CONNECTION -

    HTTP_HOST www.writephponline.com

    HTTP_REFERER http://www.writephponline.com/

    HTTP_USER_AGENT Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.66 Safari/537.36

    HTTPS -

    REMOTE_ADDR 180.211.96.227

    REMOTE_HOST -

    REMOTE_PORT 49712

    REMOTE_USER -

    REDIRECT_REMOTE_USER -

    SCRIPT_FILENAME /kunden/homepages/10/d586858977/htdocs/phponline.com/index.php

    SERVER_ADMIN

    SERVER_PORT 80

    SERVER_SIGNATURE

    PATH_TRANSLATED -

    SCRIPT_NAME /index.php

    REQUEST_URI /

    PHP_AUTH_DIGEST -

    PHP_AUTH_USER -

    PHP_AUTH_PW -

    AUTH_TYPE -

    PATH_INFO -

    ORIG_PATH_INFO /index.php
     
    Harshal shah, May 31, 2017 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    It would be very helpful if you could but such code-blocks inside [ code] [ /code] or [ php] [ /php] wrappers. (Remove the spaces). Makes it a lot more readable.
     
    PoPSiCLe, May 31, 2017 IP
  6. gallitin

    gallitin Well-Known Member

    Messages:
    722
    Likes Received:
    33
    Best Answers:
    2
    Trophy Points:
    165
    #6
    When using .htaccess or other frameworks routes to check what page is being served I use:
    
    echo basename($_SERVER['PHP_SELF']);
    
    PHP:
     
    gallitin, Jun 2, 2017 IP
  7. Blank ™

    Blank ™ Well-Known Member

    Messages:
    223
    Likes Received:
    18
    Best Answers:
    6
    Trophy Points:
    110
    #7
    
    $file = pathinfo(__FILE__, PATHINFO_FILENAME);
    
    PHP:
    Reference: http://php.net/manual/en/function.pathinfo.php
     
    Blank ™, Jun 3, 2017 IP