2

I've been php coding for a long time and have never seen anything like this. I recently changed web hosts and have started seeing some of my scripts running two or three times when they should only run once. But, it only seems to happen when there is a header(Location) kind of redirect.

If I comment out the redirect, the script will run once as it should, but if I leave in the redirect, it will run two or three times before redirecting.

How I know this is that the script inserts information into an SQL database and will insert two or three duplicates of the information.

I created a simple test of it at www.realitychurchweb.net/test1.php

The script for that page is:

require_once("handlers/connection.php");

mysql_query("INSERT INTO AA_GENERIC (name, value)VALUES('testing', '123')");

header("Location: test2.php");

The script for test2.php is:

require_once("handlers/connection.php");

$q = mysql_query("SELECT * FROM AA_GENERIC WHERE name = 'testing'");

$num = mysql_num_rows($q);

echo "There are now $num rows<br /><br />";

If I type in the URL to a browser (Safari) it will enter 1, 2, or 3 rows before redirecting to test2, which then shows how many rows now exist.

I spoke with the tech support of the web host and they were of no help. I'm really at a loss with this one.

3
  • 2
    I don't get the problem. When I call your test script, I get one row inserted. When I call it again, it's the expected two rows, then three and so on. What makes you think it's executing multiple times? Commented Apr 12, 2016 at 5:45
  • A few things 1) your test is not reproducible 2) Do not "do" anything with a GET request, use POST 3) You are quite likely calling your test script multiple times without realizing, it's the common cause for such (bad-idea) scripts to be called multiple times i.e. by having broken images, css or js references. Log the current url when you insert into the db 4) Who is upvoting this question... Commented Apr 12, 2016 at 8:06
  • You are correct that it's not doing it anymore. I didn't change anything. The only thing I can think is that the server was doing it. Thanks for checking up on it. Commented Apr 13, 2016 at 11:35

1 Answer 1

1

Have you already tried exit; after your header() call in script test1.php ?

It's not possible that the code is executed several times, so either the script is called multiple times (e.g. bots, cronjob, etc.) or some other code (maybe after the redirection?) is executed that results in multiple database rows.

Sign up to request clarification or add additional context in comments.

3 Comments

I think you mean exit();
@EdwardBlack: As exit is a PHP language construct and not a function, you can also just use exit; . See: php.net/manual/en/function.exit.php
Then why is it even possible to call it like a function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.