Forum Moderators: coopster

Message Too Old, No Replies

help - simple script doing massive bandwidth accesses

         

scorpion

10:15 pm on Jan 23, 2003 (gmt 0)

10+ Year Member



I have a php script embedded in my html, all it does is fopen a relatively small file, I extract a small piece of html and place it on my page. My logs showed on some days that this file did like 5 GIGabytes, this is highly unlikely, what could make such a dynamic pull script defective and cause it to suck major bandwidth on some occasions?

jatar_k

12:50 am on Jan 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The only guess I have is that the script may be doing something wrong or not efficiently.

What is the average bandwidth it is using? Are we talking the difference between 1GB and 5GB or 4.5 GB and 5GB?

scorpion

1:23 am on Jan 25, 2003 (gmt 0)

10+ Year Member



the whole site does 250mb per day, then maybe once a week or so (quite randomly) according to the technician, this PHP script is the cause of a spike to 3-4 gig, it's strange as the script is identical to others I've run..i tried an exit(0); but it shouldn't make a difference...

Clark

2:23 am on Jan 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



post the code (with url stripped out or modified)...

scorpion

5:16 am on Jan 25, 2003 (gmt 0)

10+ Year Member



<?php

// Retrieve segment of html from remote resource.

$fd = fopen ("URL", "r");

// Read in current tour page
$buffer = "";
while (!feof ($fd)) {

$current_line = fgets($fd);
$buffer = $buffer . $current_line;

// this line finds the end of the first table in the
// document, we want to return the first table found.
if (strstr($current_line, "</TABLE>")) { break; }


}

fclose ($fd);

// Find beginning of table and do some absolute path rewriting

$current_month = strpos($buffer, "<TABLE WIDTH");
$buffer = substr($buffer, $current_month);
$buffer = str_replace('HREF=', 'href=http://www.myserver.com/phpscripts/getimg.php?', $buffer);
$buffer = str_replace('#FFFFFF"', '#FFFFFF" size=-1', $buffer);
$buffer = str_replace('size="3"', 'size="-1"', $buffer);
$buffer = str_replace('COLOR="#000000"', 'size=-1 COLOR="#000000"', $buffer);
$buffer = str_replace('COLOR="#ff0000"', 'size=-1 COLOR="#ff0000"', $buffer);
$buffer = str_replace('IMG SRC="', 'IMG SRC="http://www.myserver.com/', $buffer);

echo $buffer;
exit(0);
?>

I call this file from another PHP file like this (in the body):

<?php
include 'http://www.myserver.com/fileabove.php';
?>

Clark

6:06 am on Jan 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can't figure it out by just looking at it, but I would definitely put in some debugging info in that puppy to see what's going on.

Something must have gotten pulled into a loop at some point. Either in the while statement or in all those string replaces...you are doing an img src= so maybe when you output that data something weird is going on...

Is the file that's being pulled for the small html snippet dynamically created?

Try using dummy data, and echoing out debugging info every time you perform an operation, on every line in the while statement to check if the file is doing what you expect it to do...

jmccormac

7:17 am on Jan 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




<?php
// Retrieve segment of html from remote resource.
$fd = fopen ("URL", "r");

The code above assumes that the URL exists and is readable. There is no error checking to stop it in the even that the file cannot be opened.
Something along the lines of the code below may help.

$fd= fopen("URL", "r");
if(!$fd) die ("Cannot read URL");

You could also fetch the entire file in one operation again, error handling should be included. My PHP is a bit rusty in this respect as I switched to using TCL for this kind of work a few years ago.

Regards...jmcc

Clark

9:17 am on Feb 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the relevant page:

[php.net...]

To check if a file exists using http or ftp use the following:

$fp = @fopen("http://www.someurl.com/testfile.php3","r");
if ($fp)
{ print"The file exists!"; }
else
{ print"The file does not exist"; }

Note: The "@" in front of fopen suppresses the error output of the function.