Hello,
I have one line of php code that would switch out information on a webpage.
<?php
@ require_once (“folder/$Product.php”);
?>
Normally, this would pull in a div with an image and paragraph (the file would be located within the same folder or subfolder). This line of code is used so I can keep the same template and pull in different descriptions depending on the item. This simple code worked fine until we upgraded to PHP5. I tried using include but that just allows the page to load with the inserted information. Is there a slight tweak that needs to be done to the code to get it to work again?
Any help would be greatly appreciated.
I’d say there is an error in folder/$Product.php that is suppressed by the @ which causes the thing not to work.
Remove the @ in front of require_once and see if you get an error.
To: ScallioXTX
I tried the code without @ and I got the same response. When I use require_once or require and test it, the page doesn’t load all the way. I also see in the page source that there is no code where I have the php code (meaning no php or inserted information). When I use inlude_once or include, the page loads up with blank spaces where the information is supposed to be. I’ve checked the files and folders and they are in the correct place. It was working before. The only variable that has changed was the change to php5.
put the following at the top of your php:
error_reporting(E_ALL);
init_set('display_errors', 1);
(while still having removed the @)
do you see any errors now?
Is the file really called $Product.php or just Product.php? The dollar sign at the start looks out of place.
Off Topic:
Moved to PHP forum.
This is the error response I received using the php code. It references three lines of code which are the three sections where I use this php code (each references a different file and location).
Notice: Undefined variable: Product in /Applications/MAMP/htdocs/Pages/Products/Bamboo_Zoo/index.php on line 181
Warning: include() [function.include <http://localhost:8888/Pages/Products/Bamboo_Zoo/function.include> ]: http:// wrapper is disabled in the server configuration by allow_url_include=0 in /Applications/MAMP/htdocs/Pages/Products/Bamboo_Zoo/index.php on line 181
Warning: include(http://www.dandelionforbaby.com/Pages/Products/Bamboo_Zoo/.php) [function.include <http://localhost:8888/Pages/Products/Bamboo_Zoo/function.include> ]: failed to open stream: no suitable wrapper could be found in /Applications/MAMP/htdocs/Pages/Products/Bamboo_Zoo/index.php on line 181
Warning: include() [function.include <http://localhost:8888/Pages/Products/Bamboo_Zoo/function.include> ]: Failed opening ‘http://www.dandelionforbaby.com/Pages/Products/Bamboo_Zoo/.php’ for inclusion (include_path=‘.:/Applications/MAMP/bin/php5.3/lib/php’) in /Applications/MAMP/htdocs/Pages/Products/Bamboo_Zoo/index.php on line 181
Notice: Undefined variable: Product in /Applications/MAMP/htdocs/Pages/Products/Bamboo_Zoo/index.php on line 201
Warning: include() [function.include <http://localhost:8888/Pages/Products/Bamboo_Zoo/function.include> ]: http:// wrapper is disabled in the server configuration by allow_url_include=0 in /Applications/MAMP/htdocs/Pages/Products/Bamboo_Zoo/index.php on line 201
Warning: include(http://www.dandelionforbaby.com/Pages/Products/Bamboo_Zoo/description/.php) [function.include <http://localhost:8888/Pages/Products/Bamboo_Zoo/function.include> ]: failed to open stream: no suitable wrapper could be found in /Applications/MAMP/htdocs/Pages/Products/Bamboo_Zoo/index.php on line 201
Warning: include() [function.include <http://localhost:8888/Pages/Products/Bamboo_Zoo/function.include> ]: Failed opening ‘http://www.dandelionforbaby.com/Pages/Products/Bamboo_Zoo/description/.php’ for inclusion (include_path=‘.:/Applications/MAMP/bin/php5.3/lib/php’) in /Applications/MAMP/htdocs/Pages/Products/Bamboo_Zoo/index.php on line 201
Notice: Undefined variable: Product in /Applications/MAMP/htdocs/Pages/Products/Bamboo_Zoo/index.php on line 342
Warning: include() [function.include <http://localhost:8888/Pages/Products/Bamboo_Zoo/function.include> ]: http:// wrapper is disabled in the server configuration by allow_url_include=0 in /Applications/MAMP/htdocs/Pages/Products/Bamboo_Zoo/index.php on line 342
Warning: include(http://www.dandelionforbaby.com/Pages/Products/Bamboo_Zoo/buy_buttons/.php) [function.include <http://localhost:8888/Pages/Products/Bamboo_Zoo/function.include> ]: failed to open stream: no suitable wrapper could be found in /Applications/MAMP/htdocs/Pages/Products/Bamboo_Zoo/index.php on line 342
Warning: include() [function.include <http://localhost:8888/Pages/Products/Bamboo_Zoo/function.include> ]: Failed opening ‘http://www.dandelionforbaby.com/Pages/Products/Bamboo_Zoo/buy_buttons/.php’ for inclusion (include_path=‘.:/Applications/MAMP/bin/php5.3/lib/php’) in /Applications/MAMP/htdocs/Pages/Products/Bamboo_Zoo/index.php on line 342
The code is $Product.php where Product will be replaced by whatever the name of the php file is. For example,
The page would load and put the snippet of information in the hat.php into the page.
I see. So the $Product variable isn’t being served up before hand. Perhaps you need to show more of your page code.
You cannot (or rather shouldn’t) be including files over http, use the files local path once you have sanitised/validated it.
For example…
<?php
/*
Obtain requested product
*/
$product = filter_input(INPUT_GET, 'product');
/*
Filter it, leave only the letters a through z
*/
$product = preg_replace('~[^a-z]~i', null, $product);
/*
Create file path
*/
$product_file = sprintf('includes/%s.php', $product);
/*
If the file exists, include it
otherwise show error page
*/
if(file_exists($product_file)){
include $product_file ;
}else{
include 'includes/error.php' ;
}
I’d also guess you’re used to using register globals, which is now disabled by default on PHP 5.
Thanks for the input and code.