使用header()重定向。 标头()不起作用(可能是因为数据已发送)

问题描述:

I am working on a noughts and crosses project. I am trying to redirect the user to a "Please wait" page while the other user has their move.

At the start of the game I give each player a letter (I know this works).

$find_user = mysql_query("SELECT `user_id` FROM `games` WHERE `game_id`='".$number_of_games."'");
$find_user = mysql_result($find_user, 0);

if($find_user == $_SESSION["userId"]) {
    $letter = "X";
} else {
    $letter = "O";
}

The rest of the page is generated and stuff is written to the page.

After the user takes their move the following is run:

header("Location: ../wait".$turn.".php");

This works for the person who is "X" (The person who creates the game on the database) and they are redirected to a page saying "Please wait for O to have their move.".

However when "O" has their move, they are not redirected to wait now page.

The header is in the same place in the code.

Also, if this makes any difference... The following code is at the bottom of the php file that the player plays the game on (To refresh it).

$url=$_SERVER['REQUEST_URI'];
header("Refresh: 5; URL=$url"); 

I know the structure is messed up, but its my first big PHP project and I could do with some help.

Why is redirecting for "X" but not for "O"?

Update:

If I type:

print("<script>top.location.href = '../waitO.php'</script>");

It redirects.

If I type:

print("<script>top.location.href = '../wait'".$letter."'.php'</script>"); 

It does nothing.

you can also redirect like this:

print("<script>top.location.href = '...'</script>")

It's because header redirection function works only before you print HTML content (or any content).

If you was sended info, you need to redirect with HTML like:

<meta http-equiv="refresh" content="0; url=http://example.com/" />