Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for file

Status
Not open for further replies.

Mute101

Programmer
Jun 28, 2001
428
GB
Hi peeps,

I am running a web application which I can track orders from a unique order number, I am trying to stop my application from adding one to the order no when it has already done it once. (ie I dont want the orderno to go up if a user hits the refresh button). The code generates a .dat file holding all the order info so I check to see if the file is created.

This is the code I am using but it doesn't work!!!

It does seem to function to a degree but it is very unstable. Any ideas?


#Get OrderNo for filename
open(INFILE,"$ENV{'DOCUMENT_ROOT'}/compaq/orderno.dat") or die "Can not open KPorderNo data file";
$orderno = <INFILE>;
close(INFILE);

#Check for data file
$file = &quot;$ENV{'DOCUMENT_ROOT'}/compaq/orders/$orderno.dat&quot;;

unless(-e $file)
{
#Move order number on
open(INFILE,&quot;>$ENV{'DOCUMENT_ROOT'}/compaq/orderno.dat&quot;) or die &quot;Can not open KPorderNo data file&quot;;
$neword = sprintf(&quot;%06d&quot;,$orderno+1);
print INFILE &quot;$neword&quot;;
close(INFILE);
}
 
you also need to create the file itself, no?

open(INFILE,&quot;>$ENV{'DOCUMENT_ROOT'}/compaq/$orderno.dat&quot;) or die &quot;Can not create order file&quot;;
close(INFILE);
 
Sorry I just posted a fragment of code showing the check for file section, the file itself is created after this code is run therefore the first time it runs it won' tfind the file but every time after it will and will skip the orderno+1 bit :)
 
Full RELEVANT code

sub Load()
{
#Get OrderNo for filename
open(INFILE,&quot;$ENV{'DOCUMENT_ROOT'}/compaq/orderno.dat&quot;) or die &quot;Can not open KPorderNo data file&quot;;
$orderno = <INFILE>;
close(INFILE);

#Check for data file
$file = &quot;$ENV{'DOCUMENT_ROOT'}/compaq/orders/$orderno.dat&quot;;

unless(-e $file)
{
#Move order number on
open(INFILE,&quot;>$ENV{'DOCUMENT_ROOT'}/compaq/orderno.dat&quot;) or die &quot;Can not open KPorderNo data file&quot;;
$neword = sprintf(&quot;%06d&quot;,$orderno+1);
print INFILE &quot;$neword&quot;;
close(INFILE);
}

#check referer
$refer = $query->param(&quot;From&quot;);
if ($refer eq &quot;recert&quot;)
{
$Prodtemplate = &quot;$ENV{'DOCUMENT_ROOT'}/compaq/products/products.data&quot;;
}
elsif ($refer eq &quot;ilp&quot;)
{
$Prodtemplate = &quot;$ENV{'DOCUMENT_ROOT'}/compaq/products/ilp.data&quot;;
}
else
{
#redirect????
die &quot;Where the hell did you come from???&quot;
}

open(PROD,&quot;$Prodtemplate&quot;) or die &quot;Can not open products file&quot;;
$a = 1;
while ($line = <PROD>)
{
($prodcode[$a], $prodname[$a], $webcode[$a]) = split /;/, $line;
$a++
};
close(PROD);

open(PRICE,&quot;$ENV{'DOCUMENT_ROOT'}/compaq/kpplfcr/plrc.dat&quot;) or die &quot;Can not open recert price file&quot;;
$p = 1;
while ($line = <PRICE>)
{
($accno[$p], $pricel[$p], $pricem[$p], $priceh[$p]) = split /;/, $line;
$p++
};
close(PRICE);

open(ILPPRICE,&quot;$ENV{'DOCUMENT_ROOT'}/compaq/kpplfcr/ilppl.dat&quot;) or die &quot;Can not open ilp price file&quot;;
$q = 1;
while ($line = <ILPPRICE>)
{
($accno[$q], $price[$q]) = split /;/, $line;
$q++
};
close(ILPPRICE);
};

****************
Calc sub HIDDEN STUFF ;)


open(DATFILE,&quot;>$ENV{'DOCUMENT_ROOT'}/compaq/orders/$orderno.dat&quot;) or die &quot;Can not open Order Data file&quot;;

@names = $query->param;

foreach $name (@names)
{
$kitprice = &quot;&quot;;
$unitprice = &quot;&quot;;

if ($name =~ /....SSK/)
{
if ($query->param($name) > 0)
{
if ($query->param($name) < 21)
{
$kitprice = $query->param($name) * $phigh;
$unitprice = &quot;£$phigh&quot;;
}

elsif ($query->param($name) < 51)
{
$kitprice = $query->param($name) * $pmed;
$unitprice = &quot;£$pmed&quot;;
}

else
{
$kitprice = $query->param($name) * $plow;
$unitprice = &quot;£$plow&quot;;
}

#Store variables
$quantity = $query->param($name);
print DATFILE &quot;$name;$quantity;$kitprice;$unitprice;&quot;;
print DATFILE &quot;\n&quot;;

$b = 1;
while ($b < $a)
{
if ($webcode[$b] eq $name)
{
$code = $prodcode[$b];
$name = $prodname[$b];
}
$b++;
}

$table .= &quot;<tr><td class=\&quot;ul\&quot; align=\&quot;left\&quot;>$name</td><td class=\&quot;ul\&quot; align=\&quot;center\&quot; width=\&quot;60\&quot;><b>$unitprice</b></td><td class=\&quot;ul\&quot; align=\&quot;center\&quot; width=\&quot;60\&quot;><b>$quantity</b></td><td class=\&quot;ul\&quot; align=\&quot;center\&quot; width=\&quot;60\&quot;><b>£$kitprice</b></td></tr>&quot;;
$hiddendata .= &quot;<input type=\&quot;hidden\&quot; name=\&quot;$name\&quot; value=\&quot;$quantity\&quot;>&quot;;
}
}

if ($name =~ /....RET/)
{
if ($query->param($name) > 0)
{
$kitprice = $query->param($name) * 30.00;
$unitprice = &quot;£30&quot;;

#Store variables
$quantity = $query->param($name);
print DATFILE &quot;$name;$quantity;$kitprice;$unitprice;&quot;;
print DATFILE &quot;\n&quot;;

$b = 1;
while ($b < $a)
{
if ($webcode[$b] eq $name)
{
$code = $prodcode[$b];
$name = $prodname[$b];
}
$b++;
}

$table .= &quot;<tr><td class=\&quot;ul\&quot; align=\&quot;left\&quot;>$name</td><td class=\&quot;ul\&quot; align=\&quot;center\&quot; width=\&quot;60\&quot;><b>$unitprice</b></td><td class=\&quot;ul\&quot; align=\&quot;center\&quot; width=\&quot;60\&quot;><b>$quantity</b></td><td class=\&quot;ul\&quot; align=\&quot;center\&quot; width=\&quot;60\&quot;><b>£$kitprice</b></td></tr>&quot;;
$hiddendata .= &quot;<input type=\&quot;hidden\&quot; name=\&quot;$name\&quot; value=\&quot;$quantity\&quot;>&quot;;
}
}

if ($name =~ /....ILP/)
{
if ($query->param($name) > 0)
{
$kitprice = $query->param($name) * $price;
$unitprice = &quot;£$price&quot;;

#Store variables
$quantity = $query->param($name);
print DATFILE &quot;$name;$quantity;$kitprice;$unitprice;&quot;;
print DATFILE &quot;\n&quot;;

$b = 1;
while ($b < $a)
{
if ($webcode[$b] eq $name)
{
$code = $prodcode[$b];
$name = $prodname[$b];
}
$b++;
}

$table .= &quot;<tr><td class=\&quot;ul\&quot; align=\&quot;left\&quot;>$name</td><td class=\&quot;ul\&quot; align=\&quot;center\&quot; width=\&quot;60\&quot;><b>$unitprice</b></td><td class=\&quot;ul\&quot; align=\&quot;center\&quot; width=\&quot;60\&quot;><b>$quantity</b></td><td class=\&quot;ul\&quot; align=\&quot;center\&quot; width=\&quot;60\&quot;><b>£$kitprice</b></td></tr>&quot;;
$hiddendata .= &quot;<input type=\&quot;hidden\&quot; name=\&quot;$name\&quot; value=\&quot;$quantity\&quot;>&quot;;
}
}

}
close(DATFILE);
};

There is more to the beginnig of the second sub but I can't post it here im afraid :(
 
this is long man :)

try comparing between the order no. sent by the user and the filename.
so instead of this:
$file = &quot;$ENV{'DOCUMENT_ROOT'}/compaq/orders/$orderno.dat&quot;;

do this:
$file = &quot;$ENV{'DOCUMENT_ROOT'}/compaq/orders/&quot;. $query->param(&quot;orderno&quot;). &quot;.dat&quot;;

:)
 
I control the order number, nothing is sent from the user, it is mearly an auto incrementing number that is used as a reference for the customers.

OK this is what I want

every time the script runs it should:

#####~~START~~#####

get orderno from ordernofile

if file &quot;orderno.dat&quot; exists
SKIP
else
add 1 to the number stored in ordernofile
end if

create orderno.dat

fill it with orderinfo.

#####~~END~~#####

this means after the first time the script runs if the user refreshes or uses the back button on his browser they wont be able to increment my orderno.

The script fragment at the top of this thread is what I think should work but it is erratic.

Any more ideas?

Why doesn't :

unless(-e $file)
{
#Move order number on
open(INFILE,&quot;>$ENV'DOCUMENT_ROOT'}/compaq/orderno.dat&quot;) or die &quot;Can not open KPorderNo data file&quot;;
$neword = sprintf(&quot;%06d&quot;,$orderno+1);
print INFILE &quot;$neword&quot;;
close(INFILE);
}
 
but you can't know that the order was already sent because you have no reference between the user and the data file. how do you know that it's the same user pressing reload and not a new user? you have to have a session id or cart id or something like this coming from the user, in order to know that this order, from this user, was submitted.
i hope i understood what you want.
try using Apache::Session

:)
 
OK thanks thats what I needed a push in the right direction.

I didn't think about the script being multi threaded :)

Once I have some code together I will post it here to make this thread worthwile :p

Thanks again.
Mute
 
OK some clarification required please.

Here is my sub routine order

&lock;
&Load;
&unlock;
&Calc;
&Data;

So in essence I lock the orderno.dat file while I am pulling numbers or updating it. Does this not mean that multi threads don't actually make any difference to the script. Once a session has started it will update the order no..............................

Wait a minute!!!!

While thinking of the way my script works I have gotten the answer myself!

first time through the script I pull the orderno then add 1 to it in the dat file. Second time (refresh) I pull the orderno and check to see if a file exists with that number but it DOESN'T yet I need to take 1 from the second pull of the order no and check for that number!

Aaaaaarrrrggg! SO simple!

Thanks for making me think about what I have written.

Yours

Noobie Mute :)
 
I could do with an edit post on this forum Ikeep thinking about something else just asI submit the last post :eek:).

Is there a session level variable I can use that doesn't involve and 'special' modules?
 
nope.
but you can use rand to generate a random string that will be unique. i can't write a sample code right now but let me know if you need one and i'll do it later...

:)
 
Creating a random number isn't a problem but how do I make that number persist over the session?

Aarrrrgg! please dont MAKE me use cookies!! :p
 
OK a theory.

I have 2 scripts 1 to display products and 1 to display selected product/totals ect.

##script1
create random number in first script
pass number to hidden form input
create file with number as name
file contains 0
##script1 end

##script2
pull number from hidden form data
check file with number
contains 0
pull orderno
add 1 to orderno
set file to 1
contains 1
pull orderno-1
end check
##script2 end

atm my script relys on a formmail set up by my ISP so the problem is where do I destroy the temp randomno file??

Im gonna go try tis and see what I get.
 
Hi peeps this it NOT a bump :)

just so there is something useful in here here id the code I used.

### ~~ Script 1 ~~ ###

sub createSession()
{
$sn = int(rand(1000));
$uid = $sn.$$;
open(S, &quot;>$ENV{'DOCUMENT_ROOT'}/compaq/seskpno/$uid.session&quot;);
print S &quot;0&quot;;
close(S);
$session = &quot;<input type=\&quot;hidden\&quot; name=\&quot;session\&quot; value=\&quot;$uid\&quot;>&quot;;
}

### ~~ Script 2 ~~ ###

sub Load()
{
#Get OrderNo for filename
open(INFILE,&quot;$ENV{'DOCUMENT_ROOT'}/compaq/orderno.dat&quot;) or die &quot;Can not open KPorderNo data file&quot;;
$orderno = <INFILE>;
close(INFILE);

#Check for data file

$file = $query->param(&quot;session&quot;);

open(S,&quot;$ENV{'DOCUMENT_ROOT'}/compaq/seskpno/$file.session&quot;) or die &quot;Can not open KPorderNo data file&quot;;
$sesinf = <S>;
close(S);

if($sesinf eq &quot;1&quot;)
{
$orderno = sprintf(&quot;%06d&quot;,$orderno-1);
}
else
{
#Move order number on
open(INFILE,&quot;>$ENV{'DOCUMENT_ROOT'}/compaq/orderno.dat&quot;) or die &quot;Can not open KPorderNo data file&quot;;
$neword = sprintf(&quot;%06d&quot;,$orderno+1);
print INFILE &quot;$neword&quot;;
close(INFILE);

open(SI,&quot;>$ENV{'DOCUMENT_ROOT'}/compaq/seskpno/$file.session&quot;) or die &quot;Can not open KPorderNo data file&quot;;
$sesinf = &quot;1&quot;;
print SI &quot;$sesinf&quot;;
close(SI);
}

Dunno if its any use to anyone but its there anyhoo.

Thanks Stakadush for your patience.
 
my post might be a bit late but... :)
you can pass the session id through the links of the webste. just add to every link something like ?sid=$session_id. for example:
this way you can track the user through the website. but it's a much better idea to use cookies.
you can store the ip in a temp file so if a user is browsing your website try looking for the ip to see if there's an open session for this user already (although using ip is not safe).
hope this helps



:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top