EchoFool Posted March 27, 2008 Share Posted March 27, 2008 I have a global include on my site which checks for which page the user views, and have a pretty large if statement on the go so far which checks quite alot of pages and if the user is on such a certain page then it will do queries and checks for that relevant page. As some pages use the same checks, i used a global include rather just putting it on every script i make. But is putting a huge if statement the best idea? This is generally what I have,,, was wondering if there if there was a more efficient way at all, i am hoping there is not as then i won't have to change it but this is the idea: Try and imagine that there was like another 40 odd more php files to check, i just put 4 for this one to keep the example small to read. <?php If(strtolower($_SERVER['PHP_SELF']) == '/publicforum.php' OR strtolower($_SERVER['PHP_SELF']) == '/login.php' OR strtolower($_SERVER['PHP_SELF']) == '/register.php' OR strtolower($_SERVER['PHP_SELF']) == '/pickyourcity.php'){ If(strtolower($_SERVER['PHP_SELF']) == '/register.php' OR strtolower($_SERVER['PHP_SELF']) == '/pickyourcity.php'){ include("registerinclude.php"); }Else{ session_start(); //code stuff } } ?> Is there a slightly more efficient way than this? Perhaps some kinda case or possibly some kind of function ? I don't know all the possibilities in php there are so many! Hope I am doing the best method! Hope you can share some advice. Quote Link to comment https://forums.phpfreaks.com/topic/98095-is-this-considered-poor-coding/ Share on other sites More sharing options...
cooldude832 Posted March 27, 2008 Share Posted March 27, 2008 well first off there is very little harm in session_start(); site wide, they aren't just for login scripts. in my config/constants file I wrote this <?php define("SESSION_AUTOSTART_ALL", "1"); if(SESSION_AUTOSTART_ALL == "1"){ session_start(); } ?> and basically it mimics the session_auto_start ON config. You can always turn session_auto_start on, but some times it creates funky stuff. Quote Link to comment https://forums.phpfreaks.com/topic/98095-is-this-considered-poor-coding/#findComment-501863 Share on other sites More sharing options...
EchoFool Posted March 27, 2008 Author Share Posted March 27, 2008 Won't it add to server resources to put session_start() when you don't use it? Like cos im a bit obsessive with making scripts as efficient as possible .... Quote Link to comment https://forums.phpfreaks.com/topic/98095-is-this-considered-poor-coding/#findComment-501865 Share on other sites More sharing options...
kenrbnsn Posted March 27, 2008 Share Posted March 27, 2008 You could use a switch statement: <?php switch (strtolower($_SERVER['PHP_SELF'])) { case '/publicforum.php': case '/login.php': case '/register.php': case '/pickyourcity.php': include("registerinclude.php"); break; default: session_start(); //code stuff }?> Or you could use an array: <?php $tst = array('/publicforum.php','/login.php','/register.php','/pickyourcity.php'); if (in_array(strtolower($_SERVER['PHP_SELF']),$tst) include("registerinclude.php"); else { start_session(); // // code // }?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/98095-is-this-considered-poor-coding/#findComment-501866 Share on other sites More sharing options...
EchoFool Posted March 27, 2008 Author Share Posted March 27, 2008 You could use a switch statement: <?php switch (strtolower($_SERVER['PHP_SELF'])) { case '/publicforum.php': case '/login.php': case '/register.php': case '/pickyourcity.php': include("registerinclude.php"); break; default: session_start(); //code stuff }?> Or you could use an array: <?php $tst = array('/publicforum.php','/login.php','/register.php','/pickyourcity.php'); if (in_array(strtolower($_SERVER['PHP_SELF']),$tst) include("registerinclude.php"); else { start_session(); // // code // }?> Ken which one would you pick personally? Or which one is considered more efficient ? Quote Link to comment https://forums.phpfreaks.com/topic/98095-is-this-considered-poor-coding/#findComment-501868 Share on other sites More sharing options...
cooldude832 Posted March 27, 2008 Share Posted March 27, 2008 probably the best idea is if you have well established custom CMS system to carry across system variables in MySQL such as (Session/no Session) and then when a user loads a page if its row has the session bool turn to 1 then session_start(); as for the more load the only additional load comes on the inital calling of session_start() for a user "session" to establish a new server side cookie of it. The recalling of session_start() only will renew the sessions being based via GET/POST/cookies which we are taking nano seconds to do. if 90% of your users will need a session to navigate your site then put it across the site to save you and them headaches beacuse if they navigate to a non sessioned page they might have to relogin when they go back across Quote Link to comment https://forums.phpfreaks.com/topic/98095-is-this-considered-poor-coding/#findComment-501871 Share on other sites More sharing options...
kenrbnsn Posted March 27, 2008 Share Posted March 27, 2008 I'm not an efficiency expert, so I can't tell you which is better. I've used both in my coding. Ken Quote Link to comment https://forums.phpfreaks.com/topic/98095-is-this-considered-poor-coding/#findComment-501877 Share on other sites More sharing options...
doni49 Posted March 29, 2008 Share Posted March 29, 2008 I'd probably load sessions for all pages too. But if you want to keep something similar to what you have, this should be pretty efficient. It may or may not be better than a switch statement (I honestly don't know)--just another option and I think it would be a little easier to manage. $pages = array( '/publicforum.php', '/login.php', '/register.php', '/pickyourcity.php', '/register.php', '/pickyourcity.php' ); if (in_array(strtolower($_SERVER['PHP_SELF']), $pages){ include("registerinclude.php"); }Else{ session_start(); //code stuff } Quote Link to comment https://forums.phpfreaks.com/topic/98095-is-this-considered-poor-coding/#findComment-503767 Share on other sites More sharing options...
keeB Posted March 29, 2008 Share Posted March 29, 2008 Don't know about you, but I do NOT want to manage the maintenance of adding a new page TO EVERY PAGE. Nightmare, nightmare nightmare. To answer your question: YES. To give you some guidance, I need to know what you're trying to accomplish, though. Quote Link to comment https://forums.phpfreaks.com/topic/98095-is-this-considered-poor-coding/#findComment-503772 Share on other sites More sharing options...
doni49 Posted March 29, 2008 Share Posted March 29, 2008 I'm pretty sure this code is in an include file which EVERY page in the OP's site includes. And I agree with you--NO WAY would I ever want to update every file when the the list changes. Quote Link to comment https://forums.phpfreaks.com/topic/98095-is-this-considered-poor-coding/#findComment-503775 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.