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.
<?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']) < 5 || strlen($_POST['email']) == 0 || strlen($_POST['password']) < 5 || strlen($_POST['password_check']) < 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->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // to get an exception when caught an error :)
} catch (PDOException $e) {
print "Erreur !: " . $e->getMessage() . "<br/>";
die();
}
$count_login = $db->prepare("SELECT COUNT(*) FROM camagru_jgengo.user WHERE login = :login");
$count_login->bindValue(':login', $_POST['login']);
$count_login->execute();
if ($count_login->fetchColumn() > 0)
{
echo "This username already taken";
die();
}
$count_email = $db->prepare("SELECT COUNT(*) FROM camagru_jgengo.user WHERE email = :email");
$count_email->bindValue(':email', $_POST['email']);
$count_email->execute();
if ($count_email->fetchColumn() > 0)
{
echo "This email already taken";
die();
}
$insert = $db->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" => $_POST['login'],
":password" => hash_it($_POST['password']),
":email" => $_POST['email'],
":validate_link" => hash('md5', time()),
":created_at" => date('Y-m-d')
);
$insert->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&hash=".$arr[':validate_link']);
?>
1 Answer 1
isset
can take multiple inputs, and fails if any of them is not set. So, the firstif
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 beUNIQUE
and just go to theINSERT
statement. TheINSERT
will fail with error code 1062. - Instead of using
camagru_jgengo.user
everywhere, you can set thedbname
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.
-
\$\begingroup\$ Hello, I could not have expect better than this answer :) Thank you a lots ! \$\endgroup\$jgengo– jgengo2017年06月08日 08:01:01 +00:00Commented Jun 8, 2017 at 8:01
camagru_jgengo.user
table? Does it have the correct indexes set? (an index onlogin
and one onemail
) \$\endgroup\$0
is not a valid HTTP status, so it is really unclear what you mean by status = 0. Are you even sure this is a timeout problem? Voting to close as this clearly is not working code. \$\endgroup\$