StevenOliver Posted April 20, 2020 Share Posted April 20, 2020 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 21, 2020 Share Posted April 21, 2020 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". Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 21, 2020 Share Posted April 21, 2020 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. Quote Link to comment Share on other sites More sharing options...
StevenOliver Posted April 25, 2020 Author Share Posted April 25, 2020 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? Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 25, 2020 Share Posted April 25, 2020 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/ Quote Link to comment Share on other sites More sharing options...
StevenOliver Posted April 26, 2020 Author Share Posted April 26, 2020 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 :-) Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 26, 2020 Share Posted April 26, 2020 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.