January 14th, 2014, 11:38 AM
Php sends data twice?
I was testing running php script through the image. I have no idea why the following scripts puts two lines of text in my text file?
every time i run this, in the text file i see "11" but i was expecting to see 1.
$name = $_GET['name'];
file_put_contents ( 'test.txt', '1', FILE_APPEND );
// Create a blank image and add some text
$im = imagecreatetruecolor(1, 1);
// Set the content type header - in this case image/jpeg
// Output the image
// Free up memory
January 14th, 2014, 11:54 AM
Are you sure the text file is indeed empty before you run the script? Also check the script with a debugger so that you can track the control flow.
By the way, why do you set the Content-Type header twice?
Last edited by Jacques1; January 14th, 2014 at 11:56 AM.
January 15th, 2014, 03:31 AM
ok i have just tried. if i remove the header(content...) code then it echoes 1 ONCE. if i leave it in, it echoes twice?
Could the script be running twice by browser in case of problems displaying the fake image?
btw i am navigating to the script DIRECTLY.
edited: just confirmed that it is hitting the script twice for some reason..
i added "output .= here1" here ">>> etc and outputted those to the browser and they get repeated twice so the script runs twice
edited 2: Not sure if this is a "proper" solution but I set a session value to 1 and then test this if it exists i dont do file_pu_contents()...
Last edited by paulh1983; January 15th, 2014 at 03:49 AM.
January 15th, 2014, 03:12 PM
You don't happen to use Firefox?
I could reproduce the issue in Firefox, but neither in Chrome nor IE. For some reason, Firefox immediately makes a second request after the first one (verified with the Apache log). I'd say this is a bug.
January 15th, 2014, 03:28 PM
Recycle your old CD's, don't just trash them
If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
January 15th, 2014, 03:53 PM
thanks guys.. you are ace.. i didnt even consider checking another browser. Funnily enough I dont think my firebug had this request coming in twice.. (FF did request it two/three times if image had errors but otherwise I could only see one request being made in Firebug).
So should I be using SESSION solution or just ignore it?