server side code crashing only IE? - php

Strange issue: PHP runs a nested foreach that generates a string (basically a calendar that shows people's holidays).
This string goes in $data['grid'] and sent to the view. var_dump shows: string(188263)
The string is printed out fine in the webpage when viewed with FF and Chrome but for some reason when viewing with IE6, 8 and 9 (not tested in 7) it crashes the browser every single time; it hangs and nothing ever appears on screen.
IE can show the string when it's printed out in the controller, so before it's passed to the view.
This works fine in IE:
print $str; die;
$data['grid'] = $str;
$this->load->view('conge', $data);
This crashes IE:
//print $str; die;
$data['grid'] = $str;
$this->load->view('conge', $data);
The total "weight" of the page is 192KB and there is no JS running. It feels like a PHP / memory problem but the fact that the "bug" only exist in IE makes little sense.
Any ideas how this can be debugged?
EDIT: When I saved the rendered output from FF in to a static HTML file and load that in IE it sill crashes. The string was printed out without line breaks (loooong) but when I add \n the problem persists. Anyway, one step closer.
EDIT2: It seems to be due to errors in the HTML markup that cause IE to crash (?!). Case closed!

The serverside code cannot crash the browser.
The HTML file generated can crash the browser so you need to examine the outputted HTML carefully.
Point the w3c validation service at the URL and see what it comes back with.
If that yields no results start commenting out large section of the header, then the body etc until you get a successful render then you can investigate a small section of code instead.
Btw I'm assuming 192k includes images, CSS etc else that's a monster HTML file!

Related

flush functions are deleting my html temporarily

Notice: Yes I did ask this question once before but it got immediatly marked as a duplicate although the linked duplicate had almost nothing to do with my question.
I wrote a php script that is executing my application to download images from the web. And in order to show some kind of progress being made on my website I used flush to echo out some information for the User. Here is my code:
echo '<li class="list-group-item list-group-item-info">Starting to gather data!</li>';
ob_flush();
flush();
$url = $unsplash;
$cmd = $unsplash . ' - ' . $amount;
exec($cmd);
echo '<li class="list-group-item list-group-item-success">Gathering Data Completed</li>';
ob_end_flush();
The code I am using does work without any problem (besides this one) so there is no error there!
Unfortunately every kind of HTML content I write under my php script is being deleted for the duration of the script executing (which sometimes can take up to 5 minutes). But immediately after the script finished the Content beneath reappears. Another thing worth mentioning is, that when I open DevTools in Chrome (F12) during the scipt is being executed is shows nothing!
Am I doing something wrong ? I cant seem to figure it out...
Thank You
I'm not sure, exactly what you mean by "being deleted", but what you need to take into consideration is that output buffering can happen at multiple levels (PHP, Web Server, Client UA, etc..).
If the output from your script does not appear right away, it's likely that you have output_buffering enabled at a higher level (i.e. in your php.ini or other loaded configuration). Because output buffers cascade, they flush at each level. Check your phpinfo() and if you see a value other than 0 for output_buffering, then you need to edit the php.ini file showing in phpinfo() under Loaded Configuration (near the top) and change that value to 0 then restart your parent PHP process.
Another thing to consider is that some browsers won't render some HTML block-level elements like <div>, <ul>, etc... until they are closed. The content may have arrived at the client, but some browsers don't process the rendering of the content until the block level element is complete. So a better way to test that the output is being received on the client end without these variable nuances might be to send a text/plain Content-type header from your PHP to get the HTML rendering stuff out of the way. header('Content-type: text/plain'), if turning off the output_buffering in PHP still doesn't give you the desired result.

PHP destoys a variable if its value starts with < symbol

This is the most weird PHP issue I ever had. Several minutes ago I noticed that my script stopped working properly. To keep it short, after debugging I found that if any variable in POST form has < symbol at the start, PHP doesn't process it anymore. This is super-strange because my code worked for years. Now, this issue happens not only on localhost, but also on 2 different servers (just installed the script there to test). So the issue can't be related to PHP config in any way.
This is the actual code I use to get all variables submitted via POST form:
if (isset($_POST)) {$form_array=$_POST;} //super variable with all form variables
Then I "extract" array values to create actual variables with correct values. But it just stopped working now. I added extra line of code to debug submitted variables:
print_r($form_array);
If I enter something into form and submit it, result is:
Array ( [var1] => something [submit_ok] =>)
However, if I enter <something, the result is:
Array ( [submit_ok] => )
The variable doesn't even exist! If I enter something<, it starts working again. However, If I enter something<here, it doesn't work again. Put simply, if any value in form contains < symbol followed by any letter, variable doesn't even exist. What the hell?
P.S. Adding HTML code of the form (this is COMPLETE code):
<form action="test.php" method="post">
<input type="text" name="var1">
<input type="submit" name="submit_ok" value="do">
</form>
I, assuming you're testing this in a browser, think it's just the browser interpreting the < symbol as the beginning of a HTML tag and then trying to render it, which fails, because it doesn't know what to do with the <something> tag.
If this is what you're seeing in the browser:
But after pressing CTRL+U (in Chrome) you're seeing:
Then it's just a rendering "problem" and my calculations were correct.
Consider adding this in your PHP files, as it tells the browser not to treat the output as HTML but rather as plain text:
<?php
// This must be called before _any_ other output is sent to the client.
header('Content-type: text/plain');
I have no logical explanation, but once I rebooted all the devices (router, server, computer) everything came back to the normal state. I noticed that after computer reboot, Firefox was updated (so maybe that update caused the issue somehow, no idea).
Anyways, everything is working normally again without modifying a single line of code.
The less than gets URL encoded as "%3C" so printing it won't work since browsers see it as a broken HTML tag, but if you...
$var1=urldecode ($_POST['var1']);
Before using $var1 in a database query, it should work. To actually print it, you could do
echo html_entity_decode(urldecode($var1));

file_get_contents not actually grabbing file? Blank?

Try this sample code I threw together to illustrate a point:
<?php
$url = "http://www.amazon.com/gp/offer-listing/B003WSNV4E/";
$html = file_get_contents($url);
echo($html);
?>
The amazon homepage works fine using this method (it is echoed in the browser), but this page just doesn't output anything. Is there a reason for this, and how can I fix it?
I think your problem is that you're misunderstanding your own code.
You made this comment on the question (emphasis mine):
I've never used those utilities before, so maybe I'm doing it wrong but it only seems to be downloading this page: https://www.amazon.com/gp/offer-listing/B003WSNV4E/ref=dp_olp_new?ie=UTF8&condition=new
This implies to me that an Amazon page is appearing in your browser when you run this code. This is entirely expected.
When you try to download https://rads.stackoverflow.com/amzn/click/B003WSNV4E, you're being redirected to https://www.amazon.com/gp/offer-listing/B003WSNV4E/ref=dp_olp_new?ie=UTF8&condition=new which is the intent of StackOverflow's RADS system.
What happens from there is your code is loading the raw HTML into your $html variable and dumping it straight to the browser. Because you're passing raw HTML to the browser, the browser is interpreting it as such, and it tries (and succeeds) in rendering the page.
If you just want to see the code, but not render it, then you need to convert it into html entities first:
echo htmlentities($html);

Firefox displays incorrect value for php strlen function on variable

I have a variable that I am calculating the length of and in all browsers except FF (IE, Chrome, Safari) the value is 0.
However in FF, the value is 65 (see screenshot - value beneath photograph)
screenshot
Link to site page
I have cleared my cache with cc cleaner and using the clear cache option in FF itself.
The code I am using wordpress and the code to display the strlen value is:-
<? $liurl = get('ksl_linkedin');
$liurl = trim($liurl," ");
echo strlen($liurl);
?>
Any help would be greatly appreciated.
Thanks
Jonathan
The result of a PHP function has nothing to do with the browser - it's calculated before the data gets to the browser. (for what it's worth, I get "0" with Firefox as well.)
The only scenario where the browser could play a role is when the data is input by the user some way, or enters the script through a GET or POST variable.
My suspicion is that your get() function returns different values, maybe depending on whether you're logged into Wordpress or not.
What does the function do?
Can you show us an example link?
I bet it has nothing to do with Firefox. PHP is executed at the server side, so it is not interacting with the browser. See also How does PHP work.
What is get() doing? Probably this the source of the problem.
Btw it shows 0 with Firefox 3.6. on a Mac.

Output buffer based progress bar

I have been trying to get the following code working.
It's a progress bar trick which uses ob_get_clean() function.
Don't know why but this script just don't work!
Only the initial percent - 1% comes up and nothing after that.
<?php
error_reporting(8191);
function flush_buffers(){
#ob_end_flush();
#ob_flush();
#flush();
#ob_start();
}
$ini = 2;
echo '<script>document.getElementById(\'lpt\').style.width=\'1%\';</script><br>';
for($i=1;$i<=100;$i++) {
$k=$ini-1;
$str=str_replace("width=\'$k%\'","width=\'$i%\'",ob_get_clean());
$ini++;
echo $str;
flush_buffers();
}
?>
You can't 'retract' output text after you've sent it to the client. It merely gets appended.
It would not work as you are trying to mix server and client side code. PHP Code on the client side would not work. You will need to build the whole progress bar using javascript itself.
What #Delan says: You can't "take back" and edit output that has already been sent to the client. You would have to output a completely new <script> snippet for every movement of the percentage bar.
You can't do progress bar in PHP, you must write in eg. JavaScript, and only echo with PHP.
You can't do that because PHP is server-side language and any loading is performing on client-side because server don't loads anything then loading script you must have in client-side language.
i don't think any of these answers are complete or correct that are up-voted.
it's true that you can't retract output, but you CAN do what KPL wants to do using a simplistic non-ajax approach that doesn't even require javascript... i do this all the time. tested on ie 9, firefox, chrome and safari. perhaps it could be true that in 2010 when this was asked this technique didn't work, but i don't see why now. it's not html5 or anything fancy...
you do so using ob_flush like KPL guesses and you re-output the current bar html (however you like it to look, fancy/animated/etc) with position:absolute and an incrementing z-index:$counter. that way on each loop the output gets flushed as the php is running on the server and the new output is neatly placed over top of the previous output. sounds messy, but this technique looks perfect and works really really well on every browser i've tried.

Categories