PHP include content from a separate php file - php

A search form in "search.php" is on the vendor's server and it has been set up so my site can display it. The URL is: http://mysite.com/search.php.
The search result page is on the vendor's server also in a separate file called "result.php". It has also been configured so the URL looks like this: http://mysite.com/result.php.
I would like to insert the "search.php" form in the "result.php" file so users don't have to swtich back and forth between the pages.
I tried:
<?php
include 'http://mysite.com/search.php';
?>
It didn't work, and then I realized that I need to set "allow_url_fopen" to yes to use URL in the include statement. This is NOT something I want to do as I read other posts here there is security issue involved.

I'm not sure you can include another server's php file, allow_url_fopen and allow_url_include are to get the file output of the designated url, i mean, the final result, you won't be able to use the variables or functions from that script.
try using file_get_contents(), or if you are displaying final results anyway, you could just use an iFrame

You can just do
<?php
include $_SERVER['DOCUMENT_ROOT']"./search.php";
?>

You can try
include __dir__ . "/search.php";

Related

Redirect from PHP page is it wasn't included or required

I have a set of PHP files that I am including and using as templates for information that I am storing in a MYSQL Database. These files can be accessed with the link to them. I do not want these files to be accessed if they are not being loaded by another file.
For example:
I have a file called errorBox.php which should only be visible when included in mainPage.php. However, currently, by going to http://myUrl.com/errorBox.php I can access the page.
I have tried using the following:
header("Location: http://myUrl.com");
but unfortunately it doesn't seem to work.
Is there a way to redirect automatically from these pages if they have not been included? Or is there a different solution to my problem?
Thank you in advance.
May you can check if was included/required using the get_included_files function.
And if was not included/required, you can redirect:
header('Location: http://myurl.com');
die;
Use die after to stop execute things.

$_GET[] doesn't work in included file?

I have a webpage which holds the number of the page to display:
mydomain.net/index.php?page=42
This works alright. Now I want to display the page only when a particular cookie is set, and I moved most of the body to an include file, so that index.php only has
<?php
if ($cookie_ok):
include("http://mydomain.net/index_d6skrif9.php");
else:
include("http://mydomain.net/noaccess.inc");
endif
?>
and now the $_GET["page"] in the include file, which is supposed to retrieve the page number returns nothing.
I read that $_GET[] is a superglobal and that superglobals' scopes are across include files. So what's wrong here, and how can I use the page number in the include file?
$_GET works in included files, it does not work in included HTTP resources.
The PHP in index_d6skrif9.php will be executed by mydomain.net before it gets to the PHP program with the include statement in it.
Use a local file path, not an HTTP URL.
include("index_d6skrif9.php");
Alternatively, pass the value to the server you are pulling the include from:
include("http://mydomain.net/index_d6skrif9.php?page=" + urlencode($_GET['page']));
Note that the latter approach has far more opportunity for things to go wrong and is much less efficient than a local file included, so it isn't recommended if you can help it.
You'll need to include them locally, not over-the-web:
if ($cookie_ok)
include("index_d6skrif9.php");
else
include("noaccess.inc");
endif
By using URLs, you're making a web-request and the server executes the PHP in the file and returns the contents (making it have its own set of super-globals).

allow_url_fopen and allow_url_include with PHP require link

I have a website which requires a certain page on all the other pages that I have. My problem is that I can't require this page using require since the link url will change, take this senario as an example:
header.php:
<?php
require('include/database.php');
?>
And then I have a file located at folder: woods/tree.php
If I want to connect to the database from my index file, there is no problem, but if I want to connect to it from tree.php, then the url will not be the correct on header.php anymore. And that leaves me a problem. I tried fixing this by using the entire url:
<?php
require('http//www.example.com/include/database.php');
?>
My problem is now that I can't do that because of security issues with allow_url_fopen and allow_url_include. I will need to find another way to link to that page since I don't want to enable any of the php.ini settings because of its insecurity.
Is there another way for me to require the database so that all files in all folders can access it? Thanks in advance.
Try using an absolute path like:
require $_SERVER['DOCUMENT_ROOT'] . '/include/database.php';
You are seeing a problem where there is none. If I understood you correctly you have this example layout.
|
|-header.php
|-include/
| `-database.php
`woods/
`-tree.php
header.php:
include("include/database.php");
tree.php:
include("../header.php");
This will work, since the include always uses the path from the file where it is included. So tree.php will include ../header.php which includes include/database.php. This will all work nicely.

Force php to look at Document Root

So I was messing around with the twitter api, and I want to include this file in the footer for every page. The footer is loaded via phps require_once function. The problem is I can't use a full url because of url file acsess being turned off for obvious reasons. So I tried to use
require_once $_SERVER['DOCUMENT_ROOT']('/lib/twitter/base.php');
But that failed. What am I doing wrong or how can I do this better?
Try this:
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/twitter/base.php');

get URL in php IN INCLUDE FILE

i have some problem i try to get the uri in php.
I'm using:
$_SERVER['REQUEST_URI']
It works just fine if i do it in the index.php, but, i NEED to get the url in a include file, but, when i do it, it takes the FILE adress, i mean, it shows something like this
adress bar: www.webpage.com/index.php
$_SERVER['REQUEST_URI'] output: webpage/includefile.php
I am explaining myself here? Thanks!
How are you including the file? If it's being included via an HTTP reference then it's actually being served as a page and the functionality you are seeing is correct. If the include path is a local file, you shouldn't be seeing this behaviour
Found this whilst trying to solve the same issue.
My solution that worked is to use $_SERVER['HTTP_REFERER']
This worked well in that it also included the parameters (e.g. ?this=that&foo=bar)
Maybe somewhere in your code (or in another include file) the value is overwritten.

Categories