提问者:小点点

标题('location:。。/。。/index.php'); PHP不工作


我试图去索引页面时,用户注册,但没有发生什么,当我注册。

我的凭据进入数据库,一切都很好,但是当我到了必须转到另一个页面的时候,它就不起作用了,我仍然停留在register.php页面上。

我在这里搜索,我看到的对每个人都有效的主要答案是ob_start();。 我在connect.php文件中打开了它,但仍然没有帮助。 所以我从connect.php文件中删除了它,并将它放入register_handler文件中,但它仍然不能工作。 我看不出有什么别的办法可以做这件事。 谁能帮帮忙吗?

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(-1); 

//check if form is submitted
/*if ( $_SERVER['REQUEST_METHOD'] != 'POST' || ! isset($_POST['Register'])) {

    // looks like a hack, send to index.php
    header('Location: index.php');
    die();
} */

require '../../config/connect.php';
$con = new mysqli(...$dbCredentials);

$first_name = $_POST['first_name'] ?? '';
$last_name = $_POST['last_name'] ?? '';
$username = $_POST['username'] ?? '';
$email = $_POST['email'] ?? '';
$pw = $_POST['pw'] ?? '';
$pw2 = $_POST['pw2'] ?? '';

$errors = [];

//$errors = array();

if (!trim($first_name)) {

    $errors[] = "Fill in first name to sign up";
}
if (!ctype_alnum($first_name)) {
    $errors[] = "Invalid first name, it only may contain letters or digits";
}

if (!trim($last_name)) {

    $errors[] = "Fill in last name to sign up";
}
if (!ctype_alnum($last_name)) {
    $errors[] = "Invalid last name, only letters and/or digits.";
}

if (!trim($username)) {

    $errors[] = "Fill in last name to sign up";
}
if (!ctype_alnum($username)) {
    $errors[] = "Invalid last name, only letters and/or digits.";
}

if (!trim($pw)) {
    $errors[] = "Fill in password to sign up";
}
if (!trim($pw2)) {
    $errors[] = "Confirm password to sign up";
}

if (!trim($email)) {
    $errors[] = "Fill in email to sign up";
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $errors[] = "Invalid email";
}

if ($pw !== $pw2) {

    //$_SESSION['error'] = '<b><p style="color: #000000; font-size: 25px; top: 34%;right: 30%;position: absolute;">The passwords do not match.</p></b>';

    echo "Your passwords do not match";
}

if (!$errors) {

    $check_username = $con->prepare("SELECT username FROM users WHERE username=?");
    $check_username->bind_param("s", $username);
    $check_username->execute();
    $row = $check_username->get_result()->fetch_assoc();

    if ($row) {

        echo "That username is already in use";
        die();
    }
}

if (!$errors) {

    $check_username = $con->prepare("SELECT email FROM users WHERE email=?");
    $check_username->bind_param("s", $email);
    $check_username->execute();
    $row = $check_username->get_result()->fetch_assoc();
    
    if ($row) {
        echo "That email is already in use";
        die();
    }
}

if (!$errors) {

    $pw = password_hash($_POST['pw'], PASSWORD_BCRYPT, array('cost' => 14));

    $stmt = $con->prepare("INSERT INTO users (first_name, last_name, username, email, pw) 
        VALUES (?, ?, ?, ?, ?)");
    $stmt->bind_param("sssss", $_POST['first_name'], $_POST['last_name'], $_POST['username'], 
        $_POST['email'], $pw);
    $stmt->execute();

    header('Location: ../../index.php');
    exit();

} else {
    // The foreach construct provides an easy way to iterate over arrays. 
    foreach ($errors as $error) {
        $errors[] = 'An error occurred.';
    }
    
}

共1个答案

匿名用户

header('Location: ../../index.php');

尝试检查位置是否存在。

1.)放置完整位置2.)如果文件在同一个文件夹中,则直接使用index.php

检查一下,然后告诉我。