php script sometime executes twice

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
afaibis
Forum Newbie
Posts: 3
Joined: Fri Aug 31, 2007 11:35 am

php script sometime executes twice

Post by afaibis »

I am sort of new to Web programming, so this may be a real obvious thing...

I wrote a php script that runs on Apache/Linux, PHP 4. It is invoked from a FORM through a GET method. The script does quite a few things:

1) It downloads a file from another website through get_file_contents and saves it to the server
2) It preprocesses the file with some commands executed through system()
3) It sftps the file to another server

This process can take some time for large files - maybe a couple of minutes

For some reason, on certain machines that run Firefox, this script gets executed twice. IE seems to work fine. Also I have a Vista machine with Firefox and that seems to work fine as well.

How do I go about debugging this??

Thanks!

-Andy
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Send some Commands/outputs when the script finishes each step, or post the code and we might be able to help.
afaibis
Forum Newbie
Posts: 3
Joined: Fri Aug 31, 2007 11:35 am

Post by afaibis »

Here is the upload script

Code: Select all

<?php
$fd = fopen("log2", "a");
// write string
$date = date("h:i:s a", time());
fwrite($fd, $date . "\n");
// close file
fclose($fd);

ini_set('memory_limit', '1024M');
session_start();
include 'config.php';
include 'opendb.php';
$video_string =  trim($_GET['txtMovieURL']);
$video_file = file_get_contents($video_string);
$filename = "/home/andy/public_html/proto/" . $_GET['uploadFileName'] . ".flv";
$handle = fopen($filename,"w");
fwrite($handle, $video_file);
fclose($handle);
system('/home/andy/public_html/proto/flvtool.sh -U -metadatacreator:XXX ' . $filename);
$subst1 = "perl -pi -e 's/FILENAME/" . $_GET['uploadFileName'] . ".flv" . "/g' /home/andy/public_html/proto/batchfile";
system($subst1);
system('sftp -b batchfile b7@b7.com');
$subst2 = "perl -pi -e 's/" . $_GET['uploadFileName'] . ".flv". "/FILENAME/g' /home/andy/public_html/proto/batchfile";
system($subst2);
$video_insert_sql = "insert into videos(name) values(\"". $_GET['uploadFileName'] . "\")";
mysql_query($video_insert_sql) or die('Query failed. ' . mysql_error());
?>
<html>
<head>
<title>Main User Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Upload successful
<p><a href="main.php">Main</a> </p>
<p><a href="logout.php">Logout</a> </p>
</body>
</html>
And here is how it gets invoked

Code: Select all

<?php
session_start();

// is the one accessing this page logged in or not?
if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) {
        // not logged in, move to login page
        header('Location: login.php');
        exit;
}
if (isset($_SESSION['user_id']))
{^M
        echo date("h:i:s a", time());
        echo "Welcome ", $_SESSION['user_id'];
}
?>
<html>
<head>
<title>Upload your video</title>^M
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="upload.php" method="get" name="frmUploadMovie" id="frmUploadMovie">
 <table width="400" border="0" align="center" cellpadding="2" cellspacing="2">
  <tr>
   <td width="150">Enter Movie URL</td>
   <td><input name="txtMovieURL" type="text" id="txtMovieURL"></td>
  </tr>
  <tr>
   <td width="150">Enter Upload Filename</td>
   <td><input name="uploadFileName" type="text" id="uploadFileName"></td>
  </tr>
  <tr>
   <td width="150">&nbsp;</td>
   <td><input name="btnLoadMovie" type="submit" id="btnLoadMovie" value="Upload Movie"></td>
  </tr>
 </table>
</form>
<p>&nbsp;</p>
<p><a href="logout.php">Logout</a> </p>
</body>
</html>

On the browser screen I see

Code: Select all

sftp> cd dir1 sftp> cd dir2 sftp> cd dir3 sftp> put beast5.flv Uploading beast5.flv to /b7/dir1/dir2/dir3/beast5.flv sftp> sftp> Upload successful
It looks to me like this gets displayed, and then when the script is executed again, the browser refreshes and displays it again.

I can tell the script is being executed twice because I get two timestamps in the log file.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Well since you are putting it in a database why not check and see if it exists before you start.
afaibis
Forum Newbie
Posts: 3
Joined: Fri Aug 31, 2007 11:35 am

Post by afaibis »

Well, I can do that, but I would like to understand why it is happening. It only happens for Firefox and only on some machines...
julz
Forum Newbie
Posts: 1
Joined: Mon Sep 03, 2007 5:05 am

Post by julz »

hi afaibis
i don't know if you have solved it by now, but i had the same problem and i solved it.
In the <form> tag you have both the 'id' and the 'name' properties set - you have to use only one.
and that's because they are the same except that 'name' is considered outdated.

hope that was helpful
Post Reply