mysql - PHP session, why is session_start() required multiple times? -
i writing web application saves posted data session in 1 page, redirects page utilize created session information. after read proper way process data , display data separate them 2 different scripts not run redundant $_post data issue. is, not keep $_posting same data server every page refresh.
i have view page, index.php, , data processing page, setdate.php. when viewing index.php, user can choose set $_post['month'] , $_post['year'] variables via input form, , submit them setdate assign $_session['desmonth'] , $_session['desyear'] respectively.
it wasn't until added second (imo redundant) session_start(); declaration on setdate.php code started work way wanted to. without it, if index.php ignoring setdate.php's $_session[*] modifications completely.
why have define redundant session_start(); if started session (and received phpsessid cookie) on initial index.php $_session[*] data being used?
here working code snippets:
setdate.php
<?php require_once 'jan.php'; session_start(); //get requested month , years view (iterative). if(isset($_post['nextmonth']) && filter_var($_post['nextmonth'], filter_sanitize_number_int)) { //this filter allows +- , 0-9 $_session['desmonth'] += sanitizeinput($_post['nextmonth']); if($_session['desmonth'] > 12) { $_session['desmonth'] = $_session['desmonth']-12; $_session['desyear'] += 1; } else if($_session['desmonth'] < 1) { $_session['desmonth'] = 12; $_session['desyear'] -= 1; } } //get explicit month , years view. if(isset($_post['month']) && filter_var($_post['month'], filter_sanitize_number_int)) { $_session['desmonth'] = sanitizeinput($_post['month']); echo "set month"; } if(isset($_post['year']) && filter_var($_post['year'], filter_sanitize_number_int)) { $_session['desyear'] = sanitizeinput($_post['year']); echo "set year"; } echo $_session['desmonth']; echo $_session['desyear']; header("location: /"); die(); ?> truncated index.php
<?php session_start(); require_once 'cellupdater.php'; $timefordateuse = mktime(1,1,10,$_session['desmonth'],1,$_session['desyear']); //this line used various formatting below. ... without session_start(); declaration in setdate.php $_session[*] data not preserved. why?
edit: question answered, editing imaginary internet points
from php.net:
session_start() creates session or resumes current 1 based on session identifier passed via or post request, or passed via cookie.
when session_start() called or when session auto starts, php call open , read session save handlers.
in other words, session_start() not create session when session not exists yet, makes possible script access current session. gives read and write access $_session variable.
without session_start, script cannot write or read session, session still there cannot read or modified script. if want give read access session can call session_write_close(); close write access. can handy when want multiple files open same session @ same time. when script has write access blocks current session file, blocking other scripts want write access same session.
if lazy , want session active, can write
php_flag session.auto_start 1 in .htaccess file enable auto start of session in php.
Comments
Post a Comment