注册页面保持刷新,即使它设置为重定向到 index.php 页面。数据库还保存了新用户信息

问题描述 投票:0回答:0

我被困在用 php 编码的基于文本的游戏的注册位上。 出于某种原因,每次您注册一个帐户时,它都会将您的信息保存到数据库中,但不会重定向到 index.php 页面,它只是重新加载 registration.php 页面。 我试了 7 次,所有 7 个帐户都保存在数据库中,但仍然不会重定向。

使用这些列成功创建数据库: ID - 用户名 - 密码 - 电子邮件 - base_coords - pop - lead - metal - oil - coal - reg_date.

registration.inc.php 代码如下:

<?php
ob_start();
$conn = mysqli_connect("sql109.epizy.com", "epiz_34060505", "GaUwGfHb60p", "epiz_34060505_marzwarz");

// check if registration form was submitted
if(isset($_POST['register'])) {
    // get user input
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    
    // generate random base coordinates
    $base_coords = generate_base_coords();
    
    // insert user data into database
    $sql = "INSERT INTO players (username, password, email, base_coords, pop, lead, metal, oil, coal) VALUES ('$username', '$password', '$email', '$base_coords', 0, 5000, 5000, 5000, 5000)";
    if(mysqli_query($conn, $sql)) {
        // display success message
        echo "Registration successful!";
        // redirect to index.php page after 2 seconds
        header("refresh:2;url=index.php");
    } else {
        echo "Error: " . mysqli_error($conn);
    }
} else {
    // redirect to index.php page after 2 seconds
    header("refresh:2;url=index.php");
}

// function to generate random base coordinates
function generate_base_coords() {
    $letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $coords = "";
    for($i = 0; $i < 2; $i++) {
        $coords .= $letters[rand(0, 25)];
    }
    $coords .= rand(10, 99);
    return $coords;
}
?>

这是 index.php 代码:

<?php
session_start();
require_once 'db.php';

// redirect to registration page if user is not logged in
if (!isset($_SESSION['username'])) {
    header("Location: registration.php");
    exit();
}

// get player's information from database
$username = $_SESSION['username'];
$query = "SELECT base_coords, pop, lead, metal, oil, coal FROM players WHERE username = '$username'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);

// display player's information
echo "Welcome, " . $username . "!<br>";
echo "Your base coordinates are " . $row['base_coords'] . ".<br>";
echo "Population: " . $row['pop'] . "<br>";
echo "Lead: " . $row['lead'] . "<br>";
echo "Metal: " . $row['metal'] . "<br>";
echo "Oil: " . $row['oil'] . "<br>";
echo "Coal: " . $row['coal'] . "<br>";
?>

<html>
<head>
    <title>Game Title</title>
</head>
<body>
    <h1>Welcome to the Game</h1>
    <p>Game content goes here.</p>
</body>
</html>

我期待它重定向到 index.php 页面,新玩家将在其中看到他们的详细信息。

index.php 有一个功能,如果数据库没有获取用户详细信息,则将播放器发送回注册页面

php html
© www.soinside.com 2019 - 2024. All rights reserved.