Invalid argument supplied for foreach() in - php

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';

Related

PHP file_put_contents doesn't work as expected within in register_shutdown_function()

I am playing with output buffering in my script and stumbled upon unexpected behaviour.
Writing to a file works everywhere else in my script, but it doesn't quite do so after entering the register_shutdown_function()-function.
I get a warning telling me I don't have permission to write to the file. So I checked which path I was in. Apparently the current working directory vanishes from the moment you enter the shutdown function.
My question is not particularly on how to solve this; As you can see, I just included the correct path. My question is, is this expected behavior and if so, what's the logic behind it?
I'm on OSX/MAMP-PRO if that matters. Haven't tried it yet on another box.
<?
register_shutdown_function('_lib_bootstrap_end');
ob_start();
_lib_bootstrap_start();
file_put_contents('test1.log','this_one_writes_fine');
function _lib_bootstrap_start()
{
echo getcwd()."\n"; // prints '/Users/macbook/Documents/WWW';
file_put_contents('test2.log','this_one_writes_fine');
}
function _lib_bootstrap_end()
{
global $html;
file_put_contents('test3.log', 'this_one_triggers_warning');
$html[] = ob_get_contents();
$return = implode("\n",$html);
ob_end_clean();
echo $return;
echo getcwd()."\n"; // prints '/';
file_put_contents('test4.log', 'this_one_triggers_warning');
// prints 'Warning: file_put_contents(test4.log): failed to open stream: Permission denied in ob_problem.php on line 24'
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/'.'test5.log','this_one_writes_fine');
}
?>
The behaviour is expected according to the manual page for register_shutdown_function():
Working directory of the script can change inside the shutdown function under some web servers, e.g. Apache.
I don't know the reasoning behind this.

PHP include call throws warnings but continues

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";

Calling included remote functions and variables

I'm trying to include a remote file from one of LAN pcs using include, allow_url_fopen = On and allow_url_include = On.
One local PC (let's say pc2), I have remote.php, which contains:
<?php
echo $var_on_pc1; // this doesn't output
$remote_var = 'Var on pc2';
function square($num){
return $num * $num;
}
?>
In my PC (let's say pc1), I have test.php, which consists of this:
<?php
$var_on_pc1 = 'Var on pc1';
include "http://pc2/path/to/remote.php";
echo $remote_var; // this doesn't output
echo square(4); // this got error
?>
When I run the script test.php, i got the error:
"Fatal error: Call to undefined function: square() in
path/to/test.php on line 7.
What happened? I thought I could call the included functions and variables and vice versa?
If I cannot implement this, what is the best way?
I have no security concern because I use this locally for temporary development.
Type http://pc2/path/to/remote.php into your browser and see what you get. PHP gets exactly the same.
If the PHP file is being processed by the web server at pc2, you likely get zilch in that file, because the code as been processed. You'd need to configure the other server to not process the PHP file and serve its raw source code instead.
This is not a good idea overall.

phpseclib Warning: unpack() [function.unpack]: Type C: not enough inpu

Some SFTP credential works fine for below open source, but some credential throwing below error. what is the root cause for the below error, i saw some threads they posted some workouts, but those solutions does not worked for me.
Why i am getting below error, what will be the solution.
open source: phpseclib
Error: Bellow Error
Warning: unpack() [function.unpack]: Type C: not enough input, need 1, have 0 in \Net\SSH2.php on line 1469
Warning: extract() expects parameter 1 to be array, boolean given in \Net\SSH2.php on line 1469
https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Net/SSH2.php#L1538
What is on your line 1469? The version you linked to has this on line 1469:
while ($this->decrypt_block_size > strlen($iv)) {
Your link is to line 1538, which is this:
break;
If you could post the three lines before and after, too, that'd be great.
Thanks!
I know this is an older question and it's been down-rated, but I am actually having the same error. It happens intermittently, so it's hard to troubleshoot. My errors are referencing line 2127 of \Net\SSH2.php. Here is that line, plus a few surrounding lines:
2121 $response = $this->_get_binary_packet();
2122 if ($response === false) {
2123 user_error('Connection closed by server');
2124 return false;
2125 }
2127 extract(unpack('Ctype', $this->_string_shift($response, 1)));
2129 switch ($type) {
2130 case NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ: // in theory, the password can be changed
2131 if (defined('NET_SSH2_LOGGING')) {
This is the syntax that I'm using to copy a file from an external server to my own after logging in:
$sftp->get('/remote/file/path/file.csv', 'myfile.csv');
Here's the link to the page: https://files.maf.org/mp/forms/ChangePL.php
Actually, that page isn't accessed directly normally, but inside an iframe via https://www.maf.org/change
I will say that I've never produced the error when going to the direct link; only accessing it from the parent page via the iframe. Why would that be the case?

PHP error line numbers completely off

I don't know what's going on with my PHP. I'm using a bunch of classes and a semi-MVC framework that I developed myself (as an experiment). So the PHP file is including a lot of class files.
My PHP line numbers for error messages are completely off and therefore useless and making it impossible for me to debug. For example, right now I'm getting an error message that says:
`Parse error: syntax error, unexpected ')' in /view.php on line 209
The only problem is: there is no ')' anywhere near line 209. Even worse, if I put die() on line 200 or so, it still gives me an error message, now pushed down to line 210. So clearly the line of code is being taken into account, yet for some reason it's not dying.
Another thing is, it's including a header.php file prior to this line. The header file basically just outputs some HTML, and works fine on other pages. Yet on this page, it doesn't even output the header; it's just dying with a blank page and that error message.
Is there anything I can do to use more reliable debugging? If I could have an accurate line number, I'm sure I'd find the bug easily.
Edit: I found the bug. The point of this question is not to solve the unexpected ')' bug. The point is: why are the line numbers inaccurate? The actual error message was on line 218, not 209 or 210.
It's your line endings. Open up a program where you can change the line endings and change it to that of the server and the line number will be correct. I don't know about windows, but on mac you can change line endings using TextWrangler (or BBEdit) and on linux using gedit.
Try adding a Logging function with a simple stacktrace.
Use it:
<?php
require_once("Log.class.php");
$myLog = new Log();
$log->write("Tag here", "Details here");
?>
Log.class.php:
<?php
$debugFolder = "/";
class ExpectedParamaterUndefinedException extends Exception {}
class Log {
private $logs = array();
function __contruct(){
}
function write($type, $message){
$trace=debug_backtrace();
$caller=array_shift($trace);
$fileParts = explode("/", $caller['file']);
$filePieces = explode(".", $fileParts[count($fileParts) -1]);
$this->logs[ $filePieces [count($filePieces ) - 2] ] .= " [ ".date(DATE_RFC822)." ] [ $type ] [Line : ".$caller['line']."] ::: $message \n ";
}
function __destruct(){
foreach($this->logs as $caller => $log){
$fp=fopen($debugFolder . "debug/" . $caller.".log", "a");
fwrite($fp, $log);
fclose($fp);
}
}
}
?>
You will end up with a .log file for each class that you log.
Happy hunting!

Categories