I have a problem with an URL-include, which I don't understand...:
For testing I have coded the following script:
<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
echo "First text";
include("http://www.xxxxxxxxxx.de/includetest.php");
echo "Second text";
?>
Allow_url_include is set to on. (via php.ini)
Allor_url_fopen ist set to on. (via php.ini)
The includetest.php only contains plain text for testing. There is no php-code.
The result of that script is only the "first text". After that the script is stopped.
If I use "or die('not working');" after the include, the result is the whole text (also the second text) with the following warning:
Warning: include(1): failed to open stream: No such file or directory
in /srv2/www/htdocs/xhtml-test/_baustelle/testphp02.php on line 6
Warning: include(): Failed opening '1' for inclusion
(include_path='.:/usr/share/php:/usr/share/pear') in
/srv2/www/htdocs/xhtml-test/_baustelle/testphp02.php on line 6
Why is that? I am at a loss...
Here is the problem of code:
// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';
// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';
// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';
Ref. of this Is Here
The file you are including is not a valid php file as it is already surved by a server as php.
This code should work as you want:
<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
echo "First text";
echo file_get_contents("http://www.xxxxxxxxxx.de/includetest.php");
echo "Second text";
?>
You should use Relative paths in PHP include function.
include '/path/to/file.php'; // You can include file by relative path
As per documentation,
include through HTTP
If "URL include wrappers" are enabled in PHP, you can specify the file
to be included using a URL (via HTTP or other supported wrapper - see
Supported Protocols and Wrappers for a list of protocols) instead of a
local pathname. If the target server interprets the target file as PHP
code, variables may be passed to the included file using a URL request
string as used with HTTP GET. This is not strictly speaking the same
thing as including the file and having it inherit the parent file's
variable scope; the script is actually being run on the remote server
and the result is then being included into the local script.
/* This example assumes that www.example.com is configured to parse .php
* files and not .txt files. Also, 'Works' here means that the variables
* $foo and $bar are available within the included file. */
// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';
// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';
// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';
$foo = 1;
$bar = 2;
include 'file.txt'; // Works.
include 'file.php'; // Works.
Warning
Security warning
Remote file may be processed at the remote server (depending on the
file extension and the fact if the remote server runs PHP or not) but
it still has to produce a valid PHP script because it will be
processed at the local server. If the file from the remote server
should be processed there and outputted only, readfile() is much
better function to use. Otherwise, special care should be taken to
secure the remote script to produce a valid and desired code.
Here is understanding of Paths.
1) Relative Paths
index.html
/graphics/image.png
/help/articles/how-do-i-set-up-a-webpage.html
2) Absolute Paths
http://www.mysite1.com
http://www.mysite2.com/graphics/image.png
http://www.mysite3.com/help/articles/how-do-i-set-up-a-webpage.html
The first difference you'll notice between the two different types of links is that absolute paths always include the domain name of the website, including http://www., whereas relative links only point to a file or a file path. When a user clicks a relative link, the browser takes them to that location on the current site.
For that reason, you can only use relative links when linking to pages or files within your site, and you must use absolute links if you're linking to a location on another website.
For more information, Refer this link also.
Hope it will help you :)
Related
I need to set these anchor href tags to absolute paths because when I have html docs in folders, the header where I include a lot of stuff doesn't work (need ../ in some cases).
I have looked at a few posts that have suggested using:
$root = realpath($_SERVER['DOCUMENT_ROOT']);
or
$root = "http://" . $_SERVER['SERVER_NAME'];
The first option resulted in an error:
localhost/:1 Not allowed to load local resource: file:///C:/xampp/htdocs/trips/index.php%7D
The second option resulted in this error:
Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\xampp\htdocs\MountainPlanner\includes\header.php on line 17
Warning: include(http://localhost/MountainPlanner/includes/db.php): failed to open stream: no suitable wrapper could be found in C:\xampp\htdocs\MountainPlanner\includes\header.php on line 17
Warning: include(): Failed opening 'http://localhost/MountainPlanner/includes/db.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\MountainPlanner\includes\header.php on line 17
I am using XAMPP if that makes a difference.
Thank you!
HREF navbar path != System Path
URL path
It's what you see in your browser address, it's used by HTML (CSS, JavaScript, etc...). PHP don't need to worry about it (except by some streams functions).
To create the base path dynamically, I've used this script
httpProtocol = !isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on' ? 'http' : 'https';
$base = $httpProtocol.'://'.$_SERVER['HTTP_HOST'].'/';
And, when put in the href a tag:
Link
System Path
It's what the server use to find any file on the server. PHP uses it to include/require any file located on the server or a shared one. Streams functions could use system path too and URL as well.
As I can see, you're asking about system path. I've used this code to normalize the application path:
ini_set('include_path',
implode(
PATH_SEPARATOR,
array_merge(
array(dirname(__FILE__)),
explode(PATH_SEPARATOR , ini_get('include_path'))
)
)
);
Then, my application root could be used as a absolute path only for my application:
/Application/Require.php
/Application/Script.php
index.php
It'll work on any file as well:
require('Application/Require.php');
require('Application/Script.php');
Set the first line somewhere in your config or constants file or even 1st line in your file.
<?php $base_url = "http://localhost/mysite/"; ?>
Then you can create href links like this:
clickie
$_SERVER contains the headers filled by the web server. It is not reliable and it disables your code from being run in local or testing environments. From php.net:
$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI/1.1 specification, so you should be able to expect those.
In summary, do not rely on this to provide information about your server.
PHP provides magic constants __FILE__ and __DIR__ that gives you the full path to the file or directory of the current file.
For PHP includes, you should use these constants with the relative path to the file you wish to access.
For example:
include __DIR__ . '/../file_in_previous_directory.php';
include __DIR__ . '/file_in_same_directory.php';
I am trying to include a php file in a page via
require_once(http://localhost/web/a.php)
I am getting an error
Warning: require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0
I changed allow_url_include=1 in the php.ini and that worked but I don't think that everybody will let me change their php.ini file.
So, is there any way to accomplish this?
The warning is generated because you are using a full URL for the file that you are including. This is NOT the right way because this way you are going to get some HTML from the webserver. Use:
require_once('../web/a.php');
so that webserver could EXECUTE the script and deliver its output, instead of just serving up the source code (your current case which leads to the warning).
I had this same error while trying to include a PHP file in my Wordpress theme. I was able to get around it by referencing the file name using dirname(__FILE__). I couldn't use relative paths since my file was going to be included in different places throughout the theme, so something like require_once '../path-to/my-file' wouldn't work.
Replacing require_once get_template_directory_uri() . '/path-to/my-file' with require_once dirname( __FILE__ ) . '/path-to/my-file' did the trick.
try to use
<?php require_once($_SERVER['DOCUMENT_ROOT'].'/web/a.php'); ?>
You have to put the path to the file. For example:
require_once('../web/a.php');
You cannot get the file to require it from internet (with http protocol) it's restricted. The files must be on the same server. With Possibility to see each others (rights)
Dir-1 -
> Folder-1 -> a.php
Dir-2 -
> Folder-2 -> b.php
To include a.php inside b.php => require_once('../../Dir-1/Folder-1/a.php');
To include b.php inside a.php => require_once('../../Dir-2/Folder-2/b.php');
WORDPRESS is having this error mostly:
SOLUTION:
Locate your PHP installed directory on Remote live hosting SERVER or "Local Server"
In case of Windows os
for example if you using xampp or wamp webserver. it will be in xammp directory
'c:\xammp\php'
Note: For Unix/Linux OS, locate your PHP directory in Webserver
Find & Edit PHP.INI file
Find 'allow_url_include'
replace it with value 'on'
allow_url_include=On
Save you php.ini & RESTART you web-server.
require_once('../web/a.php');
If this is not working for anyone, following is the good Idea to include file anywhere in the project.
require_once dirname(__FILE__)."/../../includes/enter.php";
This code will get the file from 2 directory outside of the current directory.
include and require functions require file path, but you are giving file URI instead. The parameter should not be the one that includes http/https.
Your code should be something like:
include ("folder/file.php");
For me the resolution with sending data on a PHP request:
$ch = curl_init('https://localhost/request.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_REQUEST));
echo curl_exec($ch);
curl_close($ch);
require_once(APPPATH.'web/a.php');
worked for me in codeigniter
check reference
echo file_get_contents('http://localhost/web/a.php'); //Best Example
In one of my magento template phtml files I am trying to include a seperate php file.
When I include it I get nothing outputted and when i use require instead I get the following error
Fatal error: require(): Failed opening required 'http://www.site.co.uk/dir/test.php'
(include_path='/home/usr/public_html/app/code/local:/home/usr/public_html/app/code/community:/home/usr/public_html/app/code/core:/home/usr/public_html/lib:.:/usr/lib/php:/usr/local/lib/php') in /home/usr/public_html/app/design/frontend/theme/edits/template/review/product/view/list.phtml on line 30
The first line of the error shows the correct url path and when i go to it directly it works - it just doesn't like being included/required from the phtml template page.
I've tried the following in the phtml file (using magento's BaseURL, absolute path and the relative path):
<?php
$root = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
require/include ($root.'dir/test.php');
?>
<?php
require/include ('http://www.site.co.uk/dir/test.php');
?>
<?php
require/include ('../../../../../../../../../dir/test.php');
?>
Instead of Mage::getBaseUrl use $root = Mage::getBaseDir();
require('http://....') is going to (if it's enabled) do a full-blown HTTP request to the specified URL. Since it's a URL, that webserver is going to EXECUTE the php script and send you its output.
If you're trying to actually load the CODE of that test.php, e.g. the raw un-executed PHP, then you cannot use an http request. The remote server has NO idea that the http request is actually from PHP and is coming from in include(). It'll just blindly run that script and send over the output.
You'd need to (say) rename the remote file to test.txt, so that the webserver sends it out as-is, and then that text will be executed as PHP code on YOUR server.
And as far as the other paths go, if your $root is something like:
/home/sites/example.com/html
then the require is going to be looking for
/home/sites/example.com/html/dir/test.php
Is there a dir subdir inside whatever your site's $root is?
I'm developing a site on my local wamp stack. I have created an alias to view the site so i go to localhost/eee/ to view it. Ideally i would like to go to www.eee.lo but ever since upgrading to win8 I can't get it to work.
So this is the problem, i'm making modules for the website so i don't have to change all the code etc... And i don't want to have to go around changing all the url's when i migrate to the online server so i'm creating a file called _control.php which has this;
$_SITELOC = "localhost/eee/";
And then each time i want to include a file i will go;
include "$_SITELOC/scripts/inc/_header.php";
But this doesn't work and i can't work out why as if i echo it rather than include it and then i take what it prints and put it into the url it goes to the correct file. But it throws errors on the include, it gives two warnins;
Warning: include(localhost/eee/scripts/inc/_header.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3
Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'localhost/eee/scripts/inc/_header.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3
I read somewhere that it might be to do with the include path so i tried;
set_include_path(get_include_path() . PATH_SEPARATOR . $_SITELOC."/scripts/inc/");
but this too did not work and now i'm not sure where to go.
Thanks, Chris
localhost/eee/ is your public address that you can use in your web browser. This public address should more appropriately be written as http://localhost/eee/. When you move to web server, you get the public address http://www.eee.lo/.
When including files, you have to use file paths. For example, if you have your www (or httpd, whatever) directry in D:\ on windows, then your include path should start with D:\www\eee\.
So, basically you have to use two variables to keep paths.
$_SITELOC = "http://localhost/eee/"; //For all URLs used in your HTML document.
$_INCPATH = "D:\www\eee\\"; //For all internal file includes.
In practice, you will need both of these, and it is good practice to keep the website address and internal paths out of your main script because when uploaded to remote server, not only your public address changes, but you will also have to deal with absolutely different internal (include) paths.
Your idea is basically good, to define one (root) path of the application and include files based on it, but unfortunately you're not doing it quite right. You have basically two ways of doing that.
One way (which I personally find better) is to include local files in your file system, where you can define the root path, i.e. like
define ('ROOT', 'your/document/root/path');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';
The other way would be to include a web resource, what you're trying to do, but you've forgotten to specify the scheme (protocol) you want to use, i.e.
define ('ROOT', 'http://localhost/eee');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';
For more information, see the examples, provided by the documentation for include
Note: If you want to include the source of a php file, i.e. file with definitions of functions, etc., use the first approach. Including files, using the second approach will only include the output produced by that file.
If you include() a URL, you will (probably) be including the output of the script's execution, when you want to include the script's source. It seems like you actually want to include by local file system path.
I'm a total PHP noob and am using a pretty simple PHP include:
<?php include("~head.php"); ?>
to do a bit of templating for a website (to achieve common headers, footers, menus for all my pages).
NOTE: The tilde (~) is simply there to make the directories easier to look at (pushes main files to the top of the list when sorted alphabetically)
It's working great for files that are in the same directory but when I reference a file outside of a directory, like so:
<?php include("../~head.php"); ?>
However, it simply doesn't seem to be finding the file as the header is clearly not being pulled into the markup.
Conversely, if I reference the file with a full url, e.g.
<?php include("http://example.com/~head.php"); ?>
I get the following error code on my page.
Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/content/65/7392565/html/bikini/angela_bikini.php on line 1
Warning: include(http://example.com/~head.php) [function.include]: failed to open stream: no suitable wrapper could be found in /home/content/65/7392565/html/products/product_a.php on line 1
Warning: include() [function.include]: Failed opening 'http://example.com/~head.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/65/7392565/html/products/product_a.php on line 1
Strangely, the "../file.php" syntax works for non-header files (e.g. the include I'm using for the menu).
As such code's gotten to be a bit of a fragmented mess and is difficult to maintain changes across all the different pages. Any thoughts or solutions would be very much appreciated. I really am a noob tho so I probably won't be able to wrap my head around anything too fancy. : )
Thanks for your time.
Jon
Rather than using only the ../ to get the directory above, a construct like this will create the full filepath:
// Assuming you are including from the root
$application_path = dirname(__FILE__);
include("$application_path/../header.php);
Typically I'll do this by defining a constant, rather than using a variable.
define('APP_PATH', dirname(__FILE__));
Use this as:
// Assuming you are including at the file root:
define('APP_PATH', dirname(__FILE__));
include(APP_PATH . "/include/head.php");
// Assuming you are including from /include (one directory in)
// append a "/../" onto the end to indicate that the application
// root is one directory up from the currently executing file.
define('APP_PATH', dirname(__FILE__) . "/../");
include(APP_PATH . "somefile_at_the_root.php");
You have to be careful with the tilde! Under UNIX-like operating systems, the tilde is a shortcut to your home directory. If maybe the Apache server runs under the account www, your file-reference could be interpreted like this:
/home/www/head.php
And for the approach of using the full URL, the error says all:
URL file-access is disabled in the server configuration
Ignoring that it isn't best practice to use full URLs (because your folder structure could change etc.), you have to enable allow_url_include in your php.ini (see PHP.net).
If you really want to have your important files on top, you could use the underscore _.