Jump to content

something between include() and readfile()?


P3t3r

Recommended Posts

in my forum, if I move /forum1/includes/functions.php to /forum0/includes/functions.php and replace the former file with a file saying

<?function R()
{
$newfile = str_replace('/forum1/','/forum0/',__FILE__);
include($newfile);
}

R();?>

Then everything goes fine. However, when I do the same with a index.php (or any other 'main file'), I just get a blank page. I guess this is because those files identify their own location.

 

I'd want somehow to just 'copy-paste' the contents of the file and execute it in /forum1/, instead of executing it in /forum0/ like include() does. The function readfile() does this, but then I end up with pure text, not executed. So, can I execute the text I obtain in this way? Or is there a command specificly doing this?

 

Thanks in advance!

Link to comment
Share on other sites

readfile takes the file and reads it to the buffer....

 

Basically, readfile is like doing:

 

echo(file_get_contents($file));

 

Include on the other hand, takes the file's contents and parses them.

 

 

Without seeing more code and problems, I don't know what's going on for sure, but I figure this is the problem:

 

Variable scopes in functions apply to includes as well....

 

Example:

a.php

$a = 'a';
function Inc() {
     include('b.php');
}

 

b.php

echo $a;

 

That wouldn't echo anything, and if error_reporting had warnings (or maybe notices) on, it would throw a warning.

 

 

--Edit--

 

Links:

 

http://php.net/include

http://php.net/readfile

http://php.net/variables.scope

Link to comment
Share on other sites

Thanks for your reply. I think the problem is as follows, I made an easy example to reproduce.

 

echo(__FILE__);

include("../forum0/test.php");

 

This echoes /var/www/vhosts/mydomain.com/httpdocs/forum0/test.php. While readfile() would of course just give "echo(__FILE__);".

 

I'm looking for an inclusion method that litterally reads the contents of /forum0/test.php and then executes it, so that in this case the echo should return /var/www/vhosts/mydomain.com/httpdocs/forum1/test.php  instead of /forum0/.

Link to comment
Share on other sites

From a logical standpoint, includes work about like this:

 

file1.php

echo 'File 1!';

include 'file2.php';

 

file2.php

echo 'Hrmmm now we\'re in file 2!';

 

That can be thought of as:

 

echo 'File 1!';

echo 'Hrmmm now we\'re in file2!';

 

I think include is what you're looking for.

[/code]

Link to comment
Share on other sites

I think include is what you're looking for.

How do you mean? I just showed above why include is not what I'm looking for?  ???

 

Of course, if you just have to echo predefined texts it works fine, but use a __FILE__ or ./ or alike, and you'll quickly see you're actually in file 2 and not just running the whole thing from file1 like I want...

Link to comment
Share on other sites

Ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh......................

 

Sorry, I think I just had a slow moment.

 

And actually, include would "litterally read the contents of /forum0/test.php and then execute it".

 

It just wouldn't give you the result you desire (that's the part I read, not what results you want ;p.)

 

You could have something like:

 

eval(file_get_contents('../forum0/test.php'));

 

Which would treat the __FILE__ as if it was in the /forum1/ file.

Link to comment
Share on other sites

Bah sorry for the strike through....  I had [ | s | ] with out the |'s so that reads and executes would be correct grammar, but it went all BBCode on me....

 

 

Anyway you could also just echo one of the $_SERVER valeus in the include....  Not sure which one though.

 

You could try putting print_r($_SERVER) in the include to see which one would give you what you want.

 

 

I usually try to avoid eval.

Link to comment
Share on other sites

Why avoid using eval(); ? Does it slow down your system? Is it a problem to have nested eval()'s?

It would of course be sad if this went at the cost of performance...

 

Anyway you could also just echo one of the $_SERVER valeus in the include....  Not sure which one though.

 

You could try putting print_r($_SERVER) in the include to see which one would give you what you want.

I don't quite get what you mean... here's the output from print_r($_SERVER), with some info in bold (which means hidden/changed for privacy reasons).

Can you tell me how you would use this info?

 

Array (

[HTTP_HOST] => www.mysite.com 

[HTTP_USER_AGENT] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11

[HTTP_ACCEPT] => text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5

[HTTP_ACCEPT_LANGUAGE] => en-gb,en;q=0.5

[HTTP_ACCEPT_ENCODING] => gzip,deflate

[HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7

[HTTP_KEEP_ALIVE] => 300

[HTTP_CONNECTION] => keep-alive

[HTTP_COOKIE] => (weird strings that might contain login info)

[PATH] => /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin

[sERVER_SIGNATURE] =>

Apache/2.0.52 (CentOS) Server at www.mysite.com  Port 80

[sERVER_SOFTWARE] => Apache/2.0.52 (CentOS)

[sERVER_NAME] => www.mysite.com 

[sERVER_ADDR] => (server ip) 

[sERVER_PORT] => 80

[REMOTE_ADDR] => (my ip) 

[DOCUMENT_ROOT] => /var/www/vhosts/mysite.com/httpdocs

[sERVER_ADMIN] => (sysadmin's email address) 

[sCRIPT_FILENAME] => /var/www/vhosts/mysite.com/httpdocs/pen/test.php

[REMOTE_PORT] => 2179

[GATEWAY_INTERFACE] => CGI/1.1

[sERVER_PROTOCOL] => HTTP/1.1

[REQUEST_METHOD] => GET

[QUERY_STRING] =>

[REQUEST_URI] => /pen/test.php

[sCRIPT_NAME] => /pen/test.php

[php_SELF] => /pen/test.php

[REQUEST_TIME] => 1198538095

[argv] => Array ( )

[argc] => 0 )

Link to comment
Share on other sites

This thread is a bit off track.

 

The OP has several sections of a site using copies of the same code and wants to eliminate the common files by being able to have the include() look in the default location relative to the copy of the code first and if it does not find the file, look in the "common" location. If the file exists in the default location relative to the copy of the code, that would mean it is a modified version and it should be used instead of the "common" file.

 

Using the include path idea, I believe you would need to set the include path at runtime for each copy of the code so that it looks first in the correct default location, then in the common location.

 

Here is a "quick fix" way of doing this - any file that you would like to delete because you want to use the "common" file, keep that individual file but only have an include() in it for the actual file you want to be included. This will make the include two levels deep, but this will probably run faster (just a guess) than using the include path and having php do multiple lookups to find the file in each of the folders that is part of the include path.

Link to comment
Share on other sites

The OP has several sections of a site using copies of the same code and wants to eliminate the common files by being able to have the include() look in the default location relative to the copy of the code first and if it does not find the file, look in the "common" location. If the file exists in the default location relative to the copy of the code, that would mean it is a modified version and it should be used instead of the "common" file.
Although I didn't write it in this topic, that was indeed the original goal...

 

Here is a "quick fix" way of doing this - any file that you would like to delete because you want to use the "common" file, keep that individual file but only have an include() in it for the actual file you want to be included. This will make the include two levels deep, but this will probably run faster (just a guess) than using the include path and having php do multiple lookups to find the file in each of the folders that is part of the include path.
... but for the reasons you state there, I changed my mind and I will replace every file with such a 'redirecting' file. And that method seems possible, after corbin's help. Why is this thread off track then? Does this method also have problems?
Link to comment
Share on other sites

......................................................  I tried to help, and then they told me include wasn't working.  They showed me an example that was giving the 'wrong' (more of unexpected) thing so I discussed how to solve it for that particular file.

 

I personally would probably just set the include path in the php script, after getting rid of the old path, so that way, PHP would only have 1 place to look. 

 

Anyway, sorry for any off topic-ness.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.