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
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
The PHP manual has a decent introduction to what sessions are and how to use them. It includes this paragraph:
So setting that option via the Apache configuration file (
.htacess
) is instead of runningsession_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 tosession_start()
easier to follow when reading your code, than a "magic" setting in the server configuration.