我正在创建简单的注册脚本,该脚本具有检查用户名和电子邮件是否已被占用的功能。我导入了 php 文件,该文件创建了与 DB 的 mysqli 连接并将其保存到$mysqli
变量中。现在的问题 - 为什么我在我的函数中看不到$mysqli
变量?我必须将它作为参数传递,以便我可以使用它。这是代码
<?php
require_once "./incl/db.php";
require_once "./Logger.class.php";
$logger = new Logger("register.php.log");
function isTaken($username, $email, $mysqli){
$ret = 0;
if($stmt = $mysqli->prepare("SELECT email,nick FROM users WHERE email = ? OR nick = ? LIMIT 1")){
$stmt->bind_param("ss", $email, $username);
if($stmt->execute()){
$stmt->store_result();
if($stmt->num_rows === 0){
$stmt->free_result();
return $ret;
}else{
$stmt->bind_result($dbEmail, $dbNick);
while($stmt->fetch()){
if(strcmp(strtolower($dbEmail), strtolower($email)) == 0)
$ret+= 1;
if(strcmp(strtolower($dbNick), strtolower($username)) == 0)
$ret+= 2;
return $ret;
}
}
}else{
$logger->logError("Error executing stmt(isTaken)! ".$mysqli->error.$logger->newLine.$stmt->error);
die("stmt error");
}
}else{
$logger->logError("Error preparing stmt(isTaken)! ".$mysqli->error.$logger->newLine.$stmt->error);
die("Error preparing stmt!");
}
}
session_start();
ob_start();
header ('HTTP/1.1 302 Found');
if(isset($_POST["registrovat"]) && !empty($_POST["mail"])
&& !empty($_POST["password"]) && !empty($_POST["username"])){
$email = trim($_POST["mail"]);
$username = trim($_POST["username"]);
$password = trim($_POST["password"]);
for($i = 0; $i < 10; $i++){
$password = hash("sha256", $password);
}
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
//echo "E-mail valid";
$isTaken = isTaken($username, $email, $mysqli);
if($isTaken === 0){
if($stmt = $mysqli->prepare("INSERT INTO users (id, nick, password, email, rights) VALUES(NULL, ?, ?, ?, 0)")){
if($stmt->bind_param("sss", $username, $password, $email)){
if($stmt->execute()){
$id = $stmt->insert_id;
header('Location: http://'.$_SERVER["SERVER_NAME"].'/profil/'.$id);
}else{
$logger->logError("Error executing stmt! ".$mysqli->error.$logger->newLine.$stmt->error);
die("Error executing stmt");
}
}else{
$logger->logError("Error binding params(reg): ".$mysqli->error.$logger->newLine.$stmt->error);
die("error");
}
$stmt->close();
}else{
$logger->logError("Error preparing stmt(reg)! ".$mysqli->error.$logger->newLine.$stmt->error);
die("error stmt!");
}
}else{
$text;
switch($isTaken){
case 1: $text = "E-mail already exists"; break;
case 2: $text = "Username already exists"; break;
case 3: $text = "E-mail and username already exists"; break;
default: $text = "default"; break;
}
die($text);
}
}else{
$logger->logError("E-mail not valid: ".$email);
$logger->logInfo(var_export($_POST, true));
die("E-mail not valid");
}
}else{
$logger->logError("Fields empty.");
$logger->logInfo(var_export($_POST, true));
die("Fields empty!");
}
ob_end_flush();
?>