0

I have the following setup:

  • index.php
  • subpage.php

When visiting either pages I have a "header intro animation". I only want the user to see this animation upon first page visit and not repeat it when refreshing/visiting other subpages within the same session.

I've tried doing the following:

$disableAnimationOnOtherPages;

 if(session_id() === '')
 {
    // session has NOT been started
    session_start();
    echo "SESSION WAS NOT SET";
 }
 else
 {
    // session has been started
    echo "SESSION HAS BEEN SET";
    $disableAnimationOnOtherPages = true;
 }

So when I visit any page the first time I set/start the session and then if I refresh or go to a subpage, then $disableAnimationOnOtherPages = true; will be set, so that I can use it as a reference to disable my javascript animation in my included .js file further down.

But regardless of what I do, im only getting "SESSION WAS NOT STARTED".

Any ideas on what im doing wrong in this context?

user1231561
  • 3,239
  • 6
  • 36
  • 55

2 Answers2

1

session_start(); doesn't mean 'create new session', it means create or continue the previously started session, you can then use $_SESSION to store values in.

So what you really want to do 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 then use $_SESSION['disableAnimation'] to disable your animations instead of a global variable, as globals are generally frowned upon.

Edit:

To the access this in JavaScript, you would need something like:

<script type="text/javascript">
    var disableAnimation = <?=$_SESSION['disableAnimation'];?>;
    // do Javascript stuff with disableAnimation
</script>
MatthewMcGovern
  • 3,466
  • 1
  • 19
  • 19
  • Hi MAtthew - thanks a bunch for the suggestion, I can see you very valid point about session_start();. Ill just try out your approach. Would you by any means know how i'd reference and use " $_SESSION['disableAnimation']" in my .js file? – user1231561 May 03 '13 at 13:26
  • I've updated my answer to show how to access it in Javascript. You have to echo it out from PHP and into a Javascript variable. – MatthewMcGovern May 03 '13 at 13:28
  • Kodus Matthew, this works exactly like I need it to. Thanks for the swift reply! Im noticing that when I visit the first time =$_SESSION['disableAnimation'];?>; doesnt print anything, but only: "var disableAnimation =" whereas when its set it will print "1" - is that correct behaviour? – user1231561 May 03 '13 at 13:37
  • Hmm I would hope it'd output "0" for false. Try changing the PHP code I suggested to say `= 0` instead of false and `= 1` instead of true? – MatthewMcGovern May 03 '13 at 13:39
  • yea also figured it would output "0" . Changed the PHP to 0 and 1 - and now I see "0" outputting in the first seession visit - so that is all good for me – user1231561 May 03 '13 at 13:42
0

try removing the $disableAnimationOnOtherPages that is at the top of the script? I've had similar issues where it only activates the first instance and it seems to be an issue with the order that a function is called.

OR (I've done this lately too)

Have the first conditional be the exception?

if(session_id !='')
{
//session has been started
echo "SESSION HAS BEEN SET";
$disableAnimationOnOtherPages = true;
} else {
//session has NOT been started
echo "SESSION HAS NOT BEEN SET";
session_start();
}

this is totally untested, just a top of my head idea to try. (I've been up against similar issues and have been playing around with both of these ideas, sometimes they work and other times the code laughs at me yelling "SUCKER")

S.Taylor
  • 49
  • 2