Jump to content

Getting errors/warnings on the web server but not on development machine


harryg123

Recommended Posts

In php.ini on my local development server, error_reporting is set to

error_reporting = E_ALL | E_STRICT

 

And I get no errors. 

 

However, on the remote web server I found a bunch of warnings in the error log.  I fixed most of them, but this error/warning should have popped up on my development console:

 

[02-Dec-2011 17:47:22] PHP Warning:  array_keys() [<a href='function.array-keys'>function.array-keys</a>]: The first argument should be an array in /home/bpslk/public_html/cart-checkout.php on line 177

 

[02-Dec-2011 17:47:22] PHP Warning:  Invalid argument supplied for foreach() in /home/bpslk/public_html/cart-checkout.php on line 177

 

I can fix the code to eliminate the warning, no prob.  But why am I NOT seeing this error on my development machine?

 

--Thanks

 

 

--HarryG

 

Link to comment
Share on other sites

Do you have display_errors set to ON or log_errors set to ON?

 

Is the php.ini that you are changing the one that php is using? The Loaded Configuration File line in the output from a phpinfo statement is the php.ini that php is using.

 

Also, what does the phpinfo output show for the error_reporting and display_errors settings (something might be overriding your php.ini settings)?

Link to comment
Share on other sites

PFMaBiSmAd:

 

You asked about phpinfo() report:

 

Loaded Configuration File = C:\xampp\php\php.ini (correct)

display_errors = On

error_log = C:\xampp\php\logs\php_error_log

error_reporting = 32767 (which I take to mean "E_ALL | E_STRICT")

error_log = On

 

 

BTW the errors/warnings reported in my original post are not recorded in mylocal php_error_log! Also, line 177 in my code seems to be correct:

 

foreach(array_keys($_SESSION['cart']) as $i) { ...

 

Also it is executing correctly on the server, because it produces a list of items the customer has added to the shopping cart.

 

So why should the server generate the warning? 

 

Server versions:

Apache: 2.2.19

MySQL: 5.0.92-community-log

PHP: 5.2.17

 

=====

 

There was another error that I did not include above, but now seems relevant. The server was reporting a file as not found, but was using its contents just fine. It was an include file:

 

      include ("includes/cart_functions.php");

 

So I changed the file name to cart-functions.php (and changed the filename in all the pages that link to it).  The server no longer reports the error!

 

ManiacDan:

 

I executed the test code you suggested and got the error reported by  the server:

 

Warning: array_keys() expects parameter 1 to be array...

 

I’m thinking I should put in a trouble ticket with the hosting company.  Is there anything I should consider before I write the nastygram?

 

 

Thanks to everyone for their great help!

 

 

Harry

 

 

 

 

Link to comment
Share on other sites

I doubt this is a server issue. I can't think of a situation where an array variable is going to be interpreted as a non-array variable, produce the error, but then yet process the variable as an array. Just makes no sense.

 

You say the line with the error is this

foreach(array_keys($_SESSION['cart']) as $i) { ...

 

Do you know the specific scenarios where the error occurs? Can you run through that script in the production environment and produce the error?

 

You should probably take another look at the 'flow' for your code. How can users access that script? Will they ALWAYS have $_SESSION['cart'] defined and as an array? Where is that value first defined?

 

I'm guessing that users are accessing that page in a manner that was not considered during development/testing.

Link to comment
Share on other sites

Hey I havn't read through the other replies yet so this may have been suggested already, but on each of my PHP files I always put this PHP block on the top of my code

<?php

        error_reporting(E_ALL);

        ini_set('display_errors','1');

?>

 

I have had an error in the past where I could not actually edit my ini file with my hosting company manually so this directly overrides the ini and sets error reporting to true, maybe that will fix your problem.

 

Good luck

 

Alex.

Link to comment
Share on other sites

My guess is you aren't keeping strict track of your $_SESSION array.

 

You should either set up something like this

<?php 

if( !isset($_SESSION['cart']) || !is_array($_SESSION['cart']) )
$_SESSION['cart'] = array();

?>

 

That way, if $_SESSION['cart'] wasn't set on a previous request, it sets it as an empty array (gets rid of warnings)

 

Or you can wrap your foreach in an opposite conditional

<?php 

if( isset($_SESSION['cart']) && is_array($_SESSION['cart']) ) {
foreach( array_keys($_SESSION['cart']) as $value ) {
	// etc
}
}


?>

Link to comment
Share on other sites

scootstah suggested I post the code that generates the error. I hope you all will forgive the code--it’s pretty unprofessional, being grabbed from tutorial pages and modified slightly (with lots of comments so I can remember how it worked later, when I have to update it). It’s a shopping cart page that uses GET rather than POST inputs. When the visitor clicks Add to Cart on a shopping page, the code executes a link to cart.php?cmd=add&id=<item no.> The cart-fuctions.php included file then parses the 'cmd' variable:

 

if (!isset($_GET['cmd'])) { 

$action = "";

} else {

$action = $_GET['cmd'];

// echo ' cmd='.$_GET['cmd']."<br> ";

switch ($action) {

case "add":

if (isset($_SESSION['cart'][$id])) $_SESSION['cart'][$id]['q']++;

else {

$_SESSION['cart'][$id]['q']=1;

$_SESSION['cart'][$id]['d']=0;

}

break;

case "empty":

emptycart();

echo "<script>javascript:window.close();</script>";

break;

case "update":  // stuffs new values into q, then redisplays the page

update_qty();

break;

case "remove": // delete a siingle item from the list

if ($_SESSION['cart'][$id]) unset($_SESSION['cart'][$id]);

break;

} // end of switch statement

} // end of else clause

 

Kind of backwards, but it works. At one time I thought I would need the $action variable elsewhere, but now I don't. I start with (!isset()) just so the little bit of code that clears the @action variable is up on top where I can see it! The cart page goes on to display the item no. and other info grabbed from a MySQL database.

 

The item could not be displayed, nor can the quantity be updated, unless the $_SESSION;['cart'] array has been set.  The index for the cart array is the [id] of the item to be sold. 

 

The offending line that generates the array index warning is in the update_qty function, which, as I mentioned before, cannot be called w/o something being loaded already into the $_SESSON['cart'] array:

 

function update_qty() {

if (isset($_REQUEST['cmd']) && ($_REQUEST['cmd']=='update')) {

foreach(array_keys($_SESSION['cart']) as $i) { [\b]

if (isset($_REQUEST['q'.$i])) {

$_SESSION['cart'][$i]['q']=$_REQUEST['q'.$i];

if ($_SESSION['cart'][$i]['q']>999) $_SESSION['cart'][$i]['q']=999;

if ($_SESSION['cart'][$i]['q']<1) $_SESSION['cart'][$i]['q']=1;

} //end of if

} // end of foreach

$_REQUEST['cmd']="";

} // end of if

} // end of function

 

I know it is always tempting to blame someone else for problems in code. But in this case it seems justified. Remember that the server reported file "include(includes/cart_functions.php)" as missing, then went on to load and execute its contents--which incidentally contains the code block that started this whole thread)!  Also, the server stopped reporting the not found error when I replaced the underscores with hyphens in the included file names. This certainly is not normal behaviour, is it?

 

BTW I stuck in this statement for testing: print_r($_SESSION['cart']);

 

It produced this response: Array ( [32] => Array ( [q] => 5 [d] => 0 ) )

 

I'm still scratching my head.  Any other ideas before I open a trouble ticket?

Link to comment
Share on other sites

So then you can easily fix this error with an if statement.

if (isset($_SESSION['cart']) && is_array($_SESSION['cart'])) {
       foreach(array_keys($_SESSION['cart']) as $i) {
         if (isset($_REQUEST['q'.$i])) {
            $_SESSION['cart'][$i]['q']=$_REQUEST['q'.$i];
            if ($_SESSION['cart'][$i]['q']>999) $_SESSION['cart'][$i]['q']=999;
            if ($_SESSION['cart'][$i]['q']<1) $_SESSION['cart'][$i]['q']=1;
         } //end of if         
      } // end of foreach
}

 

Link to comment
Share on other sites

Thanks, scootstah, I inserted the suggested if () statement, and the error seems to have gone away. I guess my next questions are merely academic:

 

[*]Why did the server report an array() error even though the variable was indeed an array?

[*]Why did the error go away after the server executed the if (is-array()) statement?

[*]Why did the server report a file not found, but load the file anyway?

[*]Why did the file not found error go away when I replaced underscores with hyphens in the include filename?

 

(This is the last gasp on this thread. I'll mark it solved in a few minutes...)

 

Thanks to everybody.

 

-Harry

Link to comment
Share on other sites

Just because you thought it was an array doesn't make it so. The error wouldn't have shown up if it was an array.

 

The include was probably having issues due to a typo. When you changed the name, the typo was probably fixed. I wouldn't bother opening a trouble ticket - the behavior you're attempting has been proven to work. It would be a minor mistake on your end, or a crazy misconfiguration.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.