PHP include call throws warnings but continues - php

I've got a pretty simple PHP script, but I can't for the life of me work out why one line doesn't work.
The main script:
<?php
include("/includes/processes.php");
?>
[...]
<?php
if(getUserLevel() == 3) {
?>
[...]
processes.php is in the right place and all that. It should be defining getUserLevel(). Here it is:
<?php
function getUserLevel() {
if(isset($_COOKIE["userlvl"]) && isset($_SESSION["userlvl"]) {
if($_COOKIE["userlvl"] == $_SESSION["userlvl"]) return $_SESSION["userlvl"];
else return 0;
}
else {
return 0;
}
}
function usernameIs($name) {
if($_COOKIE["username"] == $name && $_SESSION["username"] == $name) return true;
else return false;
}
?>
So when I go to index.php (the main script), it gives me two warnings and one fatal error:
Warning: include(/includes/processes.php): failed to open stream: No such file or directory in /home/u164546666/public_html/index.php on line 2
Warning: include(): Failed opening '/includes/processes.php' for inclusion (include_path='.:/opt/php-5.5/pear') in /home/u164546666/public_html/index.php on line 2
Fatal error: Call to undefined function getUserLevel() in /home/u164546666/public_html/index.php on line 27
(line 2 is the include() call, line 27 the call to getUserLevel())
It's pretty obvious why I've got the fatal error - because the include() has failed - but why is that? Is it a server config issue or have I just written it wrong?
The file tree:
index.php
/includes
/processes.php

You are probably need the relative path and is missing the .
<?php
include("./includes/processes.php");
?>

Change your include to
include_once dirname(__FILE__) . "/includes/processes.php";
However, I'd go for require as the file includes vital functionality.
require is identical to include except upon failure it will also produce a fatal E_COMPILE_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.

The issue is with your include statement. Just get rid of the parenthesis around that as:
include "/includes/processes.php";

Related

Invalid argument supplied for foreach() in

i keep getting the error for this code even though it works as it should when i run it in browser, but when called by include_once it doesnt work due to the error
foreach(($hostlist->uploaded) as $uploaded) {
if (strcmp($uploaded->url,"http://someurl.com/")==0) {
$host = simplexml_load_file($config['hostlist']);
unset($host->uploaded->url);
unset($host->uploaded->pass);
$host->uploaded->addChild('url',"http://anotherurl.com/");
$host->uploaded->addChild('pass',"anotherpass");
$host->uploaded->asXML($config['hostlist']);
$host->asXML($config['hostlist']);`
echo "URL Changed to http://anotherurl.com/";
}
}
knowing that the variables are as follows:
$config['hostlist'] = 'xml/host.xml';
$hostlist = simplexml_load_file($config['hostlist']);
and this is a sample of the xml file:
<host>
<uploaded>
<work>yes</work>
<url>http://someurl.com/</url>
<pass>pass</pass>
</uploaded>
</host>
As I had early mentioned via comments, your issue maybe that the file is using a relative path and being accessed via include_once by a script on a different folder, which causes the file path of your XML to be invalid.
Here is a simple example of what may be happening to you.
I have the following folder structure:
root
- include_folder/
- include_folder/read_host.php
- include_folder/xml
- include_folder/xml/host.xml
- test_xml.php
When accessing include_folder/read_host.php it reads the file just fine.
This is my read_host.php:
<?php
$config['hostlist'] = 'xml/host.xml';
$hostlist = simplexml_load_file($config['hostlist']);
foreach($hostlist->uploaded as $uploaded)
{
echo $uploaded->work, "\n";
echo $uploaded->url, "\n";
echo $uploaded->pass, "\n";
}
The output:
yes
http://someurl.com/
pass
However if I access from test_xml.php which have the following content:
<?php
include_once('include_folder/read_host.php');
echo "what happens";
It fails with error:
PHP Warning: simplexml_load_file(): I/O warning : failed to load external entity "xml/host.xml" in /home/admin/include_folder/read_host.php on line 4
PHP Notice: Trying to get property of non-object in /home/admin/include_folder/read_host.php on line 5
PHP Warning: Invalid argument supplied for foreach() in /home/admin/include_folder/read_host.php on line 5
what happens
However if I change $config['hostlist'] = 'xml/host.xml'; to the absolute path to the XML file it works just fine and output:
yes
http://someurl.com/
pass
what happens
So in my case the absolute folder was:
$config['hostlist'] = '/home/admin/include_folder/xml/host.xml';

PHP Error Handling - "missing argument for errorHandler"

I'm trying to setup error handling for the first time.
It does actually work and report errors if there is one, but for some reason it always shows errors of 'missing arguments' for the error handling function itself. Please note my error handling function is in a separate file and is included to the index page, I'm not sure if that's the problem :S
Here is my error handling function
function errorHandler($errno, $errstr, $error_file, $error_line) {
if(isset($errstr)) {
# There is an error, display it
echo $errno." - ".$errstr." in ".$error_file." at line ".$error_line."<br>";
} else {
# There isn't any error, do nothing
return false;
}
}
// We must tell PHP to use the above error handler.
set_error_handler("errorHanlder");
Here is the index page
<!-- # Error Handler -->
<? if(errorHandler()) { ?>
<section id="error-handler">
<?=errorHandler();?>
</section>
<? } ?>
Here is the result in the browser (bear in mind there is no php error, so this error handler shouldn't be outputting anything - this is what I can't understand
2 - Missing argument 1 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10
2 - Missing argument 2 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10
2 - Missing argument 3 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10
2 - Missing argument 4 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10
Any idea's why PHP is reporting the missing arguments?
You are calling the function with ZERO argument...
<?=errorHandler();?>
Why do you need to call it anyway?
And there are several typos in your code: replace "Hanlder" with "Handler".
There's no need to do that:
if(isset($errstr)) {
Your error-handling function is automatically called when there is an error (and ONLY in that case!). $errstr is a parameter of this function, it is always set when the function gets executed.
New code:
function errorHandler($errno, $errstr, $error_file, $error_line) {
# There is an error, display it
echo "<section id='error-handler'>$errno - $errstr in $error_file at line $error_line</section><br>";
}
// We must tell PHP to use the above error handler.
set_error_handler("errorHandler");

How do I define fire_event, undefined fire_event error

I'm cleaning up all the errors in an old script and I'm getting one that says undefined function fire_event. How do I define fire_event. everything works fine but i've turned on error_reporting(E_ALL); to fix all hidden errors
Fatal error: Call to undefined function fire_event() in /home/social/public_html/includes/dologin.php on line 96
if (strpos($en['redirect'],'index.php?req=login')) unset($en['redirect']);
$redirect = (isset($en['redirect']) && $en['redirect']!='' ? $en['redirect'] : constant('dir').'members.html');
fire_event('member_login',$line['m_id']);
header('Location: '.$redirect);
exit;
}
That has to be some custom function and we don't know what belongs in there. Search all source files, because you most likely removed or changed a dependency and/or include() or required() line.

try catch a failed include

This is a simple question one hour of Googling do not seem to solve. How do you catch a failed include in PHP? For the following code:
try {
include_once 'mythical_file';
} catch (Exception $e) {
exit('Fatal');
}
echo '?';
With mythical_file not existing, I get the output '?'. I know PHP can not catch failed required because it triggers a Warning Error, but here? What is the best way to catch a failed include? For example, the following works:
(include_once 'unicorn') or exit('!');
but it does not trigger an exception so I cannot retrieve file, line and stack context.
You can use require_once instead of include_once
include and include_once trigger warning (E_WARNING), require and require_once trigger error (E_COMPILE_ERROR). So you should use require or require_once.
php.net quote:
"require() is identical to include()
except upon failure it will also
produce a fatal E_COMPILE_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. "

include or required

What should I use in the following statement? Include or required.
if(a ==b){
require 'requiredfile.php';
} else {
require 'requiredfile_2.php'
}
If in a function, I know that one, either include or require, only includes the file when called, the other one will include the file regardless. Am I correct?
The difference between include and require is that include will only emit a warning when the file is not found, and require will terminate with a fatal error.
If you are loading vital program parts, you probably want to go with require.
Manual
require() is identical to include() except upon failure it will also 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.

Categories