473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Does Synchronization work properly in file handling in php?

hello,
i have prepared a php file for visitcount. that creates a text file per

day. as soon as any user visits that page ,a counter is incremented. but on

my site there is lots of trafic. that's why counter again starts with zero

. and file also written to null.b'coz multiple users(abt 1 to 10 lakh) can

visit page at same time.

so i want to know if code is right then what is the problem?
how can we solve this proble? is there any locking file mechanism in php?
code :
Expand|Select|Wrap|Line Numbers
  1.  $count_path = "../count_visit/".date("d-m-Y").".txt";
  2.  
  3.        if(file_exists($count_path))
  4.        {
  5.         $handle = fopen($count_path, 'r');
  6.                     $read_count= fread($handle, filesize($count_path));     
  7.  
  8.  
  9.                     fclose($handle);
  10.               /* or used    $read_count = file_get_contents($count_path);
  11.               */         
  12.                    if($read_count <> '' )
  13.                    {
  14.                        $inc_count = (int)$read_count + 1;
  15.                        $handle = fopen($count_path, 'w');
  16.                        fwrite($handle, $inc_count);
  17.                        fclose($handle);  
  18.                     }
  19.                  }
  20.                  else
  21.        {
  22.                       // executes when any user visits first time
  23.                       $handle = fopen($count_path, 'w');
  24.                        fwrite($handle, '1' );
  25.                       fclose($handle);
  26.         }
Plz solve this problem.
Jul 26 '07 #1
4 1316
pbmods
5,821 Expert 4TB
Heya, Tushar.

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Jul 26 '07 #2
ak1dnar
1,584 Expert 1GB
Moving thread from Php Articles section to the Forum.
-ajaxrand
Jul 27 '07 #3
pbmods
5,821 Expert 4TB
Heya, Tushar.

Check out flock().
Jul 27 '07 #4
kovik
1,044 Expert 1GB
File-locking may give you unexpected results when multiple visitors come, as some of them will be disenfranchised.

If you have access to a database, you could do this very easily there, as it accounts for multiple queries within itself.
Jul 27 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
3
by: John Bowling | last post by:
I have a java (2.0) program with the following lines: String cmdArray1 = {"lp", "-d", "hp4m", "MyFile"}; System.out.println(Runtime.getRuntime().exec(cmdArray1)); It compliles properly, but...
7
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
11
by: Alvin Bruney [MVP] | last post by:
What happens in the case where a mutex is used to protect a file on a windows application, but a unix script running elsewhere attempts to manipulate that file while the mutex is not signaled? Is...
5
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will...
1
by: Bill Davidson | last post by:
(RESEND: I added a little more code to the sample for clarity) Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things,...
0
by: Rod | last post by:
I orginally posted this to microsoft.public.sqlserver.ce but had not received any responses. I have a CF.NET application (C#) with a SqlCE database. We had originally planned to use SQL...
7
by: atlaste | last post by:
Hi, I have two different things I'd like to discuss here. Both are about cross-process synchronization of shared resources. The resources that are shared are: (1) an object writing to a file and...
15
by: ingejg | last post by:
I am starting to study internet synchronization, and my head is still spinning since internet is not my forte, however my boss is breathing down my neck at the moment. Our company has only one...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.