このスクリプトは 2 日前に動作しました。しかし、今ではすべての登録で、ユーザー名が定義されていない、パスワードが定義されていない、電子メールが定義されていないというエラーが表示されます。
$username、$password、$email 変数が設定されていないようです。
これは私のerror_logからのものです:
PHP 警告: mysql_real_escape_string() [function.mysql-real-escape-string]: 行 52 の /registerScr.php でユーザー 'root'@'localhost' (使用パスワード: NO) のアクセスが拒否されました PHP 警告: mysql_real_escape_string() [ function.mysql-real-escape-string]: サーバーへのリンクを 52 行目の /registerScr.php で確立できませんでした
52 行目には、mysql_real_escape_string 関数があります。解決策についてstackoverflowで検索しましたが、私の問題を解決した人はいません。
コードは次のとおりです。
<?
/* Include Files *********************/
session_start();
include("db.php"); //Database connection
include("lScr.php"); // check if I am connected
/*************************************/
?>
<!DOCTYPE html>
<html>
................
<?
function protect($string){
$string = mysql_real_escape_string($string);
$string = strip_tags($string);
$string = addslashes($string);
return $string;
}
echo " <br>
<form class=\"form-horizontal\" action=\"\" method=\"post\">
<fieldset>
<div class=\"control-group\">
<label class=\"control-label\" for=\"input01\">Username </label>
<div class=\"controls\">
<input type=\"text\" class=\"input-xlarge\" id=\"input01\" name=\"username\" maxlength=\"30\" placeholder=\"Your username\">
</div>
</div>
<div class=\"control-group\">
<label class=\"control-label\" for=\"input01\">Password </label>
<div class=\"controls\">
<input type=\"password\" class=\"input-xlarge\" id=\"input01\" name=\"password\" maxlength=\"30\" placeholder=\"Your password\">
</div>
</div>
<div class=\"control-group\">
<label class=\"control-label\" for=\"input01\">Confirm Password </label>
<div class=\"controls\">
<input type=\"password\" class=\"input-xlarge\" id=\"input01\" name=\"passconf\" maxlength=\"30\" placeholder=\"Your password,again!\">
</div>
</div>
<div class=\"control-group\">
<label class=\"control-label\" for=\"input01\">E-mail </label>
<div class=\"controls\">
<input type=\"text\" class=\"input-xlarge\" id=\"input01\" name=\"email\" placeholder=\"Your e-mail\">
</div>
</div>
<div class=\"control-group\">
<label class=\"control-label\" for=\"input01\">Enter Captcha </label>
<div class=\"controls\">";
require_once('recaptchalib.php');
$publickey = "examplekey******"; // you got this from the signup page
echo recaptcha_get_html($publickey);
echo"
</div>
</div>
<br/><div class=\"control-group\">
<div class=\"controls\">
<input type=\"submit\" class=\"btn btn-primary\" name=\"submit\" value=\"Register\">
</div>
</div>
</fieldset>
</form>";
?>
</div>
<div class="span4">
<? if (isset($_POST['submit'])) {
$username = protect($_POST['username']);
$password = protect($_POST['password']);
$confirm = protect($_POST['passconf']);
$email = protect($_POST['email']);
$errors = array();
if(!$username){
$errors[] = "Username is not defined!";
}
if(!$password){
$errors[] = "Password is not defined!";
}
if($password){
if(!$confirm){
$errors[] = "Confirmation password is not defined!";
}
}
if(!$email){
$errors[] = "E-mail is not defined!";
}
if($username){
$aValid = array('-', '_');
if(!ctype_alnum(str_replace($aValid, '', $username))){
$errors[] = "Username can only contain numbers, letters and symbols like \"-\" or \"_\"!";
}
$range = range(1,32);
if(!in_array(strlen($username),$range)){
$errors[] = "Username must be between 1 and 32 characters!";
}
}
if($password && $confirm){
if($password != $confirm){
$errors[] = "Passwords do not match!";
}
}
if($email){
$checkemail = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
if(!preg_match($checkemail, $email)){
$errors[] = "E-mail is not valid, must be name@server.tld!";
}
}
if($username){
$sql = "SELECT * FROM `users` WHERE `username`='".$username."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) > 0){
$errors[] = "The username you supplied is already in use!";
}
}
if($email){
$sql2 = "SELECT * FROM `users` WHERE `email`='".$email."'";
$res2 = mysql_query($sql2) or die(mysql_error());
if(mysql_num_rows($res2) > 0){
$errors[] = "The e-mail address you supplied is already in use of another user!";
}
}
require_once('recaptchalib.php');
$privatekey = "examplekey*****";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
$errors[] = "Wrong Captcha. Please re-write Captcha!";
}
if(count($errors) > 0){
foreach($errors AS $error){
echo "<div class=\"alert alert-block alert-error fade in\">
<h4 class=\"alert-heading\"><li>$error</li></h4></div>";
}
}else {
$sql4 = "INSERT INTO `users`
(`username`,`password`,`email`)
VALUES ('".$username."','".$password."','".$email."')";
$res4 = mysql_query($sql4) or die(mysql_error());
echo "Registed";
}
}
?>
db.php コード:
$conn = mysql_connect($dbhost, $dbuser, $dbpassword);
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_example", $conn);