Include and Post don't get along.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Include and Post don't get along.

Post by lovelf »

File A:

Code: Select all

<?php $variablea = $_POST['variablea']; require("http://www.domain.com/fileb.php"); ?>
File B:

Code: Select all

<?php echo $variablea; ?>
//fileb.php

File b doesn't seem to use the post made on filea.php which is including this file. Is it possible to make file b use the post made on a, considering a makes the post, and is including b which has the echo statement ?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Include and Post don't get along.

Post by jaoudestudios »

Try include instead of require
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Include and Post don't get along.

Post by lovelf »

Hi, thanks, I tried but still it doesn't work.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Include and Post don't get along.

Post by lovelf »

The same happens with $_GET.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Include and Post don't get along.

Post by Benjamin »

Be specific please. "Doesn't work" is not enough information.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Include and Post don't get along.

Post by jaoudestudios »

Can you post your actual code?

Variables should be inherited into the include. Same rules apply that they will not be inherited into a function or class, unless global is specified (but I advise against it)
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Include and Post don't get along.

Post by Darhazer »

If you use include / require (by the way the only difference is that include produce a warning, while require produce a fatal error when operation is not successful), to include a URL, do not expect that you will receive a PHP code, that will be evaluated. The server can parse and execute PHP script and return only it's output, and you will include the output. And the variables, that are set in the local script won't be available for the executed script. What you are trying to do is applicable only for local application, and it is not the right way anyway. The included file should not depend on global variables, defined before the include.

And if you are trying to achieve this including from a remote server, you have to POST the data to the server (using curl for example)

Hope this clarifies the things and will be helpfull
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Include and Post don't get along.

Post by lovelf »

Code: Select all

<form method="post" action="fileb.php" name="rtd"><input type="text" name="myvar" value="myvar"></form>
<script type="text/javascript">
document.rtd.submit();
</script>
//filea.php

Code: Select all

<?php
$myvar=$_POST['myvar'];
include("filec.php");
?>
//fileb.php

Code: Select all

<?php
echo $myvar;
?>
 
//filec.php

OK, it works, it wasn't working because I had http://www.domain.com/, seems it doesn't work with absolute paths.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Include and Post don't get along.

Post by crazycoders »

No its by design...

Include is a language construct that includes the code from another file depending where it is located. If you include a local file, then the code is included INTO your own code. If you use include on a remote file, it can't get access to the code or it would be a major security flaw...

Imagine: (And lets assume it was possible)

Code: Select all

 
include('http://www.nasa.com/includes/db.php');
 
And now i have included the code that connects me to their database server which by any chance is available through the web. Now i have full access to the NASA database.

So you see the concept here? It would be a major security flaw if it would be possible but fear not, it is NOT possible. Including a file NOT from your server forces the server to request that page just the same way you would query for a webpage. Thus, what gets returned is content that is already rendered such as a webpage with tags. The code, the memory is all used up at THAT location. Nevermind if it's your server, remember that programs are dumb, so they will request it as a distant file even though it is not!

So like Darhazer said, you could actually do:

Code: Select all

 
include('http://www.domain.com/c.php?c='.$c);
 
And now you'd get some information returned, but that info is sent to the buffer immediately i think! You'll have to test that...
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Include and Post don't get along.

Post by lovelf »

Yeah, that works. I see the point.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Include and Post don't get along.

Post by jaoudestudios »

crazycoders wrote:No its by design...

Include is a language construct that includes the code from another file depending where it is located. If you include a local file, then the code is included INTO your own code. If you use include on a remote file, it can't get access to the code or it would be a major security flaw...

Imagine: (And lets assume it was possible)

Code: Select all

 
include('http://www.nasa.com/includes/db.php');
 
And now i have included the code that connects me to their database server which by any chance is available through the web. Now i have full access to the NASA database.

So you see the concept here? It would be a major security flaw if it would be possible but fear not, it is NOT possible. Including a file NOT from your server forces the server to request that page just the same way you would query for a webpage. Thus, what gets returned is content that is already rendered such as a webpage with tags. The code, the memory is all used up at THAT location. Nevermind if it's your server, remember that programs are dumb, so they will request it as a distant file even though it is not!

So like Darhazer said, you could actually do:

Code: Select all

 
include('http://www.domain.com/c.php?c='.$c);
 
And now you'd get some information returned, but that info is sent to the buffer immediately i think! You'll have to test that...
Good explaination! :)
Post Reply