开发者

Redirecting Pages with PHP causing problems

I have a page which has a link to a php page which takes data from $_GET and updates a database. After that it returns the user to the homepage with:

header("Location: http://localhost/");

The thing is that this seems to "interrupt" the mysql part of the code. If I remove this redirect, everything in the database is updated, but when I put it back, nothing gets updated...

This is the database update code, I am using a class of mine as a mysql wrapper:

$conn->where('hash',$data1['hash']);
$conn->update(TABLE_ITEMS,$newData1);

$conn->where('hash',$data2['hash']);
$conn->update(TABLE_ITEMS,$newData2);

Notes:

-There is no text o开发者_开发知识库r echo()'s on the page and no space before the <?php tag

Order of Code:

  1. Data received from $_SESSION and $_GET
  2. Data processed and placed into arrays
  3. Data placed into mysql database
  4. header(); used to redirect page

Code

<?php
require_once('config.php');
import();

if ( isset ( $_GET['g'] ) && isset ( $_SESSION['itemA'] ) && isset ( $_SESSION['itemB'] ) ) {
$itemA = $_SESSION['gameA'];
$itemB = $_SESSION['gameB'];

$newData1 = processData($itemA);
$newData2 = processData($itemB);

$conn->update(TABLE_ITEMS,$newData1);

$conn->update(TABLE_ITEMS,$newData2);

header('Location: http://localhost/');

} else {
    header('Location: http://localhost/');
}


If you send a header when previously content is outputted, you will get an error that may cause your script to stop execution. So if the header is above the update, the update may not be executed at all. It depends on your settings whether you see this error or not.

<?
echo 'yo';
header('Location: ....'); // <-- error

Update(); // Never gets executed

The output doesn't have to be an echo. It can even be a single space before the opening <?.


Without seeing much of the code, it's hard to be certain, but my guess would be that the PHP page is continuing to work exactly at it was before. What I would suggest might be happening is that the redirected page (ie your home page) is itself doing some database work which is overwriting the changes that had been done by the original page.

As I say, that's quite a wild guess in the absence of any more code (or even any detail about the data in question or what the site does), but I'd say it's worth investigating that possibility.


Try putting ob_start() at the top of the file. It sometimes helps. You can't output before calling header(). Show more code. It's to less of it to think what is wrong.


I have no idea why this worked, but it turned out that if I change this:

header("Location: http://localhost/");

to this:

header('Location: http://localhost/');

everything works. Weird!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜