On our platform, users may have different combinations of products in their inventory. Every now and then, a user will report to us that the page that lists these products is crashing. What this "crashing" actually is is PHP just stoping the rendering of a product for no reason at all, and then not rendering the rest of the page (for example, PHP won't include the footer file, so the page just ends at the half-rendered product).
Sometimes it stops at a random product after rendering it fully, sometimes it only renders a product's image and stops the whole page rendering there.
Seemingly random changes to the code seem to make the crashing stop. What do I mean?
If I add this anywhere inside the product loop:
<?php echo "<script>console.log('test');</script>"; ?>
It will stop the error. Just today I decided turning php.ini's display errors on and that fixed the error for some dude. Turning it off brought it back. Some other user was fixed when I changed the loop's syntax from
<?php foreach ($inventory as $code) : ?>
to
<?php
foreach ($inventory as $code)
{
This is the current code for the loop:
<div class="myProductsList">
<?php
$inventory = GetUserInventory($user, $plan);
if ($inventory)
{
$evenOdd = false;
foreach ($inventory as $code)
{
$product = GetProductObject($code, $plan);
$product = $product[0]->ID;
if ($product)
{
$prdTitle = get_the_title($product);
$prdDate = get_the_date('F, Y', $product);
$prdImg = get_the_post_thumbnail_url($product);
$prdLink = str_replace('/offer/', '/product/', get_the_permalink($product));
$evenOdd = !$evenOdd;
$tagCloud = get_field("prd_tagcloud", $product);
$owner = false;
include 'parts/product-card.php';
}
}
}
?>
</div>
And this is the current code for the product-card
<div class="productCard altCard_<?=$evenOdd;?> <?=$owner;?>" stamp="<?=get_the_date('U', $product);?>">
<div class="productCardCont fullw fullh cont col">
<a href="<?=$prdLink;?>">
<img class="fullw" src="<?=$prdImg;?>">
</a>
<div class="productCardAbout fullw hcent cont col npad">
<h3 class="fullw fcent"><?=$prdTitle;?></h3>
<span class="fullw fcent"><?=$prdDate;?></span>
<div class="productCardWarp"></div>
<div class="ctaMain cont col hcent" id="#ctaMain">
<?=ctaMain($prdLink, "Select");?>
</div>
</div>
<span style="display:none!important;"><?=$tagCloud;?></span>
</div>
</div>
I have absoulutely no idea what causes this, especially since no errors appear when I turn them on, no product has any invalid or corrupted data (as you can see I only pull their images, names and date), no inventory code is ever wrong, we have very few users compared to our VM's specs capabilities, the page always loads blazing fast, and as I said:
Random users, with seemingly nothing in common experience these issues, and the 'fixes' often make no sense at all.
One last thing. Sometimes in order to investigate these error reports, I will copy the user's inventory and set it to my own test account (so we both have a identical inventory). Sometimes my account experiences the error, sometimes it doesn't.
The "inventory" I talk about is a table in our database, where each user has a row, and the inventory is just a comma separeated sequence of our internal codes for each products. The codes are a simple number-letter combination.
Example of a inventory that has caused my account to crash:
F2112S04E6B,F2105S03E05,FL21M03V2,F2106S03E06,F2107S04E01,FL20M03V1,F2109S04E03,F2110S04E04,F2111S04E05,F2202S05E02,F2006S1E00,F2208S06E02,F2209S06E03,
Sometimes, if I remove a random product from it, it stops crashing.
PHP version: PHP 8.0.18
Database: 10.6.7-MariaDB
I'm running on a Bitnami Wordpress stack
The question: What is causing this error?
I know it's a complex environment and many things could be at fault, but looking at the code I provided, is anything there that looks potentially guilty?
If the error that I have described is not clear enough, please bare with me as this is a very unique situation which I have never encountered, and I will do my best to make it clearer for you.
EDIT
Tim Morton raised some relevant topics in the comments, so I'll reply to them here to improve context for this question.
'Your first "fix" was to print a javascript command?'
I printed multiple JS commands (console.log) just to see at what point in the PHP script it crashed. When I ran it I noticed that the simple presence of these console.log commands, for some bizarre reason, fixed the crash itself.
'Then changed the syntax of your loop? This seems aimless.'
Yes, and yes it does seem aimless, but believe me it worked. That's why I'm dumbfounded, since it shouldn't.
'Is it printing all of the php-generated content and then failing some javascript?'
No, there is no Javascript here. The only JS was added later (console.log), as I explained, in order to see at what point the PHP crashed. It usually prints the PHP generated product-card.php (included by the loop) and at a random product, stops printing anything, even outside the loop.
'or is it failing in one particular part of the php loop?'
I'm pretty sure its in the PHP loop, but I have not been able to narrow it down.
'To diagnose, you could use try/catch and error_log() to help narrow it down to what is actually failing.'
Will do! The issue is, simply adding a try/catch will probably 'fix it' since as I stated earlier, random changes to the code seem to 'fix' the issue temporarily.
'Does the same record fail every time (hint, it could be the next record that's actually failing)'
So far it seems random. I've tried modifying the inventory record to place different products at different orders. This sometimes works, sometimes it doesn't. So far no pattern emerged.
UPDATE
So far, I've noticed two types of crashes. One of them it prints out halfway thru on a product-card.php iteration, then crashes (so it doesn't even get to the other iterations). On one of these cases, it crashed at only the second iteration.
On this specific case I've been able to see the front end code thru Chrome dev tools, and I noticed that it crashed right here, at the product-card.php
<a href="https://some-link.com/something/
As you can see, it crashed right after the echo, as it didn't even put the finishing quote and '>'.
On the other type of crash, it'd crash at a random iteration too, only the last iteration it'd finish rendering whatever card it was on and then crash, instead of crashing halfway thru.
I saw people commenting that I should just update or modify my PHP installation and be done with it, and to be honest I may do that since this is happening in prod, but this question is aimed at why this is happening.
What does contains $prdLink ?
What does contains $prdImg ?
What does contains $prdTitle ?
Are those variables correct sanitized ?
Does contains chars like ", ', > or anything that could ruins your dom ?
PS. If does, I'm afraid you have bigger problem, a security problem (XSS, etc).
After a very exhaustive wild-goose chase, I've come to the conclusion to the (likely) root cause of the issue:
Too many errors and warnings being logged onto the apache2/error.log file.
Seriously, it was like hundreds of warning logs per second, the file was several GBs large. I don't know exactly why it caused this behaviour, but turning off the logging for everything but critical errors seemed to solve the issue AFAIK (we didn't do anything else, it's AFAIK because we can't be 100% sure that this completely stopped for all our users).
Maybe it was a problem with the exception handler itself, as previously suggested. Maybe it were just too many exceptions to handle, maybe PHP or Apache were overwhelmed with so many logs. We'll never know.
Thank you for everyone who helped, commented and answered, definetely pointed me into the right direction. I were so desperate that I even bountied this, I think. Hopefully this will help another clueless developer out there.
I don't know if that's a typo made on this post or you copy pasted it but
<a href="https://some-link.com/something/ notice you are missing a " (sorry to not comment but not enough karma)
I know this question has been asked millions of times, but please actually take the time to understand my problem before marking as duplicate or closing.
So I am using the following code. For some reason it gets all of the correct header information the first time I run the code EXCEPT for content-length. The second time I run the code it actually gets it correctly. I am retrieving the images from Facebook API if that changes anything.
function remote_filesize($url) {
$data = get_headers($url, 1);
if(isset($data['Content-Length'])) {
return (int) $data['Content-Length'];
}
else {
return -1;
}
}
Edit
Gotta love when you get downvoted with no explanation. Personally I think it should be required to provide a reason.
Anyway, this is still an issue, but in case anyone googling this needs a solution for getting the remote filesize, I would suggest using this awesome snippet from the PHP docs http://php.net/manual/en/function.filesize.php#114952
Sounds like a server caching issue.
So you may have to issue a full GET request instead of just a HEAD request.
Also, maybe check different casing -- 'content-length:' -- lowercase.
Long ago, this code used to work but now it seems something is going on preventing it from functioning properly. I'm hoping somebody can tell me whats going on. I'm concerned upgrades to PHP have killed this code. Or has it?
I use this code (posted below) to check and to see if an html file exists, and if it does it'll use it. If not, it will use the file index2.html.
<?php if ((file_exists("$id.html")) == true) { require ("$id.html"); } else { require ("index2.html"); } ?>
I use this code on my homepage, index.php. However for some odd reason, when I type in a link: example: index.php?id=exampleurlhere, the code isn't checking to see if the file exists and is automatically using the index2.html file, despite my code telling that if the file exists (which it does), then it's required to use it. Why is it now ignoring my command?
Been using this code for years and never had any problems with it until recently it seems. Any suggestions to fix it?
I think the problem lies in the "register_globals" setting. This used to be on by default in the (very) older versions of PHP, but has been removed in the newer versions, as this caused a lot of variable injection attacks in the old PHP.
Your $id came directly from the value in the URL. Now that it no longer auto registers, you can add in a line "$id=$_GET['id'];" just above that line to get it to work again.
This is just a quick fix. I suggest you rewrite the program so that there will not be any change of users accessing files illegally using the URL.
i am creating a CMS and have php creating a page. i have a while loop like this
while($row = mysql_fetch_array($results)) {
echo "some html code" . $row['name'];
its shortend but hopefully you get the point. i have the full thing in my page working just as it should and i wanted to move it to a function include as i want to reuse it. the problem is i do that and it stops working.
i did some testing and found that the function is getting the query result and after doing a var dump both were identical the problem comes when i try to assign it to an array. it comes back as false so in the above code, for example,
$row = false;
im toatly lost in this and if my explanation is confusing i appologise but i am a bit of a newbie i have tried searching but....i dont really know where to begin
any thoughts.
the query you are doing is basically wrong, try posting exactly the code which you have in $query and then let us see the problem.
also, it is better to use mysqli functions.
but for this, edit the question and type the query, or simply put a die(mysql_error()) at the end of your query which is in $query. It will show your exact error.
i fugured it out
when i was testing the function i commented out the original code on the main page but for some reason i had not comented out enough (it was a mix of php and html clearly the php had not been commented out properly) this must have been causing a clash of some kind as when i put the function above the code on my page the function worked and the long code below it did not
sorry for wasting your time guys
So I've got all of this really neato PHP code and I've started doing some reuse with functions out of necessity. I'm debugging, trying to figure out why I can't delete comments on my website while I'm deleting folder (because who wants orphaned comments?)
So I have a call to deletefolder( $parent) inside a file called deletefolder.php. This a function that will recursively traverse my tree structure.
I've include another file inside deletefolder.php. The file is call helpers.php, and it contains the deletefolder function.
The deletefolder function calls deletecomments (kills all the comments per file) and delete file (which kills the file itself).
Now, all of it is just slathered with echo statements to help me figure out what's going on. When I call this combination of functions from other locations I don't seem to have a problem getting messages. But when I call them from the deletefolder.php page I don't get any. Does anybody know why this would be the case?
A few things you might want to verify.
Check the source of the output. You might be echoing straight in a middle of a HTML comment or a tag which is hiding the output.
Are you using output buffering (ob_start()) ? You might be clearing the buffer at some point in your code and forgot all about it.
Different files with the same name but not in the same directory. Do a die() in your function to make sure it actually reaches your code. You might be editing/including a copy of your file (happened to me quite a few times).
Well, I seriously doubt you've found a bug in the echo command, so the problem is with your program logic somewhere. Without seeing your code, it's impossible to say really. Perhaps there's some variable being set or unset unexpectedly, or you're not actually include()ing the files properly.