php - Check if session has been started and set global var for javascript to use -
i have following setup:
- index.php
- subpage.php
when visiting either pages have "header intro animation". want user see animation upon first page visit , not repeat when refreshing/visiting other subpages within same session.
i've tried doing following:
$disableanimationonotherpages; if(session_id() === '') { // session has not been started session_start(); echo "session not set"; } else { // session has been started echo "session has been set"; $disableanimationonotherpages = true; } so when visit page first time set/start session , if refresh or go subpage, $disableanimationonotherpages = true; set, can use reference disable javascript animation in included .js file further down.
but regardless of do, im getting "session not started".
any ideas on im doing wrong in context?
session_start(); doesn't mean 'create new session', means create or continue started session, can use $_session store values in.
so want is:
session_start(); if(!isset($_session['disableanimation'])) { // session wasn't started, show animations. $_session['disableanimation'] = false; } else { // session has been started previously, disable animations. $_session['disableanimation'] = true; } you can use $_session['disableanimation'] disable animations instead of global variable, globals frowned upon.
edit:
to access in javascript, need like:
<script type="text/javascript"> var disableanimation = <?=$_session['disableanimation'];?>; // javascript stuff disableanimation </script>
Comments
Post a Comment