User registration function too slow I just wrote a PHP code to register users.

It's called through AJAX and it looks like it's too slow because it often sent me a .status = 0 that it means it took too much time to proceed.

If i replace my php function that register only by :

echo "Ok";


It always sent me a .status = 200

Could someone helps me to improve this file to make it faster ? I'm not used to code in PHP.

&lt;?php

require_once('../config/database.php');
require_once('../config/functions.php');
$keywords = preg_split("/MyWebSite\//", getcwd());
$folder = explode('/', $keywords[1]);

if (!isset($_POST['login']) || !isset($_POST['email']) || !isset($_POST['password']) || !isset($_POST['password_check']))
{
    echo "missing some fields, did you try to edit my html?";
    die();
}
if (strlen($_POST['login']) &lt; 5 || strlen($_POST['email']) == 0 || strlen($_POST['password']) &lt; 5 || strlen($_POST['password_check']) &lt; 5)
{
    echo "fieds badly filled";
    die();
}
if ($_POST['password'] != $_POST['password_check'])
{
    echo "passwords field aren't same";
    die();
}



try
{
    $db = new PDO($DB_DSN, $DB_USER, $DB_PASSWORD);
    $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // to get an exception when caught an error :)
} catch (PDOException $e) {
    print "Erreur !: " . $e-&gt;getMessage() . "&lt;br/&gt;";
    die();
}

$count_login = $db-&gt;prepare("SELECT COUNT(*) FROM camagru_jgengo.user WHERE login = :login");
$count_login-&gt;bindValue(':login', $_POST['login']);
$count_login-&gt;execute();
if ($count_login-&gt;fetchColumn() &gt; 0)
{
    echo "This username already taken";
    die();
}

$count_email = $db-&gt;prepare("SELECT COUNT(*) FROM camagru_jgengo.user WHERE email = :email");
$count_email-&gt;bindValue(':email', $_POST['email']);
$count_email-&gt;execute();
if ($count_email-&gt;fetchColumn() &gt; 0)
{
    echo "This email already taken";
    die();
}



$insert = $db-&gt;prepare('INSERT INTO camagru_jgengo.user (email, login, password, admin, validate_link, created_at) VALUES (:email, :login, :password, 0, :validate_link, :created_at)');
$arr = array(
    ":login" =&gt; $_POST['login'],
    ":password" =&gt; hash_it($_POST['password']),
    ":email" =&gt; $_POST['email'],
    ":validate_link" =&gt; hash('md5', time()),
    ":created_at" =&gt; date('Y-m-d')
    );
$insert-&gt;execute($arr);

echo "Created!\n";

mail ($_POST['email'], "[Camagru] Active your account", "To active your account click that link : http://localhost:8080/".$folder[0]."/?p=activate&amp;hash=".$arr[':validate_link']);

?&gt;
isset can take multiple inputs, and fails if any of them is not set. So, the first if clause could become:

if (!isset($_POST['login'], $_POST['email'], $_POST['password'], $_POST['password_check']))

I personally prefer using !== so that type juggling will not happen.

if ($_POST['password'] !== $_POST['password_check'])

Instead of using SELECT COUNT(*) query to fetch existing username/email in your DB, you can instead set those columns to be UNIQUE and just go to the INSERT statement. The INSERT will fail with error code 1062.
Instead of using camagru_jgengo.user everywhere, you can set the dbname in the DSN string.
While not necessary, you can switch to using crypt() instead of md5 hashing. An MD5 has a higher collision chance, and since you are using it for sending emails with verification links. Using some form of salt will protect you from sending the same verification links to multiple users.