passing header data in .NET and IIS - php

I have existing code that passes information for a video file in php to the video player through .htaccess. I'm trying to understand how something like this can be replicated in IIS and .NET
php script:
$hash = $_GET['h'];
$streamname = $_GET['v'];
$timestamp = $_GET['t'];
$current = time();
$token = 'sn983pjcnhupclavsnda';
$checkhash = md5($token . '/' . $streamname . $timestamp);
if (($current - $timestamp) <= 2 && ($checkhash == $hash)) {
$fsize = filesize($streamname);
header('Content-Disposition: attachment; filename="' . $streamname . '"');
if (strrchr($streamname, '.') == '.mp4') {
header('Content-Type: video/mp4');
} else {
header('Content-Type: video/x-flv');
}
header('Content-Length: ' . $fsize);
session_cache_limiter('nocache');
header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
$file = fopen($streamname, 'rb');
print(fread($file, $fsize));
fclose($file);
exit;
} else {
header("location:.$player_id");
}
.htaccess
RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*)$ videoSecure.php?h=$1&t=$2&v=$3
RewriteRule ^$ - [F]
RewriteRule ^[^/]+\.(flv|mp4)$ - [F]
What this does is obfuscate and secure a video file from being downloaded directly off the sever, but still allowing it to be streamed.
I'm trying to understand if I can achieve something like this in IIS6/7 + .NET MVC4
Alternatively, would I be able to keep the php and have it execute through IIS despite a .NET environment for the majority of the site and would that produce any problems?

You can use some thing like this.
Response.AppendHeader("Content-Type", "video/mp4");

Related

ERR_CONTENT_LENGTH_MISMATCH when loading video in chrome

So I get this error when some of the video plays. When Chrome completely downloads the video, it stops playing with this error. Also the status of the request changes from 206 to (failed) and then Chrome sends some other requests with Range:bytes=2990775- and the server response is:
Accept-Ranges:bytes
Cache-Control:private
Connection:keep-alive
Content-Disposition:attachment; filename="About The Author.mp4"
Content-Length:0
Content-Range:bytes 2990775-2990775/2990776
Content-Transfer-Encoding:binary
Content-Type:video/mp4
Date:Tue, 14 Feb 2017 13:46:24 GMT
Last-Modified:Wed, 08 Feb 2017 05:43:27 GMT
Pragma:public
Server:Apache/2
Vary:User-Agent
X-Powered-By:PHP/5.4.45
I have another website on the same server and it works fine there.
Here is my PHP code:
$filesize = filesize($resource->path);
$matches = explode("-", substr($_SERVER['HTTP_RANGE'],6));
if (empty($matches[1]))
{
$matches[1] = $filesize - 1;
}
$offset = intval($matches[0]);
$length = intval($matches[1]) - $offset;
$file = fopen($resource->path, 'r');
fseek($file, $offset);
$data = fread($file, $length);
fclose($file);
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $filesize);
header('Pragma: public');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($resource->path)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: ' . $mime);
header('Content-Length: ' . ($length + 1));
header('Content-Disposition: attachment; filename="' . $resource->filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
print($data);
Sorry for my bad english.
I've found the problem.
I changed $data = fread($file, $length); to $data = fread($file, $length + 1);.
I don't know why the requested length should be plus one but it solve'd the problem.

Secure HTTP Streaming Plugin with FlowPlayer always says "Stream Not Found"

I think I have everything configured correctly, but I can not stream any video with flowplayer and this plugin actived.
The structure of the files is:
index.php
sectimestamp.php
secure/
--.htaccess
--video.mp4 <-- Mi video (2.4 MB)
--video.php
flowplayer/
--flowplayer-3.2.18.swf
--flowplayer.controls-3.2.16.swf
--flowplayer.securestreaming-3.2.9.swf
(All files and dirs set with 755 permission)
In index.php:
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- A minimal Flowplayer setup to get you started -->
<!--
include flowplayer JavaScript file that does
Flash embedding and provides the Flowplayer API.
-->
<!-- flowplayer depends on jQuery 1.7.1+ (for now) -->
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="flowplayer/flowplayer-3.2.13.min.js"></script>
<script>
$(document).ready(function() {
$f("player", "flowplayer/flowplayer-3.2.18.swf", {
plugins: {
secure: {
url: "flowplayer/flowplayer.securestreaming-3.2.9.swf",
timestampUrl: "sectimestamp.php"
}
},
clip: {
baseUrl: "secure",
url: "video.mp4",
autoPlay: false,
urlResolvers: "secure",
scaling: "fit",
}
});
});
</script>
</head><body>
<div id="player"></div>
</body></html>
sectimestamp.php:
<?php
echo time();
?>
video.php:
<?php
$hash = $_GET['h'];
$streamname = $_GET['v'];
$timestamp = $_GET['t'];
$current = time();
$token = 'sn983pjcnhupclavsnda';
$checkhash = md5($token . '/' . $streamname . $timestamp);
if (($current - $timestamp) <= 2 && ($checkhash == $hash)) {
$fsize = filesize($streamname);
header('Content-Disposition: attachment; filename="' . $streamname . '"');
if (strrchr($streamname, '.') == '.mp4') {
header('Content-Type: video/mp4');
} else {
header('Content-Type: video/x-flv');
}
header('Content-Length: ' . $fsize);
session_cache_limiter('nocache');
header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
$file = fopen($streamname, 'rb');
print(fread($file, $fsize));
fclose($file);
exit;
} else {
header('Location: /secure');
}
?>
.htaccess:
RewriteEngine on
RewriteBase /secure
RewriteRule ^(.*)/(.*)/(.*)$ video.php?h=$1&t=$2&v=$3
RewriteRule ^$ - [F]
RewriteRule ^[^/]+\.(flv|mp4)$ - [F]
Mod Rewrite is enabled in apache.
Server info:
PHP Version 5.6.0-1
Apache/2.4.10 (Debian)
Test URL
What is my problem? Thanks in advance.
Ps. Sorry for my bad english.
RewriteRule ^(.*)/(.*)/(.*)$ /FULLPATH/video.php?h=$1&t=$2&v=$3

export and download as csv in php not working in chrome and IE but working in firefox

I have a simple web page which pulls some data from DB and displays it in a html table and stores the data in some session variables.
I have a button "Export to csv" which calls a page which exports the results to a csv file.
The code for ExportToCsv file is:
<?php
session_start();
include "conn/sqlserverConn.php";
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
$data = $_SESSION['data'];
$rows = $_SESSION['row'];
$header = $_SESSION['header'];
download_send_headers("data_export_" . date("Y-m-d") . ".csv");
$file = fopen("php://output", 'w');
fputcsv($file,$header);
for($i=0; $i<$rows; ++$i)
{
$valuesArray=array();
foreach($data[$i] as $name => $value)
{
$valuesArray[]=$value;
}
fputcsv($file,$valuesArray);
}
fclose($file);
die();
?>
My code is working flawlessly in firefox but it is not working in chrome or IE. On chrome and IE it is showing error 404 (Object not found!). Please tell me what is the problem ?
As outlined in the blog article at: http://www.exchangecore.com/blog/php-output-array-csv-headers/, I tested the following and it worked in IE8-11, and in chrome version 34. I presume it will work in other browsers fine as well.
Headers to use:
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=' . $fileName);

pseudo streaming

I currently test script an pseudo-streaming for read mp4 files, when I reading everything works fine, but I can not move the timeline? probleme ?
my coding (mp4 metadata) is correct because when reading without this code in the player, I can move in the timeline. I use fplayer for read the mp4.
// ----- NO CACHE -----
session_cache_limiter('nocache');
// General header for no caching
$now = gmdate('D, d M Y H:i:s') . ' GMT';
header('Expires: ' . $now); // rfc2616 - Section 14.21
header('Last-Modified: ' . $now);
header('Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
header('Pragma: no-cache'); // HTTP/1.0
// ----- Seek position -----
$seekat = 0;
if (isset($_GET["pos"])) {
$position = $_GET["pos"];
if (is_numeric ($position)) {
$seekat = $position;
}
if ($seekat < 0) $seekat = 0;
}
$filename = 'test.mp4';
$ext = strrchr($filename, ".");
$prefix = "";
$file = $prefix . $filename;
if (($filename != "") && (file_exists($file)) && ($ext==".mp4")) {
header("Content-Type: video/x-mp4");
if ($seekat > 0) header('Content-Length: ' . (filesize($file)-$seekat));
else header('Content-Length: ' . filesize($file));
if ($seekat != 0) {
print("FLV");
print(pack('C', 1 ));
print(pack('C', 1 ));
print(pack('N', 9 ));
print(pack('N', 9 ));
}
$fh = fopen($file, "rb");
fseek($fh, $seekat);
while (!feof($fh)) {
print (fread($fh, 16384));
// print (fread($fh, filesize($file)));
}
fclose($fh);
}
Can you help me thank you.
header("Content-Type: video/x-mp4");
i currently use the same code but even when i stream an mp4 i let the header content as for a flv
Content-Type: video/x-flv
hope it's will help

Force download working, but showing invalid when trying to open locally

I wrote this function and everything works well till i try to open the downloaded copy and it shows that the file is invalid. Here is my function
function download_file() {
//Check for download request:
if(isset($_GET['file'])) {
//Make sure there is a file before doing anything
if(is_file($this->path . basename($_GET['file']))) {
//Below required for IE:
if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}
//Set Headers:
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->path . basename($_GET['file'])) . ' GMT');
header('Content-Type: application/force-download');
header('Content-Disposition: inline; filename="' . basename($_GET['file']) . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($this->path . basename($_GET['file'])));
header('Connection: close');
readfile($this->path . basename($_GET['file']));
exit();
}
}
}
EDIT: By invalid for example I download a picture and try to view it in iPhotos or Windows Picture Viewer and it says, File Format Unsupported. When I view it on the server it looks fine but after download it is corrupt.
Thanks Gumbo, tried that and it outputted:
Warning: gmdate() expects
parameter 2 to be long, string given
in C:\Program Files\Wamp
Server\www\TutToasterUpload\PHPClass.php
on line 83 lets see what
happens
Fixed this line:
//Added filemtime();
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($this->path . basename($_GET['file']))) . ' GMT');

Categories