• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

PHP session_start AND command pattern

Wingy

[H]ard|Gawd
Joined
Dec 18, 2000
Messages
1,294
I'm wiriting up some PHP using a command pattern type of design. Here's my directory setup:

Code:
/include/command/CommandRouter.php     //  Routes command to proper command object
/include/command/UserLogin.php               //  Class implementing ICommand
/include/command/ICommand.php              //  Command object interface

/admin/index.php         //  admin login form
/admin/main.php          //  admin dashboad from successful admin login

/index.php        //  Normal user login
/command.php  //  Makes a commandRouter, gets the command response, and forwards to result page

I'm running into a problem with session_start and how it only works within a directory. Here's a scenerio.

I'm and admin, I go to www.mysite.com/admin and enter my login credentials.
The form action points to ../command.php?commandName=UserLogin, which calls a UserLogin method to login. This is where user data is stored in the session.

I logged in alright, except because of the session_start issue, when /admin/main.php looks for $_SESSION["Logged_In"], it's empty.

My question is this, how do you all do you session_start stuff and still keep a somewhat complex but neat directory structure, while maintaining some kind of command type pattern as I am?

Thanks
 
You have to make sure that you call the
PHP:
session_start()
function before you try to access any session variables. Otherwise, as far as PHP is concerned, the session variable doesn't exist. You can put the function call in the command.php file at the top, if you are using the command.php as a controller for your framework.
 
Last edited:
You have to make sure that you call the
PHP:
session_start()
function before you try to access any session variables.

With the older (3.x), you had to call session_start() before everything else. I don't know if that's that same with 4 or 5 as I mostly work inside of a CMS anymore, and the CMS takes care of that automatically.
 
With the older (3.x), you had to call session_start() before everything else. I don't know if that's that same with 4 or 5 as I mostly work inside of a CMS anymore, and the CMS takes care of that automatically.

taken from php.net
If you want to use a named session, you must call session_name() before calling session_start().
 
Back
Top