What(s) problems will I have with using ob_start() and ob_end_flush()?

Asked

Viewed 386 times

2

My application is a website for a real estate company and I’m having problems with a feature developed with Session similar to a shopping cart to add more than one property in a single proposal. The problem that this happening, is that when selected a property is created the session however, there are times that the property appears in the order list and there are times that no, and there are times that only appears after a certain time(delay).

Can this issue be related to ob_start() and end_flush()? Can anyone give me any hints as to what might be going on?

My application this way:

<?php
    ob_start();
    session_start();
    require('includes/paginator.class.php');
    require('includes/cookie.class.php');  
    require('config/get.php');
    require('config/config.php');
    require_once ('includes/recaptchalib.php');
    require_once('classes/Conexao.php');
    require_once('classes/Carrinho.php');

    <conteúdo estatico head>

<conteúdo estatico header>

    <conteúdo dinamico com includes das páginas(contato/institucional/pedido/etc)>

    <conteúdo estático footer>
    end_flush();
    ?>
  • When you use ob_start(); and end_flush(); you are saying that the HTML content will be printed between these two elements, you should not put the class requires, connection, and config etc. within these rules, nor the session_start();.

  • If I understand correctly, all ob_start(); and session_start(); should it be after includes and require? Usually people say they should stay ahead of everything.

2 answers

2


When you use ob_start(); and end_flush(); You’re carrying a buffer of content. I do not recommend that you place class, connection, and config requests within these functions, for this you can use the session_start(); or setcookie().

In your case, I believe you could solve your problem by doing something like:

<?php
session_start();
require('includes/paginator.class.php');
require('includes/cookie.class.php');  
require('config/get.php');
require('config/config.php');
require_once('includes/recaptchalib.php');
require_once('classes/Conexao.php');
require_once('classes/Carrinho.php');

if (session_id() == '') {
   echo "A sessão expirou!";
   die();
}

echo getTemplate('arquivo_externo');

  function getTemplate($file) {
      ob_start();
      include $file.'.php';
      $template = ob_get_contents(); 
      ob_end_clean();
      return $template;
      end_flush();
  }
  • Thank you Ivan, I’ll give you a test with the example of your answer.

  • Here’s an improved example of that: http://stackoverflow.com/questions/2061032/php-file-get-contents-with-php-intact

  • Ivan, if I have started with Sesssion in the main file (index.php), and then set other Sesssion in secondary files like on the included pages, I may have trouble repeating session_start() several times?

  • Actually you have to set the session_start() at the top of your main config file which will handle the other pages that are logged in. No need to put this on all includes, just put on the first include that will be loaded on all pages you need to be logged in.

1

My problem wasn’t exactly with ob_start(), but rather the server cache. I am hosting on the kinghost with the Varnish cache server active which totally bugged my application, for the simple fact that this server is storing the sessions and cookies and not on the user’s machine, after disabling the cache server everything went back to normal.

Thanks for the answers. Problem solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.