Get name of file that is including a PHP script - php

Here is an example of what I am trying to do:
index.php
<ul><?php include("list.php") ?></ul>
list.php
<?php
if (PAGE_NAME is index.php) {
//Do something
}
else {
//Do something
}
?>
How can I get the name of the file that is including the list.php script (PAGE_NAME)? I have tried basename(__FILE__), but that gives me list.php.

$_SERVER["PHP_SELF"]; returns what you want

If you really need to know what file the current one has been included from - this is the solution:
$trace = debug_backtrace();
$from_index = false;
if (isset($trace[0])) {
$file = basename($trace[0]['file']);
if ($file == 'index.php') {
$from_index = true;
}
}
if ($from_index) {
// Do something
} else {
// Do something else
}

In case someone got here from search engine, the accepted answer will work only if the script is in server root directory, as PHP_SELF is filename with path relative to the server root. So the universal solution is
basename($_SERVER['PHP_SELF'])
Also keep in mind, that this returns the top script, for example if you have a script and include a file, and then in included file include another file and try this, you will get the name of the first script, not the second.

In the code including list.php, before you include, you can set a variable called $this_page and then list.php can see the test for the value of $this_page and act accordingly.

Perhaps you can do something like the following:
<ul>
<?php
$page_name = 'index';
include("list.php")
?>
</ul>
list.php
<?php
if ($pagename == 'index') {
//Do something
}
else {
//Do something
}
?>

The solution basename($_SERVER['PHP_SELF']) works but I recommend to put a strtolower(basename($_SERVER['PHP_SELF'])) to check 'Index.php' or 'index.php' mistakes.
But if you want an alternative you can do:
<?php if (strtolower(basename($_SERVER['SCRIPT_FILENAME'], '.php')) === 'index'): ?>.

Related

Determine Logo link based on URL using PHP

Situation is getting a logo on:
domain.com/special_dir/any_page
or
domain.com/special_dir/any_dir/
to use a link to [domain.com/special_dir/].
Everywhere else on [domain.com/] the logo must a link to [domain.com/]
This is what I have so far.
<?php
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if( $host == 'domain.com/special_dir/' ) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>
The logo for [domain.com/special_dir/] only works for [domain.com/special_dir/] URL, no others. I suppose the code it doing what it should, I just don't know how to make it recursive. I did search and read a lot of similar situations but none based on PHP code worked for me.
It is WordPress Multi-site setup and the "special_dir" is a regular sub-directory.
How to correct?
Thanks
Your if ($host == 'domain.com/special_dir/') statement means the special link will be printed for domain.com/special_dir/ only. It excludes everything else, including comain.com/special_dir/any_dir and domain.com/special_dir/any_page.
If think you want ...
if (substr($host,0,22) == 'domain.com/special_dir/') { ... }
This did the trick.
<?php
$url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/special_dir/") === 0) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>

How to place include in other place

I make some function in php, but I am getting stuck
if (isset($_GET['page']) )
{
$open = __DIR__.'/../view/html/'.$_GET['page'].'.php';
if (file_exists($open)){
include $open; //<<<<can i//
}
else {
"echo "The file not found";
}
}
If true, I want to include that file in another place, but how?
I am trying to put the code in where I want, but the __DIR__ is not working as I expected. I don't know how to fix it to become right. Solution cannot be found in the tutorial.
I would use:
if( isset( $_GET['page'] ) ) {
switch( strtolower( $_GET['page') ) ) {
case 'download':
include '../download.php';
break;
case 'blog':
include '../blog.php';
break;
// ... And so on
default:
echo 'File not found';
}
} else {
echo 'No file specified';
}
This way you have full control over which files can be included!
You have to do like this.
Use file_get_contents()
if (file_exists($open)){
file_get_contents($open);
}
The answer to your question is yes, that will work. Whether or not you should use readfile(), file_get_contents() or include depends on the contents of the file. If you have php code in that file, you need either include or require. But this actually brings up another problem.
As mentioned in the comments by #anonymous, you are exposing yourself to an LFI attack. To resolve this, pages should be defined as a whitelisted array. You should then check if the page is in the whitelisted array. If it is not, do not attempt to open that file.
$pages = array(
'page1',
'page2'
);
Then you can make a reference and check if it exists.
if(in_array($_GET['page'], $pages)){
//now check for the file
$open = __DIR__.'/../view/html/'.$_GET['page'].'.php';
if(file_exists($open)){
include $open;
}
} else {
//page does not exist, redirect them elsewhere.
header('Location: http://example.com/404.php');
}

PHP: Rendering included files in ajax request

I have the following files
/includes
- file_inc1.php
- file_inc2.php
ajax.php
index.php
index.php calls an ajax request to ajax.php and ajax.php has something like below code
$nextStep = $_GET["nextStep"];
if($nextStep == 1) {
include "includes/file_inc1.php";
} else if($nextStep == 2) {
include "includes/file_inc2.php";
}
With this set-up, does anyone have a clue why the returned response from ajax is empty?
$nextStep = $_GET["nextStep"];
if($nextStep == '1') {
include "includes/file_inc1.php";
} else if($nextStep == '2') {
include "includes/file_inc2.php";
}
Thanks for all your responses. I was able to solve my own problem. I need to make sure that on my includes/file_inc1.php and includes/file_inc2.php files, I use ob_start() and ob_get_clean() like so:
<?php
ob_start();
// HTML + MYSQL + OTHERS
echo ob_get_clean();
?>

PHP search url request

I need some help with this I have for example index.php and and i need to make it something like.
someone access:
index.php?search=blbla
include search.php
else
include home.php
I need an advice with this thanks
Try this
if (isset($_GET['search'])) include('search.php');
else include('home.php');
Well, you could use isset() to see if the variable is set. e.g.
if(isset($_GET['search'])){
include "search.php";
}
else {
include "home.php";
}
$sq = $_GET['search']; //$_GET['']
if (isset($sq) && $sq != '') {
include('search.php');
} else {
include('home.php');
}
I personally prefer to check if a $_GET is set and if it actually equals something like so:
if(isset($_GET['search']) && strlen(trim($_GET['search'])) > 0): include 'search.php';
else: include 'home.php';
This will avoid the problem of putting in the $_GET variable but not actually setting it.
use it like this
if (isset($_GET['search']))
include 'search.php';
else
include 'home.php';
<?php
//if($_GET['search'] > ""){ // this will likely cause an error
if(isset($_GET['search']) && trim($_GET['search']) > ""){ // this is better
include ('search.php');
}else{
include ('home.php');
}
?>
When using isset() you need to be aware that with an empty GET variable like this script.php?foo= that isset($_GET['foo']) will return TRUE
Foo is set but has no value.
So if you want to make sure that a GET variable has a value you might want to use strlen() combined with trim() instead...
if (strlen(trim($_GET['search'])) > 0) {
include('search.php');
} else {
include('home.php');
}
Also you might want to use require() instead of include(). A PHP fatal error is generated if search.php cannot be "required" with just a PHP warning if search.php cannot be "included".

How to check if an include() returned anything?

Is there any way to check if an included document via include('to_include.php') has returned anything?
This is how it looks:
//to_include.php
echo function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
include('to_include.php');
if($the_return_of_the_include != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
So after I've included to_include.php in my main document I would like to check if anything was generated by the included document.
I know the obvious solution would be to just use function_that_generates_some_html_sometimes_but_not_all_the_times() in the main_document.php, but that's not possible in my current setup.
make function_that_generates_some_html_sometimes_but_not_all_the_times() return something when it outputs something and set a variable:
//to_include.php
$ok=function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$ok='';
include('to_include.php');
if($ok != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
If you are talking about generated output you can use:
ob_start();
include "MY_FILEEEZZZ.php";
function_that_generates_html_in_include();
$string = ob_get_contents();
ob_clean();
if(!empty($string)) { // Or any other check
echo $some_crap_that_makes_my_life_difficult;
}
Might have to tweak the ob_ calls... I think that's right from memory, but memory is that of a goldfish.
You could also just set the contents of variable like $GLOBALS['done'] = true; in the include file when it generates something and check for that in your main code.
Given the wording of the question, it sounds as if you want this:
//to_include.php
return function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$the_return_of_the_include = include 'to_include.php';
if (empty($the_return_of_the_include)) {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
} else {
echo $the_return_of_the_include;
}
Which should work in your situation. That way you don't have to worry about output buffering, variable creep, etc.
I'm not sure if I'm missing the point of the question but ....
function_exists();
Will return true if the function is defined.
include()
returns true if the file is inclued.
so wrap either or both in an if() and you're good to go, unless I got wrong end of the stick
if(include('file.php') && function_exists(my_function))
{
// wee
}
try
// to_include.php
$returnvalue = function_that_generates_some_html_sometimes_but_not_all_the_times();
echo $returnvalue;
//main_document.php
include('to_include.php');
if ( $returnvalue != '' ){
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}

Categories