...

View Full Version : Need really help with this code



viyanali
01-09-2006, 07:05 AM
hi @ all
Need help with php script..its a download script
when i use it it cant download any files couse the script cant get the download file size.
I tryed really lot things.
I am thinking that the problem is
on "$header = getallheaders();"
when i remove that i can download with a download manager
but when i try to start the download without a downoad manager its just waiting...
anyway here is the code..hope someone can help me:
as far readed from php pages the problem is that the php module is not installed at my host..
is there another way to let this code work..? or maybe something like this?



<?php
require './functions.php';
register_shutdown_function('bye_bye');
ReadConfig ('DEVINFO, MAIN, WHITELIST, BLACKLIST, REFLIST');
header ("Expires: Tue, 1 Jan 1980 00:00:00 GMT");
header ("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header ("Cache-Control: no-store, no-cache, must-revalidate");
header ("Cache-Control: post-check=0, pre-check=0", false);
header ("Pragma: no-cache");
header ("X-Powered-By: Antileech");
$IP = GetIP();
$whitelisted = WhiteList($IP);
if (!$whitelisted && BlackList($IP)) {
Message ('You were added to blacklist. Go **** yourself...', $MAIN['url']);
}
if (!$whitelisted && !CheckReferer()) {
Message ('This page is only avaliable from the '.$MAIN['url'].' site, where you\'re being redirected...', $MAIN['url']);
}
mysql_connect($MAIN['db_address'], $MAIN['db_username'], $MAIN['db_password']);
mysql_select_db($MAIN['db_database']);

ClearTimeouted();

$return = GetParams();
if (sizeof($return) != 2) {
Message ('Wrong query!');
}
$session = $return[0];

if (!empty($MAIN['maxtotalthreads'])) {
$Query = "SELECT SUM(threads) AS `total` FROM ".$MAIN['db_table'];
$Res = mysql_query($Query);
list($total) = mysql_fetch_array($Res);
if ($total >= $MAIN['maxtotalthreads']) {
Message ('Unfortunately, the server load is too high at the moment. This server can handle only '.$MAIN['maxtotalthreads'] .' threads simultaneous. Please, try again later.');
}
}

$Query = "SELECT SUM(threads) FROM ".$MAIN['db_table']." WHERE ip = '".$IP."'";
$Result = mysql_query($Query);
if (mysql_num_rows($Result)) {
list($threads) = mysql_fetch_array($Result);
if ($threads >= $MAIN['maxthreadspersession']) {
Message ('You cannot download more than '.$MAIN['maxthreadspersession'].' threads at the same time!', $MAIN['url']);
}
}

$Query = "SELECT * FROM ".$MAIN['db_table']." WHERE session = '".addslashes($session)."' AND ip = '".$IP."'";
$Result = mysql_query($Query);
if (!mysql_num_rows($Result)) {
Message ('Your session expired or you specified wrong session name.', $MAIN['url']);
}
$info = mysql_fetch_assoc($Result);

$Query = "UPDATE ".$MAIN['db_table']." SET threads = threads + 1 WHERE session = '".addslashes($session)."'";
$Result = mysql_query($Query);
$is_increased = true;

$fullpath = $MAIN['filepath'].$info['category'].'/'.$info['file'];
$size = filesize($fullpath);

$fp = fopen($fullpath, 'r');
header ('Accept-Ranges: bytes');
header ('Content-Type: application/force-download');
header ('Connection: close');

$header = getallheaders();
if (isset($header['Range'])) {
header('HTTP/1.0 206 Partial Content');
$range = $header['Range'];
$r = explode('=', $range);
list($from) = explode('-', $r[1]);
$length = $size - $from;
$s_start = $size - $length;
$s_end = $size - 1;
fseek($fp, $s_start);
header ('Content-Range: bytes '.$s_start.'-'.$s_end.'/'.$size);
header ('Content-Length: '.$length);
} else {
header('Content-Length: '. $size);
}
set_time_limit(0);
ob_implicit_flush(true);
if (empty($MAIN['maxkbps'])) {
while (!feof($fp)) {
print (fread($fp, 10240));
if (connection_aborted()) {
break;
}
}
} else {
$koef = 10;
$readlen = intval(1024 * $MAIN['maxkbps'] / $koef);
$waittime = intval(1000000 / $koef);
while (!feof($fp)) {
if (connection_aborted()) {
break;
}
print (fread($fp, $readlen));
usleep($waittime);
}
}
fclose($fp);
?>

marek_mar
01-09-2006, 09:39 AM
Do you use that Content-Length header?

viyanali
01-09-2006, 09:57 AM
the script uses the getallheaders function..
but this only useable when php is installed as module on apache
wich is not at my host.
so i need something else instead of this thats the scripts works with my site..

marek_mar
01-09-2006, 11:42 AM
My previous post was about this:

when i use it it cant download any files couse the script cant get the download file size.
You can add:


header('Content-Length:' . filesize($file));

BTW getallheaders() lists sent headers and doesn't set any.

viyanali
01-09-2006, 12:30 PM
tryed to add...dont work
is there not a code wich i can use instead of "getallheaders"
but a code wich dont need the php as module for apache

Spookster
01-09-2006, 01:21 PM
And we need really help with you to read our posting guidelines
http://www.codingforums.com/postguide.htm

marek_mar
01-09-2006, 03:45 PM
You can write your own functions/class to replace getallheaders() if it doesn't work for you or use headers_list() (http://www.php.net/headers_list) (PHP5) or write code without the need of checking what headers are sent.

viyanali
01-09-2006, 07:17 PM
can u give me a example how u mean "u own functions/class"
dont be really good at such things...
when i remove headers the download is only working with use of a download manager... but i cant say to all boardmembers (have a board and need this for that) go and install all download managers..:(

marek_mar
01-09-2006, 07:37 PM
You could use a class like this (something very simple):


class header
{
var $headers = array();

function send($header)
{
$this->headers[] = $header;
return header($header);
}

function getallheaders()
{
return $this->headers;
}
}
$header = new header;
$header->send('Content-Type: text/css');
// ...
var_dump($header->getallheaders());

viyanali
01-09-2006, 08:10 PM
should i modify the code like this?
if wrong can u show me on this code?



<?php
require './functions.php';
register_shutdown_function('bye_bye');
ReadConfig ('DEVINFO, MAIN, WHITELIST, BLACKLIST, REFLIST');
header ("Expires: Tue, 1 Jan 1980 00:00:00 GMT");
header ("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header ("Cache-Control: no-store, no-cache, must-revalidate");
header ("Cache-Control: post-check=0, pre-check=0", false);
header ("Pragma: no-cache");
header ("X-Powered-By: Antileech");
$IP = GetIP();
$whitelisted = WhiteList($IP);
if (!$whitelisted && BlackList($IP)) {
Message ('You were added to blacklist. Go **** yourself...', $MAIN['url']);
}
if (!$whitelisted && !CheckReferer()) {
Message ('This page is only avaliable from the '.$MAIN['url'].' site, where you\'re being redirected...', $MAIN['url']);
}
mysql_connect($MAIN['db_address'], $MAIN['db_username'], $MAIN['db_password']);
mysql_select_db($MAIN['db_database']);

ClearTimeouted();

$return = GetParams();
if (sizeof($return) != 2) {
Message ('Wrong query!');
}
$session = $return[0];

if (!empty($MAIN['maxtotalthreads'])) {
$Query = "SELECT SUM(threads) AS `total` FROM ".$MAIN['db_table'];
$Res = mysql_query($Query);
list($total) = mysql_fetch_array($Res);
if ($total >= $MAIN['maxtotalthreads']) {
Message ('Unfortunately, the server load is too high at the moment. This server can handle only '.$MAIN['maxtotalthreads'] .' threads simultaneous. Please, try again later.');
}
}

$Query = "SELECT SUM(threads) FROM ".$MAIN['db_table']." WHERE ip = '".$IP."'";
$Result = mysql_query($Query);
if (mysql_num_rows($Result)) {
list($threads) = mysql_fetch_array($Result);
if ($threads >= $MAIN['maxthreadspersession']) {
Message ('You cannot download more than '.$MAIN['maxthreadspersession'].' threads at the same time!', $MAIN['url']);
}
}

$Query = "SELECT * FROM ".$MAIN['db_table']." WHERE session = '".addslashes($session)."' AND ip = '".$IP."'";
$Result = mysql_query($Query);
if (!mysql_num_rows($Result)) {
Message ('Your session expired or you specified wrong session name.', $MAIN['url']);
}
$info = mysql_fetch_assoc($Result);

$Query = "UPDATE ".$MAIN['db_table']." SET threads = threads + 1 WHERE session = '".addslashes($session)."'";
$Result = mysql_query($Query);
$is_increased = true;

$fullpath = $MAIN['filepath'].$info['category'].'/'.$info['file'];
$size = filesize($fullpath);

class header
{
var $headers = array();

function send($header)
{
$this->headers[] = $header;
return header($header);
}

function getallheaders()
{
return $this->headers;
}
}
$header = new header;
$header->send('Content-Type: text/css');
// ...
var_dump($header->getallheaders());

viyanali
01-09-2006, 08:12 PM
its not working like i tryed.sry i know i am noob in such things..:(

marek_mar
01-09-2006, 08:13 PM
You should add the "$header = new header;" line before any headers and change all "header(" function calls into
"$header->send(".
the


$header->send('Content-Type: text/css');
// ...
var_dump($header->getallheaders());

Was just an example. You don't have to use it anywhere.

viyanali
01-11-2006, 04:55 PM
really thx for your help but i couldt get it work...
maybe i making something false but now i am trying since 3 days to get this work on my server...:(



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum