title: HTML5 video seeking in CakePHP
body: <p>I have video streaming working just fine in CakePHP. Since the videos are private to each user, I have a CakePHP controller serve up the files if the users are authenticated. I also notice that all requests sent to the server have a <code>Cookie: CAKEPHP=&lt;stuff&gt;</code> header in their request.</p>
  
  <p>The problem is that when a user pauses, then plays the video or when the user seeks along the video, Chrome sends a Range request with a certain byte range for my server to deliver. The request gets cancelled immediately. It should be noted that this request <strong>has no</strong> <code>Cookie: CAKEPHP=&lt;stuff&gt;</code> header.</p>
  
  <p>I believe the request is denied because there is no session cookie. How can I force chrome (and possibly other browsers) to send a session cookie?</p>
  
  <p>My CakePHP Version is 2.4.1</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>view_media.ctp</p>
  
  <pre><code>&lt;?php echo $this-&gt;Html-&gt;script(array('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', 'mediaelement-and-player'), array('inline' =&gt; false));
  echo $this-&gt;Html-&gt;css(array('mediaelementplayer.min'), array('inline' =&gt; false));
  ?&gt;
  &lt;video width="320" height="240"  controls preload="none"&gt;
  &lt;?php 
      if(isset($_SERVER['HTTP_USER_AGENT'])){
          $agent = $_SERVER['HTTP_USER_AGENT'];
          if (strpos($agent,'Chrome') !== false) {
              echo '&lt;source src="/pages/get_media/264/webm" type="video/webm"&gt;';
          }
          if (strpos($agent,'Mozilla') !== false) {
              echo '&lt;source src="/pages/get_media/264/mp4" type="video/mp4"&gt;';
          }
      }
  ?&gt;
  Your browser does not support HTML5.
  &lt;/video&gt;
  </code></pre>
  
  <p>(The view_media function in the Controller is blank)</p>
  
  <p>Relevant section of PagesController</p>
  
  <pre><code>public function get_media($id, $type){
      $dbh = new PDO('mysql:host='.$dbhost;dbname=$dbname, $username, $password);
      //get media info
      $sth = $dbh-&gt;prepare("SELECT `owner`, `type` FROM media WHERE id=:id");
      $sth-&gt;bindParam(':id', $id);
      $sth-&gt;execute();
      $row = $sth-&gt;fetch();
      if($row['owner'] == $this-&gt;Auth-&gt;user('id')){
          if($row['type'] == 'png'){
              header("Content-type: image/png");
              echo  file_get_contents("/srv/Ads/Ad_".$id.".png");
          }else if($row['type'] == 'mp4'){
              if($type == "mp4"){
                  header("Content-type: video/mp4");
                  $filename = "/srv/Ads/Ad_".$id.".mp4";
              }else if($type == "webm"){
                  header("Content-type: video/webm");
                  $filename = "/srv/Ads/Ad_".$id.".webm";
              }else{
                  die;
              }
              $this-&gt;send_video($filename);
          }
          die;
      }
  }
  //thanks to http://stackoverflow.com/questions/16732419/mp4-from-php-not-playing-in-html5-video-tag
  private function send_video($path){
      if (file_exists($path)){
          $size=filesize($path);
          $fm=@fopen($path,'rb');
          if(!$fm) {
              // You can also redirect here
              header ("HTTP/1.0 404 Not Found");
              die;
          }
          $begin=0;
          $end=$size;
          if(isset($_SERVER['HTTP_RANGE'])) {
              if(preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i',   
              $_SERVER['HTTP_RANGE'],$matches)){
                  $begin=intval($matches[0]);
                  if(!empty($matches[1])) {
                      $end=intval($matches[1]);
                  }
              }
          }
          if($begin&gt;0||$end&lt;$size)
              header('HTTP/1.0 206 Partial Content');
          else
              header('HTTP/1.0 200 OK');
          header('Accept-Ranges: bytes');
          header('Content-Length:'.($end-$begin));
          header("Content-Disposition: inline;");
          header("Content-Range: bytes $begin-$end/$size");
          header("Content-Transfer-Encoding: binary\n");
          header('Connection: close');
          $cur=$begin;
          fseek($fm,$begin,0);
          while(!feof($fm)&amp;&amp;$cur&lt;$end&amp;&amp;(connection_status()==0)){
              echo fread($fm,min(1024*16,$end-$cur));
              $cur+=1024*16;
              usleep(1000);
          }
          die;
      }
  }
  </code></pre>
  
  <p>(get_media has no view)</p>
  
  <p>These are the requests. manage_media.js is blank for now, I will use it to load all media files one this video thing is working.</p>
  
  <p><a href="http://i.imgur.com/LZaXjf0.png" rel="nofollow">link</a></p>