php use $_Get to include a .php - php

at the moment a friend and I are working through a PHP tutorial. We are both new to php, but have some general experience in other languages.
We want to include "testseite.php" into our index.php The "testseite.php" is in the folder content/articles
The way the tutorial does it is to use
include("content/articles/".$_GET['include']);
but if we do that there is the following error:
Warning: include(/users/xxx/www/users/xxx/www/myCms/content/articles) [function.include]: failed to open stream: No such file or directory in /users/flateric/www/users/flateric/www/myCms/index.php on line 5
Warning: include() [function.include]: Failed opening 'content/articles/' for inclusion (include_path='.') in /users/xxx/www/users/xxx/www/myCms/index.php on line 5
We thought ourselves that the problem might be, that it doubles the username (i changed it to xxx) and "users" and "www" in the path.
Thanks for your help

In PHP, include means to load the file into the arguments, like loading a library. So, this error means the file /users/flateric/www/users/flateric/www/myCms/index.php does not exist. You should evaluate if the $_GET value points to a physical file.

It appears that $_GET['include'] is empty (or the include key is non-existent). This is causing the filename to include to be wrong. You're expecting to include a file named this:
content/articles/testseite.php
but actually getting an include for this:
content/articles/
Clearly, this is incorrect and causes the error. If the $_GET params don't include the include value (or it's blank), you should probably consider another approach, such as providing a default value or generating an error to the user.

Related

How are include_once loops handled in php?

I have 3 different files-
fileMainIncludeEverywhere.php
...
include_once('fileMinorInclude.php');
?>
fileMinorInclude.php
...
include_once('fileMainIncludeEverywhere.php');
...
fileToRun.php
...
include_once('fileMainIncludeEverywhere.php');
...
I have a lot of files like fileToRun.php.
Currently I'm not facing any errors in my code, but I want to know if there's any case where this would fail?
I think no error in this case. Because include_once will only load the file for the first time then upcoming load request will be rejected for the same file.
So from your example:
fileToRun.php will load fileMainIncludeEverywhere.php (first call)
fileMainIncludeEverywhere.php will load fileMinorInclude.php (first call)
fileMinorInclude.php will call to load fileMainIncludeEverywhere.php but it will be rejected as it has been already loaded in first step.
Hope it will help.
include_once:
The include_once statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again, and include_once returns TRUE. As the name suggests, the file will be included just once.
Here "code from a file" also entails the executed PHP file.
Note it's generally best practice to use require_once() instead of include_once() unless you've got a specific reason for using include_once() (like say including optional template components). This because require_once() will terminate (fail fast) if the required resource is not found, and not finding it normally should be a terminal failure.

Fail to include file within while loop

So, I have a small script fetching some information from a database.
What it basically needs to do is include a svg generated in 'miescudo.php'.
$sql = "SELECT u.id, u.id_facebook, t.*
FROM user_facebook u
LEFT JOIN user_team t ON u.id = t.id_user
WHERE u.id_facebook='100003809660283'";
$result = mysql_query($sql) or die (mysql_error());
while($row = mysql_fetch_array($result)){
echo '<div>'.include('miescudo.php').'</div>';
}
I get the following error message:
Warning: include(miescudo.php?id=1
): failed to open stream: No such file or directory in full-path-goes-here/tusequipos.php on line 56 Warning: include(): Failed opening 'miescudo.php?id=1
But yet, when I include the file the same way but outside my while loop it works fine.
I assume that the following is the reason to this error, but I haven't figured out how to fix it:
// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
Any help and advice is much appriciated.
Oh, and I don't need any reminders about not using the deprecated mysql_* functions. I am well aware and will update this piece of code accordingly - I promise!
include doesn't understand non-absolute URLs. It does NOT do an http request unless the string you're passing in as an argument is a full-blown url, which means including the protocol:
include('http://example.com/path/to/script.php'); // does an HTTP request
include('/path/to/script.php'); // local request only
include('http://example.com/script.php?foo=bar'); // does an HTTP request
include('script.php?foo=bar'); // local request only, will look for a "...?foo=bar" file
Unless an HTTP request is done, your query parameters are going to be treated as if they're literally part of the filename. So PHP wouldn't look for script.php and pass in foo=bar. It'll look for a file whose name is literally script.php?foo=bar.
You can't include a file with a GET-Parameter, since the GET-Parameter won't be noticed. Either you have to read the output over the HTTP-Protocol (if allow_url_fopen is enabled, you can just use file_get_contents()) or you can simply use the variable (maybe you need to declare it as global) if you include the script. And remember to use the absolute filepath.

How do I use require_once()?

I create a PHP page with the following content:
<?php session_start(); ?>
<?php require_once(./classes/MaterialUtil.class.php);
$mUtil = new MaterialUtil();
?>
I put the MaterialUtil.class.php file in D:\xampp\htdocs\drupal\sites\all\themes\zeropoint\classes, but I get the following error message:
Parse error: syntax error, unexpected '.' in D:\xampp\htdocs\drupal\modules\php\php.module(80) : eval()'d code on line 7
Could you please tell me what I do wrong?
The error is caused from the fact you didn't use a string for the filename, and PHP understood the dot as being the concatenation operator; as such, because there wasn't any value before the operator, PHP gave you an error saying it found the concatenation operator in the wrong place.
As kalabro said, the correct code is the following one:
<?php session_start(); ?>
<?php require_once('./classes/MaterialUtil.class.php');
$mUtil = new MaterialUtil();
?>
This is the part of the answer that is not strictly related to Drupal.
What you are doing is not what I would suggest to do, for two reasons:
You are putting the "classes" directory in the wrong place. Those files are not related to the theme being enabled, but they are related to the page being viewed. Even if you have a single theme, and users are not allowed to select a theme for themselves, it still wrong to put those files in a theme directory.
Putting the files in the directory containing a theme, which will needs to be updated when a new version is available, could cause you to lose the additional files you added, if you are not careful.
Executing PHP through eval() to, e.g., get content to show in a node is not something that you should do. This is because:
As you have used the PHP filter for the node, the node becomes only editable to a restricted group of users. (I would not suggest to allow untrusted users to use PHP as input format)
When you have PHP code that you need to execute, it is always better to create a custom module that is enabled for the site.
If you were trying to include a PHP file from inside a module, then you should use module_load_include(), instead of require_once(), as already suggested by marcvangend.
XAMP server does not run on windows file system format. You must write your file location like localhost/xyz/abc ..

PHP Include operator can't seem to find php script

I am having an odd problem that continues to baffle me. I appreciate your advice....
In a PHP 5.3 script I am including another PHP script using the following code;
include 'moninit.php?id=1234'; // initialize array variables
moninit.php is stored as C:\xampp\htdocs\CarmelServices\moninit.php
In php.ini the include path is:
include_path = "C:\xampp\htdocs\CarmelServices"
So, the include should execute moninit.php but I get the following error returns;
Warning: include(moninit.php?id=1234) [function.include]: failed to
open stream: No error in C:\xampp\htdocs\CarmelServices\SensorW.php on
line 48
Warning: include() [function.include]: Failed opening
'moninit.php?id=1234' for inclusion
(include_path='C:\xampp\htdocs\CarmelServices') in
C:\xampp\htdocs\CarmelServices\SensorW.php on line 48
If I execute moninit.php directly using a browser, it works fine. So, somehow the include cant seem to find moninit. SensorW is also in the same folder as moninit.
Very odd, at least to me. Thanks!
include does not execute a PHP script; it only inserts the contents of the file into the currently-running script.
In your example, you are telling the PHP interpreter to find and open a file named 'moninit.php?id=1234' which does not exist. You may wish to include 'monit.php' itself or find another way (such as cURL) to execute the script and retrieve the response.
You cannot pass array variables with it, it's included directly in your script and will inherit any variables in the available scope. So you can assign to an array and use that instead.
$data = array('id' => '1234');
include 'moninit.php'; // In moninit.php, use $data instead
If you're just routing the parameters passed, don't worry - they already work.
#GeorgeCummins' answer explains how include() works. Also, you can only pass it the name of the file. You can't pass it variables like you're doing. It's a file name, not a URL.

What is the difference between require and include with php? [duplicate]

This question already has answers here:
Difference between "include" and "require" in php
(7 answers)
Closed 8 years ago.
I want to know when I should use include or require and what's the advantage of each one.
require requires, include includes.
According to the manual:
require() is identical to include() except upon failure it will produce a fatal E_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.
As others have said, if "require" doesn't find the file it's looking for, execution will halt. If include doesn't file the file it's looking for, execution will continue.
In general, require should be used when importing code/class/function libraries. If you attempt to call a function, instantiate a class, etc. and the definitions aren't there, Bad Things will happen. Therefore, you require php to include your file, and if it can't, you stop.
Use include when you're using PHP to output content or otherwise execute code that, if it doesn't run, won't necessarily destroy later code. The classic example of this is implementing a View in a Model/View/Controller framework. Nothing new should be defined in a view, nor should it change application state. Therefore, it's ok to use include, because a failure won't break other things happening in the application.
One small tangent. There's a lot of conflicting information and mis-information out there regarding performance of include vs. require vs. require_once vs. include_once. They perform radically different under different situations/use-cases. This is one of those places where you really need to benchmark the difference in your own application.
The difference is this:
include will not fail if it cannot find the resource, require will. Honestly, it's kind of silly that include exists at all, because if you are attempting to load a resource you are pretty much counting on it being there. If you are going to use anything, I would recommend using require_once always, that way you don't run into collisions (ie, if another script is requiring the same file) and your code always works as intended because you know the resources you are including are there (otherwise it is failing).
If a file is optional, include it. For example, you might have a file 'breaking-news.txt' that gets created when there's breaking news, but doesn't exist when there's none. It could be included without the script breaking if there's no breaking news.
If the file is required for the rest of the script to function properly, require it.
Per http://www.alt-php-faq.org/local/78/:
Unlike include(), require() will always read in the target file, even if the line it's on never executes. If you want to conditionally include a file, use include(). The conditional statement won't affect the require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed.
In simple language if we use require we must sure that the file is existing in that era while it is not necessary in case of include. But try to make sure file exist.
Include and require are identical, except upon failure:
require will produce a fatal error (E_COMPILE_ERROR) and stop the script
include will only produce a warning (E_WARNING) and the script will continue
You can understand with examle
include("test.php");
echo "\nThis line will be print";
Output :Warning: include(test.php): failed to open stream: No such file or directory in /var/www/........
This line will be print
require("test.php");
echo "\nThis line will be print";
Warning: require(test.php): failed to open stream: No such file or directory in /var/www/....
Require() and include() are the same with respect to handling
failures. However, require() results in a fatal error and does not
allow the processing of the page. i.e. include will allow the script
to continue.

Categories