P3t3r Posted December 24, 2007 Share Posted December 24, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/83052-something-between-include-and-readfile/ Share on other sites More sharing options...
P3t3r Posted December 24, 2007 Author Share Posted December 24, 2007 *bump* Merry Christmas by the way! Quote Link to comment https://forums.phpfreaks.com/topic/83052-something-between-include-and-readfile/#findComment-422566 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83052-something-between-include-and-readfile/#findComment-422573 Share on other sites More sharing options...
P3t3r Posted December 24, 2007 Author Share Posted December 24, 2007 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/. Quote Link to comment https://forums.phpfreaks.com/topic/83052-something-between-include-and-readfile/#findComment-422602 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/83052-something-between-include-and-readfile/#findComment-422634 Share on other sites More sharing options...
P3t3r Posted December 24, 2007 Author Share Posted December 24, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/83052-something-between-include-and-readfile/#findComment-422637 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83052-something-between-include-and-readfile/#findComment-422641 Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83052-something-between-include-and-readfile/#findComment-422645 Share on other sites More sharing options...
P3t3r Posted December 24, 2007 Author Share Posted December 24, 2007 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/83052-something-between-include-and-readfile/#findComment-422665 Share on other sites More sharing options...
PFMaBiSmAd Posted December 24, 2007 Share Posted December 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83052-something-between-include-and-readfile/#findComment-422673 Share on other sites More sharing options...
P3t3r Posted December 24, 2007 Author Share Posted December 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/83052-something-between-include-and-readfile/#findComment-422680 Share on other sites More sharing options...
corbin Posted December 25, 2007 Share Posted December 25, 2007 ...................................................... 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. Quote Link to comment https://forums.phpfreaks.com/topic/83052-something-between-include-and-readfile/#findComment-422707 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.