PHP Include from second domain/subdomain on same server - php

I try to include a php file from the same server but a different domain within a php site (domain.com).
include 'domain2.com/file.php';
the file should look like this for example:
echo 'test';
I now want the main site to have the file.php included to echo 'test' on the site.
However, it doesn't.
Does anybody have an idea how I could solve that?
My thoughts are the following, though I cannot solve it myself. Maybe they help you to come up with a solution:
It seems like it is included but the file.php does not return anything, because the php is being run on 'the other domain's server'. So the echo could be done in the other file, but is not included within the main site.
Note that if I include via path, it works:
include '../domain2/file.php';
Note also that allow_url_include and allow_url_fopen are allowed in the php.ini.

Related

php include isnt working

I have a server, and I decided to update the OS, php was also updated from php5 to php7. In the web pages, I have includes to get layout info.
like this:
<html><body>
<?php include "scripts/sidebar.php";?>
</body></html>
(For example).
The php files individually work, but the web pages do not INCLUDE the files in the include statements. Formerly, this had been working. I have looked for errors in /var/log/nginx/error.log and /var/log/nginx/access.log, there are none.
There are also no errors in /var/log/php7.0-fpm.log
I tried changing the php.ini file variable "include_path" to point to the location of the scripts, (and reload nginx and php), but this had no effect.
I tried setting "allow_url_include = On" as described in the PHP documentation: http://php.net/manual/en/filesystem.configuration.php
After restarting php, and nginx, this seemed to have no effect.
What can I do to make the <?php include 'scripts/sidebar.php';?> function work?
if you can use like this.
include_once './test.php';
all files in same directory
You have not mentioned your file name from where you are including the php file, if your file extension is php then it will work.
Try to use
<?php include_once(dirname(__FILE__)."/scripts/sidebar.php"); ?>

including files with $_SERVER['DOCUMENT_ROOT'] on localhost

I just moved my site to my local computer(a mac) but the site is not able to load properly because the include files doesn't seem to be able to include properly.
say eg:
I have the following lines:
include($_SERVER['DOCUMENT_ROOT'].'/pages/includes/functions.php');
on my index.php but the files are not able to be included properly.
I tried to use the functions in the file but it doesn't seem to be able to run the functions properly as per online.
I tried echo-ing $_SERVER['DOCUMENT_ROOT'] but it shows: /Applications/MAMP/htdocs
I'm not sure if that is the full path or if there is anything else i need to add to the url to get my files to be properly included. Perhaps i'm missing something. Any help will be greatly appreciated.

Why PHP include is working on local server and not on website

Problem Description in Brief:
PHP script seems to work on my local web server when I 'include' it from the footer tag of my index.html file, but does not work when I upload it to my website. Note that I have made sure that all paths are correct, and that the script file has its own php tags, etc.
Problem Description in Detail:
Yes, I am new to PHP scripting, and yes, variants of this question have probably been asked before. The answers to a few of the questions I have read have noted the path of the php script files to be incorrect. I have checked all paths and confirmed that they are indeed correct (including those on the web hosting server). Furthermore, I have been successful in getting the script to work on my local server running Apache2 with PHP5, but have not been successful when uploading it to my website.
Essentially, I am trying to implement a hit counter script which I have acquired from a Stack Overflow post labelled Visitors counter for simple web sites like Vinaora. The code that invokes the php script looks something like this....
&ltfooter&gt
&lt!-- Execute Hit Counter Script --&gt
&lt?php include($_SERVER['DOCUMENT_ROOT'].'/php/hitcounter.php'); ?&gt
&lt/footer&gt
For the likes of me, I cannot figure out why it does not work on the web hosting server. I have tried other combinations of invoking the script like,
&ltfooter&gt
&lt!-- Execute Hit Counter Script --&gt
&lt?php include('./php/hitcounter.php'); ?&gt
&lt/footer&gt
and,
&ltfooter&gt
&lt!-- Execute Hit Counter Script --&gt
&lt?php include(dirname(__FILE__).'/php/hitcounter.php'); ?&gt
&lt/footer&gt
All combinations seem to work on my local web server, but not on the website! Also note that, I have no problem invoking other PHP scripts using other methods (even on the web hosting server), eg.
&ltform id="form-query" onsubmit="this.checkValidity();" action="./php/contact.php" method="post"&gt
Any advice/suggestions would be appreciated.
Do you get any PHP error?
First of all, you need to activate error reporting.
Put this before including your file
ini_set('display_errors',1);
error_reporting(-1);
PHP should tell you what's happening.
If you don't see anything, change the filename index.html to index.php and try again.
Maybe be you have used "\" in your include path
Wrong:
<?php include 'includes\header.php'; ?>
You should use "/" to work.
Current:
<?php include 'includes/header.php'; ?>
sometimes it might be dues to casing. check if you you uppercase in naming. on some servers Index.php is not equal to index.php
You might try pasting the "include" code directly into the calling code. Maybe it's the included code itself that's misbehaving...
You are doing completely incorrect thing in the first place.
PHP script seems to work on my local web server when I 'include' it from the footer tag of my index.html
is just totally wrong
There is no such thing as embedding php file within html file (aside from mod_rewrite ...).
For PHP script to be interpreted you must have it (in 99% of cases) with php suffix. This way you allow PHP to distinguish it from regular PHP and send to php interpreter.
Put simply - create this file (a.html):
<body>
abcd<?php echo 'efgh';?>
</body>
and see the result in your browser - use .../a.html
What do you see?
abcd
and not
abcdefgh
On top you always have to have php not the other way around. Solve this or update your question if incorrect.

include statement using a variable in the filename

(Please be patient, this does have something to do with include.) I am waiting for a domain to transfer over and am trying to set it up on the new hosting service ahead of time. I realized that on the old site all the path names were absolute, so all my links on the new host point to pages on the old host. I decided to make them all relative (for future possible moves also). I first did it like this:
index.php
include ('./header.php');
header.php
include "./panel.php";
panel.php
Contents of panel.
This works, and my page displays:
Contents of panel.
Then I decided to set a variable for the domain because I want to include this header file from files in subdirectories and I can use the domain variable to make an absolute path. Right now I have a temporary domain name, which I can change later to the real domain name when the transfer comes through. So I changed header.php to:
$domain="http://tempdomain.com"; //I can change this after the transfer
$panel=$domain."/panel.php";
echo $panel;
if ((include $panel) !== 1)
{
echo "<br>include failed";
}
What I get is:
http://tempdomain.com/panel.php
include failed
I've looked at various sites for include syntax, but I can't find any error in my code. All these files are in the / directory. Any ideas?
When you include, you have to give the directory structured, not the url.
Your hosting server path may be home/public/www/htdocs/your_directory_name/panel.php something like this. Then it will work.
remort include is also posiible
if
1. server's php.ini should allow it.
2. the file which will be included should not be preprossed before include. That means it must return unprocessed code :)
First, the allow_url_fopen flag must be set in php.ini. Otherwise remote include() cannot be done. Run var_dump(ini_get("allow_url_fopen")); to see if it is the case.
Second, "Windows versions of PHP prior to PHP 4.3.0 do not support access of remote files via this function, even if allow_url_fopen is enabled." - see PHP docs
Third, your remote PHP script must produce valid PHP code as output. If you include() via http, then not the script itself, but its output will be included.

how to resitrict user from accessing a folder in php?

I have searched it all around but couldn't find it all i want to know is i have a folder called temp like
->public_html
-->temp
now how do i restrict a user from accessing it from outside server like it gives an error when someone includes it in their php script? i want to do it through php not through .htaccess or apache i tried chmod but it restricts the whole website from the local server only. i have done the constant thing also but if someone knows the constant name then he can still extract the data.
You can't include a remote PHP file. If they have allow_furl_open and allow_url_include set to true and use include('http://yoursite/temp/yourfile.php'), then what gets included is the output of that PHP file. Not the PHP source itself.
So when you have a php file with the following contents:
<?php
$password = "secret";
echo "Test";
?>
And someone includes that file remotely, they'll only get "Test" back, which isn't valid PHP syntax. They won't be able to see the contents of the file, only what gets outputted. The file runs on the remote (or in this case your) server. Whoever includes it gets the output after execution on that server.
So you don't have to do anything like if (!isset(some_magical_constant)) die("Go away!"), that's just plain silly, but unfortunately I've seen it all over the web.
htaccess, only way I know of... don't think it's possible with PHP
<Directory /public_html/temp>
order allow,deny
deny from all
</Directory>
Put an empty "index.html" file inside your "temp" folder. This prevents user from seeing the contents of that folder. Which basically makes it impossible for users to work around it.
As for including it in a script, people have to know what files are in it to use it in a script.

Categories