skip to Main Content

I’m a newbie to .htaccess and came across a a line that I think needs to be in the htaccess file. That lines reads "php_value session.auto_start 1." If I understand correctly this is all I need to do for all the files in the directory where htaccess is located. Currently every files contains the code below.

     <?php  
     session_start(); 
     $admin_id=   $_SESSION["admin_id"];
     $administrator =   $_SESSION["administrator"];
     $role=   $_SESSION["role"];
     $sub_role=   $_SESSION["sub_role"];
     if($role == 'webmaster') {$role ='admin';}
     ?>

Do I need the session_start() in those files? Do I have a correct understanding?

2

Answers


  1. Create or open a .htaccess file in the root of your directory where you want this setting to apply.

    Add the following line to the .htaccess file.

    php_value session.auto_start 1

    Login or Signup to reply.
  2. The PHP manual has a decent introduction to what sessions are and how to use them. It includes this paragraph:

    Sessions can be started manually using the session_start() function. If the session.auto_start directive is set to 1, a session will automatically start on request startup.

    So setting that option via the Apache configuration file (.htacess) is instead of running session_start on each request.

    However, I would note that as your application grows, you will probably find a lot of "boilerplate" gets added to the top of each file anyway. A common approach is to put this in a file called something like "startup.php", and use a require_once statement to load it at the top of each page. At that point, you may find an explicit call to session_start() easier to follow when reading your code, than a "magic" setting in the server configuration.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search