Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/98095-is-this-considered-poor-coding/
Share on other sites

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.

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

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 ?

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

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
}

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.

 

 

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.