• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hooks not working iff im using @ob_start()

#1
Hooks to minimize and compress html code is working fine, if i am not using @ob_start(), but i need to use this function to remove several problems like  ** "PHP: Cannot modify header information", die() and exit() not working...** . Using @ob_start() makes useless  to minify and compress function.
Reply

#2
Could you provide us with an example code?

Why are you bothering with compressing HTML? With modern internet you will not find it worth your while, as long as you have gzip enabled on your server for HTML.

Have you placed exit; after all your redirect(); functions? That's the only way to get rid of "Cannot modify header information"*.

If it's not your redirect() functions creating problems, you may have whitespace in your files. UTF-8 BOM etc in your files.
https://stackoverflow.com/a/8028987

* The easy way at least, as you need to stop it rendering HTML or other content that sends headers.
Reply

#3
(01-05-2020, 06:28 AM)shailesh tripathi Wrote: Hooks to minimize and compress html code is working fine, if i am not using @ob_start(), but i need to use this function to remove several problems like  ** "PHP: Cannot modify header information", die() and exit() not working...** . Using @ob_start() makes useless  to minify and compress function.

I am using following code using Hooks file-
PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function 
compress()
{
ini_set("pcre.recursion_limit""16777");
$CI =& get_instance();
$buffer $CI->output->get_output();

$re '%# Collapse whitespace everywhere but in blacklisted elements.
        (?>            # Match all whitespans other than single space.
          [^\S ]\s*    # Either one [\t\r\n\f\v] and zero or more ws,
        | \s{2,}        # or two or more consecutive-any-whitespace.
        ) # Note: The remaining regex consumes no text at all...
        (?=            # Ensure we are not in a blacklist tag.
          [^<]*+        # Either zero or more non-"<" {normal*}
          (?:          # Begin {(special normal*)*} construct
            <          # or a < starting a non-blacklist tag.
            (?!/?(?:textarea|pre|script)\b)
            [^<]*+      # more non-"<" {normal*}
          )*+          # Finish "unrolling-the-loop"
          (?:          # Begin alternation group.
            <          # Either a blacklist start tag.
            (?>textarea|pre|script)\b
          | \z          # or end of file.
          )            # End alternation group.
        )  # If we made it here, we are not in a blacklist tag.
        %Six'
;

    $new_buffer preg_replace($re" "$buffer);
$new_buffer =preg_replace('/<!--(.|\s)*?-->/'''$new_buffer);

    // We are going to check if processing has working
if ($new_buffer === null)
{
$new_buffer $buffer;
}

$CI->output->set_output($new_buffer);
$CI->output->_display();
}

/* End of file compress.php */
/* Location: ./system/application/hooks/compress.php */ 
And following code in my common file for handling requests...
PHP Code:
<?php
  
class Universal_Model extends CI_Model {

        public function __construct()
        {
                parent::__construct();
                ob_start(); //using it is best but hooks and compress not working with it, if not using it then several php functions not working like die(), exit()
        }


public function 
Sget_loginid(){
  if(isset($_SESSION['login_id'])){
   
    
return $_SESSION['login_id'];
  }
else return 
" Value is Null or empty..";
}

Everything is working fine, but using ob_start() ruins everything.... Im attaching screenshot of html output of the website pages with AND without using ob_start().        
Reply

#4
(01-05-2020, 11:30 PM)jreklund Wrote: Could you provide us with an example code?

Why are you bothering with compressing HTML? With modern internet you will not find it worth your while, as long as you have gzip enabled on your server for HTML.

Have you placed exit; after all your redirect(); functions? That's the only way to get rid of "Cannot modify header information"*.

If it's not your redirect() functions creating problems, you may have whitespace in your files. UTF-8 BOM etc in your files.
https://stackoverflow.com/a/8028987

* The easy way at least, as you need to stop it rendering HTML or other content that sends headers.
Im using that code to remove comments and whitespaces & other stuff to reduce html rendered page code upto few lines. Code is working fine without any problem. Using ob_start() function makes is uneffective. I have updated the question by providing sample code .
Reply

#5
IF you look at your code you are loading the title twice and also jQuery twice.

I suggest that you go to w3schools and catch up on html.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
(01-06-2020, 08:47 AM)InsiteFX Wrote: IF you look at your code you are loading the title twice and also jQuery twice.

I suggest that you go to w3schools and catch up on html.
That's not the matter sir, instead of pointing out unusual mistakes, could you please help on actual problem if you can.
Reply

#7
Well it's a proven fact that any kind of errors can cause multiple other errors.

So you start by fixing your current errors that you know about then proceed with
the other errors.

And your ob_start should be place above the class under the <php
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#8
It may be that you never flush it as CodeIgniter already buffers it's output. So you are getting double buffering.
https://github.com/bcit-ci/CodeIgniter/b...r.php#L947

But my recommendation are that you ignore this minify and enable gzip compression instead.
https://github.com/bcit-ci/CodeIgniter/b...g.php#L420

If you aren't doing it on server level. (This is the recommended way!)

If you still want it to work, with that slow preg_replace function. You need to fix your redirect(); and add a working exit; as they do work. If applied correctly.
Do you have an example that dosen't work?

You aren't using a caching server I guess? That mean you will be running that replace every time a user visit that page.
Reply

#9
Lightbulb 
(01-08-2020, 11:56 AM)jreklund Wrote: It may be that you never flush it as CodeIgniter already buffers it's output. So you are getting double buffering.
https://github.com/bcit-ci/CodeIgniter/b...r.php#L947

But my recommendation are that you ignore this minify and enable gzip compression instead.
https://github.com/bcit-ci/CodeIgniter/b...g.php#L420

If you aren't doing it on server level. (This is the recommended way!)

If you still want it to work, with that slow preg_replace function. You need to fix your redirect(); and add a working exit; as they do work. If applied correctly.
Do you have an example that dosen't work?

You aren't using a caching server I guess? That mean you will be running that replace every time a user visit that page.
You are right as because Buffering by Codeigniter is probably the reason behind it.

Thanks for this suggestion, I should use gzip compression.

"That mean you will be running that replace every time a user visit that page." ---> Yes

"Do you have an example that doesn't work?" ---> I will share the code where i am getting the problem on exit() and die() when im not using ob_start() as soon as possible.
Reply

#10
(01-07-2020, 09:21 AM)InsiteFX Wrote: Well it's a proven fact that any kind of errors can cause multiple other errors.

So you start by fixing your current errors that you know about then proceed with
the other errors.

And your ob_start should be place above the class under the <php
Thanks, i have done that correction in code that you had suggested previously Smile  . I have changed the position of the ob_start to Under php tag, but still facing that problem.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.