PHP file_get_contents() from another file file_get_contents() - php

How can i get content from a file with file_get_contents() that includes the function file_get_contents() to get content from another file?
for axample:
a.php -> file_get_contents(b.php)
b.php -> file_get_contents(www.google.com)
i want to have html output from www.google.com in a.php
EDIT: a.php is on a different server than b.php

a.php (in first server)
<?php
echo file_get_contents("http://mysecondserver.com/b.php");
?>
b.php (in second server)
<?php
echo file_get_contents("http://www.google.com");
?>
When you open a.php, it is going to take the html content of b.php, and b.php is taking the html content of google.com, so i guess you are done.

The same way you get content from another file that doesn`t have file_get_contents, you can use file_get_contents() as many times as u want in as many files as you want.
For ex:
1.php
<?php
echo file_get_contents("2.php");
?>
2.php
<?php
echo file_get_contents("3.php");
?>
3.php
<?php
echo "OK";
?>
The output will be - OK

Related

Can I use variables of a included file in another included file?

Hello let me explain my question. I'm using PHP at the moment and I'm just playing around a bit now im wondering if I have a file for example:
//This is file1.php
<?php
$text = "Hello";
?>
And include it in file2 and also include file3:
//This is file2.php
<?php
include 'file1.php';
include 'file3.php';
?>
Now Comes my question can I use $text in file3 like so?:
//This is file3.php
<?php
echo $text;
?>
Thank you for your response!
Short ans quick: Yes you can, but NOT in your example. If you call $text in your file2 after including it, it works.
Include does the same, as you have the code in the same file. But look at the order, its important.
Build one file you call over the browser and require all you need in this file.

how to echo the file name that includes a file [changing page title depending on file running]

I tried using php predefined variables but no function seems to work for me.
index.php, page2.php, page3.php contains:
include 'header.php';
now, i want my header.php to echo different title tags depending which file called it.
I tried running echo FILE but echo header.php, rather than index.php or page2.php,.. etc.
is there a way to handle this?
What you could do is set a variable like $title on every of your pages (i.e. index.php, page2.php and page3.php) with your desired title and then echo this variable in the header.php like this (example for index.php):
$title = "Welcome Page";
include("header.php");
And in header.php:
echo $title;
To my knowledge there is no way in determining which file A included an other file B from within file B. For the opposite, determining all included files there however exists the function get_included_files().

PHP - How to echo a variable that contains php?

If I have a piece of code that reads a chunk of HTML from a txt file and then echos that html onto the page, how can I accomplish the same task, but when there is PHP inside of the txt file?
ex:
this is the file being read:
<?php
$filecontent = // read some other file
echo($filecontent);
?>
and this is the page that is reading the file:
<?php
$code1 = //reading the above file
?>
<html>
<?php echo($code1); ?>
</html>
When you want to process files containing PHP code you need to use include instead of echo.
<?php include('your_php_file_name'); ?>
If you have the contents of the file in a string you are in a tough spot because the only way to process the code is eval, and in addition you have to properly set up any environment that the code requires. eval itself should be avoided, and the latter is impossible to do in the general case.
Use include instead of echo:
<?php include($file_that_contains_php); ?>
you need to include the first file and echo statement in the first file will get executed.
<html>
<?php require_once("firstfile.php"); ?>
You need to echo htmlentities($code1), because when you echo then browser will not show it contents, because it try to parse it as a html tag, but htmlentities will encode to safe html output this characters.
If you want to evaulate the code, then you need eval($code1) or include it.

Echoing a variable in an included php file

Okay, so let's say I have a webpage with a defined php $variable, and then I use include("variableEchoed.php), which just echos $variable.
How do I accomplish this? Simply doing what I said above doesn't work.
Again, I want to use an included php file to echo a variable onto another webpage where that variable is defined.
Index.php
<?php
$variable = 'test';
include('variableEchoed.php');
?>
variableEchoed.php
<?php
echo($variable);
?>
The result will be test.
yes its easily doable.
$ cat 1.php
<?
echo $x;
?>
$ cat 2.php
<?
$x="hello world";
include("1.php");
?>
$ php 2.php
hello world
Write a function in the file to echo that $variable.. Call the function just after including the file.

Passing parameter while including PHP script

I want to do this, but it gives error :( for better understanding my problem I'm giving an example:
<?php
include 'script.php?text=hiii';
?>
content of the script.php
<?php
echo $_GET['text'];
?>
So, how can i pass an argument while including the script page?
You could set $_GET['text'] before including the file:
$_GET['text'] = 'hiii';
include 'script.php';
But this obviously won’t affect other variables like $_SERVER['REQUEST_URI'], $_SERVER['QUERY_STRING'] etc.
After you include any script, the included script will act as it's in the same page.
For yourpage.php?text=hiii, that include('script.php') will automatically print hiii, as content of script.php will be in your included page.
You could've done something like this:
<?php
$_GET['text'] = 'what you want to do';
include('script.php');
?>
Actually we don't need to add it to $_GET. just create a variable and use it. Example:
script.php
<title><?php $text; ?></title>
<!--- other code goes here -->
index.php
<?php
$text = 'Welcome back';
include 'script.php';
?>

Categories