Jump to content

ob_start before session and includes? or after?


StevenOliver

Recommended Posts

Which of these is the "best coding practice" for the proper order of "ob_start," "sessions," and "require?" (my "require" files are usually just mySQL login stuff):

a.)
<?php
ob_start("ob_gzhandler");
session_start();
require("some_required_files");
echo '<html>'; // etc. etc.

b.)
<?php
session_start();
ob_start("ob_gzhandler");
require("some_required_files");
echo '<html>'; // etc. etc.

And also, instead of just plain "session_start()" isn't it more proper to ALWAYS use  if(!isset($_SESSION)) { session_start(); }

Thank you in advance.

 

Link to comment
Share on other sites

11 hours ago, StevenOliver said:

Which of these is the "best coding practice" for the proper order of "ob_start," "sessions," and "require?" (my "require" files are usually just mySQL login stuff):

That question has the same answer as whether

$a = 1;
$b = 2;

is a "better coding practice" than

$b = 2;
$a = 1;

 

11 hours ago, StevenOliver said:

And also, instead of just plain "session_start()" isn't it more proper to ALWAYS use  if(!isset($_SESSION)) { session_start(); }

No. The second form suggests that you don't know whether the session has been started yet, and not knowing what your code is doing is not "proper".

Link to comment
Share on other sites

I think it might be good to really read about the functions you are utilizing.  The ob_* functions, like their name are there to buffer output which I'm sure you know.

It's clear you are wanting to do this to gzip output, so why not use ob_gzhandler()?

Better yet, let Apache use mod_deflate to do this for you?  Tasking PHP to do the compression is less efficient than having the webserver do it, and again depending on your web server stack, is going to increase the memory footprint of your individual php processes.  Delivering the html is really the job of the webserver, not php.

I would always start_session() first.

Link to comment
Share on other sites

Requinix, I applaud your "not knowing what your code is doing is not 'proper" -- good point, and very witty! I'm going to hit up the manual this afternoon.

Gizmola, you said  'why not use ob_gzhander()," but I thought that's what I was doing with the "ob_start("ob_gzhandler");" line of code? I will definitely read up on your suggestion about mod_deflate... Does it make a difference if mod_deflate is done in Apache vs .htaccess?

Link to comment
Share on other sites

Yes the code is equivalent, but as ob_gzhandler() is a simplified shortcut, that would be preferred by just about anyone.

Yes you can specify mod_deflate in an htaccess. 

<IfModule mod_deflate.c>
  <FilesMatch "\\.(js|css|html|htm|php|xml)$">
    SetOutputFilter DEFLATE
  </FilesMatch>
</IfModule>

There are various analysis sites you can use to examine the output from your server like https://webpagetest.org/ 

Link to comment
Share on other sites

gizmola, thank you! I didn't realize I could just type it without the ob_start prefix. I'll try it just like this:
<?php
ob_gzhandler();
// etc. etc.

But I'll try the ifModule mod_deflate in my .htaccess. The first and second lines of my .htaccess are:
Options -Indexes
RewriteEngine On

Does it matter where the <IfModule mod_deflate goes? I would think it wouldn't matter, but with PHP I'm wrong 99% of the time so that means it does matter :-)

 

Link to comment
Share on other sites

I assume from these questions that you are on shared hosting?  Since you want this processing to take place for any requests that might be coming in, I would place it at the top of your htaccess and see if it works or not. You don't want to manually code in ob_gzhandler() calls for your pages if you can have apache take care of it for you.

Ideally, you just want the configuration to be serverwide or at very least vhost wide, but perhaps that isn't possible for you, so in that case a htaccess statement at the root of your site would be preferable.

 

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.