title: Storing PHP variable with session (or cookies)?
body: <p>I've been looking into using a variable to let visitors choose if embedded Youtube videos autoplay or not. I've been thinking about using a link like <code>myurl.com/?autoplay=0</code> to stop autoplay and <code>1</code> to enable it again. I read a bit about sessions (don't know if that's the best practice here though since I'm new to PHP), but couldn't quite get it to work after loading a second video. If anyone could point out my error(s) and help me, <em>or suggest a better way to solve it</em>, I'd be most grateful.</p>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  $_SESSION['autoplay']=0
  ?&gt;
  
  &lt;!-- content here removed for readability --&gt;
          &lt;button class="randomizerButton" data-href="data.php"&gt;Randomize&lt;/button&gt;
          &lt;hr&gt;
  
          &lt;div id="results"&gt;
          &lt;?php include('data.php'); ?&gt;
          &lt;/div&gt;
  
          &lt;script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  
          &lt;script type="text/javascript"&gt;
              $(document).ready(function(){
                  $('button.randomizerButton').click(function(){
                      scriptUrl = $(this).attr('data-href');
                      $.post(scriptUrl, function(response){
                          $('#results').html(response);
                      });
                  });
              });
          &lt;/script&gt;
  </code></pre>
  
  <p><strong>data.php</strong></p>
  
  <pre><code>if ($_SESSION['autoplay'] == "0"){
      $autoplayVar = 0;
  }
  else{
      $autoplayVar = 1;
      }
  
  echo ($autoplayVar);
  
  echo ('&lt;br&gt;');
  
  
  echo('&lt;iframe id="ytplayer" width="557" height="315" src="http://www.youtube.com/v/0wLljngvrpw&amp;autoplay='.$autoplayVar.'" frameborder="0"&gt;&lt;/iframe&gt;');
  </code></pre>