Member Avatar for bratbo

I want to make include file with a function, which among other things creates a link to the page itself, with an querystring variable added, how can i get the name of the current php file thus avoiding to have it as a string constant that has to be changed for each different page the include file is included in ?

Regards

Martin Bratbo

Recommended Answers

All 9 Replies

I want to make include file with a function, which among other things creates a link to the page itself, with an querystring variable added, how can i get the name of the current php file thus avoiding to have it as a string constant that has to be changed for each different page the include file is included in ?

Regards

Martin Bratbo

$PHP_SELF :cheesy:

Or, if you (very wisely) do not have auto_globals enabled, then $_SERVER.

Get the name of the current php file
Reply:

hope so it works for you.thank you.

<?php
    $currentFile = $_SERVER["PHP_SELF"];
    $parts = Explode('/', $currentFile);
    echo $parts[count($parts) - 1];
?>

If you want the currently executing PHP file you can use the magic constant __FILE__ (http://php.net/manual/en/language.constants.predefined.php) which will give the filesystem path.

OR

If you want to link to the current file in a url fashion you can use $_SERVER['PHP_SELF'] (http://php.net/manual/en/reserved.variables.server.php)
e.g. if the url is http://www.domain.com/script.php then $_SERVER['PHP_SELF'] will contain /script.php

You can try this one.hope so it works.

<?php
        $currentFile = $_SERVER["PHP_SELF"];
        $parts = Explode('/', $currentFile);
        echo $parts[count($parts) - 2];   OR echo $parts[count($parts) - 3];
     ?>

Please check and reply me about it.

d'ya think any of the ops are going to read this crap after 7 years

I appreciated it.

too long!

basename($_SERVER['PHP_SELF'], ".php");

Keep it simple.

basename($_SERVER['PHP_SELF'], ".php") is best answer. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.