Jump to content

PHP loading files twice - normal?


bobkoure

Recommended Posts

Hi,

I've got a PHP app that writes some information to a MySQL database.

To keep the data from being entered twice, I've got a separate "update database" PHP file that should not go to the browser at all (no HTML other than header('location:...').

The puzzle is that the data still gets entered in duplicate.

I've written a bit of code to detect this double load by using the database (IP of requesting workstation, time of last request from that IP). This catches the double load and keeps it from being a double write - but I still have a case of the WTFs.

 

The calling page is calling through a simple page action

<form class="page" id="form1" name="form1" method="get" action="updateDB.php" onsubmit="return checkinput();">

 

The called file (hard to call it a "page"), gets all options passed to it via Get, detects a second call from the same IP within 3 seconds, and does not write to the db if it detects this second call.

After all this, I have

$options = buildOptions($startSN); // builds all the options in the "?x=1&y=2" format
$location = sprintf('location:%s%s','askdone.php',$options);
header($location);

 

So... is it just normal for PHP to load a file twice? I know this mostly doesn't matter, but I'd love to know if I'm approaching this problem the right way (or wrong, of course).

 

Thanks! ...and please ask for more details if that might help. I'm a newbie here and not sure how much "stuff" to put into a question.

Which forwards fine - but I get that second load

 

Link to comment
https://forums.phpfreaks.com/topic/157438-php-loading-files-twice-normal/
Share on other sites

Absolutely

<?php
require_once("include/common.php");
require_once("./include/open_db.php");
// this sits between howmany.php and askdone.php
// it updates the "counter" and writes all db records as though the labels were already printed
// (this is a request from Eric C. - floor folks were *not* pressing the "Yes" key in askdone, so records were being lost)
// All URL vars are passed forward by building the URL "by hand" - no browser involved
// this PHP file hould never be visible to the user - it jumps via "header(location)

function buildOptions($StartSN)
{
$options = '?'.'StartSN='.(string)$StartSN;
$get = $GLOBALS['_GET'];
foreach($get as $key => $val)
  {
  $options=$options.sprintf('&%s=%s',$key,$val);
  }
return $options;
}

if((isset($_GET['Series']))&&(isset($_GET['Qty'])))
{
$qty = $_GET['Qty'];
$series = $_GET['Series'];
$series_base  = $series_list[$series];

if($qty > 0)
  {
  /*
  $startSN = ReserveSnBlock($qty); // we've now "reserved" $startSN to $startsn + $qty
  WriteRecordToDb($series_base,$startSN+$i); // write to the DB, using $series_base[dbItems]
  */
    $refresh = IsRefresh($qty);
  if($refresh == 0)
    {
    $startSN = ReserveSnBlock($qty); // we've now "reserved" $startSN to $startsn + $qty
    // printf('startsn %s qty %s<br>',$startSN,$qty);
    for ($i=0; $i<$qty; $i++)
      {
      WriteRecordToDb($series_base,$startSN+$i); // write to the DB, using $series_base[dbItems]
      }
    }
  else
    {
    // this is a refresh - don't write to the DB, don't update counters, just set old startSN
    $startSN = $refresh;
    }
  $options = buildOptions($startSN);
  $location = sprintf('location:%s%s','askdone.php',$options);
  header($location);
  }

}

?>

 

Qty is passed along the URL

common.php, along with $series are use to get a list of what fields to write to the db.

 

Thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.