2
私のサイトでは、spl_autoload_register
を使用して、class\
というフォルダにあるすべてのクラスを呼び出します。それは素晴らしいですが、私はページにGoogle reCaptchaライブラリを必要としたいと思っています。 spl_autoload_register
はrecaptcha\recaptcha
をクラスと見なし、クラスフォルダのクラスファイルのように自動的に呼び出します。これをどうすれば解決できますか?ありがとうございました。Google reCaptchaとspl_autoload_registerとの競合
コアの\ core.phpの
spl_autoload_register(function($class) {
$path = "class/" . $class . ".php";
require_once($path);
});
post.php
require_once ("core/core.php");
require('exLib/captcha/autoload.php');
$secret = CONFIG::getCaptchaKey("secretKey");
$reCaptcha = new \recaptcha\recaptcha($secret);
$gReCaptchaResponse = $_POST['g-recaptcha-response'];
$remoteIP = $_SERVER['REMOTE_ADDR'];
$resp = $reCaptcha->verify($gReCaptchaResponse, $remoteIP);
if (!$resp->issuccess()) {
echo "invalid";
exit;
} else {
echo "valid";
}
エラー
Fatal error: require_once(): Failed opening required 'class/recaptcha\recaptcha.php' (include_path='.:/usr/local/php56/pear') in /home/user/public_html/home/core/core.php on line 15
この回答は機能しません。それは警告ではなく、間違いです。 'recaptcha \ recaptcha'は' class'フォルダのクラスではないので、if条件は常にfalseを返し、関数はライブラリからファイルをロードしません。 –
関数はそれをロードしません。名前空間を使用して作成します。ですから、あなたはrecaptcha seperateのクラスを要求するか、spl_autoloadにelseなどを追加する必要があります。しかし、あなたは2番目のライブラリに関する情報やファイルの場所を投稿していません... –
ええ、そうです。できます。ありがとうございました。 –