Page 1 of 2

Whats wrong in this???

Posted: Tue Feb 04, 2003 6:25 am
by Nik
<?php
ob_start();
error_reporting(E_ALL); $i=0;
echo "<body bgproperties='fixed' background='pics/anemos.bmp'>";

$stream = fopen("e:\\web\\data.txt", "r");

while(($x=fgetc($string))!=EOF)
{
echo "<font face='Com' size='7' color='Yellow'>" .$x;
header("Refresh: 2; URL=http://localhost/test.php");
}

ob_end_flush();
?>

I wanted after each letter thet would be readen from the file to refresh ti after 2 secs and the display the next so it would be lik1

h refresh
he refresh
hel refresh
hell refresh
hello refresh

Can it be done and how? I also i think that i have to save the fgetc position after each refresh so that it would not start from the beggining!

right?
:)

Posted: Tue Feb 04, 2003 9:14 am
by volka
you're advising the browser to re-request the document each 2 seconds.
Sending such a header does not delay further execution of the script (it's not waiting for 2 seconds). Also read viewtopic.php?t=1157
For each request your script will start over again, not continuing at the last position.
Probably it's best to let a client-side langauge (like javascript) perform that task. E.g. take a look at http://emuseum.mnsu.edu/information/gif ... t/marquee/

Posted: Tue Feb 04, 2003 10:01 am
by Nik
The refresh works ok! The problem as u noticed is in the thatthe script start all over the beginning which means that i dosen remember where it left! i think that i must save somewhere where the fgetc last accesed!

i also wnted to ask if thsi can be done with $file_array=file("data.txt");

Posted: Tue Feb 04, 2003 10:33 am
by mydimension
what volka is trying to say is that what you're trying to do won't work. to achive this kind of result you'd be better off using javascript.

Posted: Tue Feb 04, 2003 10:46 am
by Nik
i dont want to use javascipt! i already done it eith php
but i want to do it in a more simple way from this one:

<?php

$file_name = "data.txt";
$file_size = filesize($file_name);

$file_offset = isset($_GET['file_offset']) ? $_GET['file_offset'] : 0;
$file_offset = min( $file_offset, $file_size );

$file_so_far = isset($_GET['file_so_far']) ? $_GET['file_so_far'] : '';

$file_stream = fopen($file_name, "r");
fseek($file_stream,$file_offset,SEEK_SET);
$file_so_far .= fread($file_stream,1);
fclose($file_stream);

if ( $file_size > $file_offset ) {
$file_offset++;
$more_to_come = 'yes';
$url_file_so_far = htmlentities(urlencode($file_so_far));
header("Refresh: 0; URL={$PHP_SELF}?file_offset={$file_offset}&file_so_far={$url_file_so_far}");
} else {
$more_to_come = 'start over';
header("Refresh: 3; URL={$PHP_SELF}?file_offset=0");
}

print <<<END
<fieldset style="width:300px">
<legend>Info</legend>
file name = {$file_name} <br/>
file size = {$file_size}<br/>
next offset = {$file_offset}<br/>
more to come ? {$more_to_come}<br/>
</fieldset>
<br />
<br />
<font face='Com' size='7'>{$file_so_far}</font>
END;

?>

So, it can be donw with php!

Posted: Tue Feb 04, 2003 10:56 am
by mydimension
well, then your previous method won't work as you don't send any status parameters between each refresh as you do with this longer on. if you can figure how to send these parameters along with the refresh (session maybe?) then you should be fine.

Posted: Tue Feb 04, 2003 12:22 pm
by Nik
Ye nut as i have told i dont want this to become in that way! i always seek better and more siple ways! Evilcoder if u read this pls respond!

Posted: Wed Feb 05, 2003 12:20 pm
by Nik
<?php
ob_start(); error_reporting(E_ALL); $i=$j=0;
echo "<body bgproperties='fixed' background='pics/anemos.bmp'>";

if (@mysql_connect('localhost', 'nik', 'macgyver'));
else @mysql_connect('localhost', 'root');
mysql_select_db('db_nik');

mysql_query("UPDATE visitor SET i=i+1");
$sql = mysql_query("SELECT i FROM visitor");
$i = mysql_fetch_array($sql);

$f = fopen("data.txt", "r");
$keimeno = fread($f, filesize("data.txt"));

if ($keimeno[$j]=='EOF') {mysql_query("UPDATE visitor SET i=0"); exit;}
?>

<center><table border='1' cellspacing='1' width='80%' id='AutoNumber1' bordercolor='Yellow' style='border-style: solid; border-width: 5' background='pics/brick.jpg'>
<tr>
<td align=left bordercolor='#ffcc00'> <font face='Com' size='4' color='LightBlue'>
<? while ($j<=$i[0]) echo $keimeno[$j++]; ?>
</td>
</tr>
</table>

<?
header("Refresh: 0; URL=http://localhost/test.php");
ob_end_flush();
?>


Everythings seems to work ok but i cannot seem to end the loop when the file ends why?

If u pls help!

Posted: Wed Feb 05, 2003 12:32 pm
by volka
because you're sending the refresh-header wether there is more data or not.
You're also aware that 2 people requesting the same page at the same time will double the speed?

Posted: Wed Feb 05, 2003 12:34 pm
by Nik
really?? No am not aware of this!! can u pls show me how to know wehere the string ends s that i will not have to refresh?? :))

Thanks!!

Posted: Wed Feb 05, 2003 12:43 pm
by volka
try

Code: Select all

<?php
session_start();
if (!isset($_SESSION['i']))
	$_SESSION['i'] = 0;

$f = fopen("data.txt", "r");
$keimeno = fread($f, ++$_SESSION['i']);

if(strlen($keimeno) == $_SESSION['i'])
	header("Refresh: 1; URL=http://localhost/test.php"); // changed timeout to 1 second for testing
// if you want to enable "re-writing" you need something like
// else
//    $_SESSION['i']=0;    --or--  session_destroy();    here
?>
<html><body>
	<center>
		<table border="1" cellspacing="1" width="80%" id="AutoNumber1" bordercolor="Yellow" style="border-style: solid; border-width: 5" background="pics/brick.jpg">
			<tr>
				<td align=left bordercolor="#ffcc00">
					<font face="Com" size="4" color="LightBlue">
<?php echo $keimeno; ?> 
					</font>
				</td>
			</tr>
		</table>
	</center>
</body></html>
Are you still sure you do not want to use javascript? I mean, always a complete request for only one character...?
Anyways ;)
btw: which version of php are you using? Maybe $_SESSION doesn't work

Posted: Wed Feb 05, 2003 12:46 pm
by Nik
lol, i guess u r right man!!

But i dont know nthing about javascript!!

Why js is better in this occasion?

Posted: Wed Feb 05, 2003 12:49 pm
by Nik
By the way!! The script u gave me rocks!! Works great! And u dont even save the files position to a text file or to a mysql table!!! wooww!!! How do u do that man? its great!!!

This session thing is cool!! Can u tell me what exaclt did u do?

Posted: Wed Feb 05, 2003 12:52 pm
by volka
waste of valuable server computing time e.g. ;)
as mentioned it's a complete new request for each single character.
You're testing it at your local, personal box where only you access the script, but think about putting it on a shared webserver with multiple concurrent requests (not only your script)

Posted: Wed Feb 05, 2003 12:53 pm
by Nik
I also wantes to sk if i could refresh the page at 1/5 of a sec insted of 1 sec with header command can it be done??

sorry for the bother man!