M
M
mihailos2020-04-16 08:34:35
PHP
mihailos, 2020-04-16 08:34:35

How can I fix a slow header?

There is an index file on it there is a form, the action of this form is check.php
check.php

<?php
session_start();
require_once 'connection.php';
    $login = $_POST['login'];
    $email = $_POST['email'];
    $pass = $_POST['pass'];
    $pas_con = $_POST['pass2'];
    if($pass === $pas_con && $pass != ''){
            $passw = md5($pass);
            $query = $conn->prepare("INSERT INTO users(login, email, password) VALUES(?, ?, ?)");
            $query->execute([$login, $email, $passw]);
            $_SESSION['message'] = 'Регистрация прошла успешно! Теперь вы можете войти в свой аккаунт.';
            header("Location: /");

    }else{
        $_SESSION['message'] = 'Пароли не совпадают!';
        header("Location: /");
    }
?>

There is a redirect to index to issue an alert that the registration was successful or not.
Why does it take a long time to load the header? And how can this be corrected? Or is it possible to somehow reduce redirects?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mylistryx, 2020-04-16
@Mylistryx

Most likely not the slow work of the header!
In the database connection settings, change localhost to 127.0.0.1 and then the connection to the database will not take ~ 1 second.
How to check? Comment out the work with the database in the file, if everything is good, then the problem is in the slow connection to the database.

//$query = $conn->prepare("INSERT INTO users(login, email, password) VALUES(?, ?, ?)");
//$query->execute([$login, $email, $passw]);

N
nokimaro, 2020-04-16
@nokimaro

You don't have a problem with header() being slow, but something else.
For example, what can slow down in the provided code:
1. session_start (lock or disk brakes if the session is on files)
2. connect to the database from connection.php
3. queries to the database (insert)
4. the script lags / where the redirect goes and it seems to take a long time answer
5. slows down the web server in principle (TTFB)
If you still think that header() slows down,
then I would like to see the metrics (screenshot) where you can see the fact of slowing down (waterfall is the browser's dev tools). For example, the same script where there is nothing but <?php header("Location /");against a script that slows down

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question