Php __autoload()vs include,会自动加载()输出什么吗?
我正在开发一个基于第三方的程序。我编写了一个类并使用_autoload()加载该类。 程序运行如下:Php __autoload()vs include,会自动加载()输出什么吗?,php,include,autoload,Php,Include,Autoload,我正在开发一个基于第三方的程序。我编写了一个类并使用_autoload()加载该类。 程序运行如下: 1.我将脚本上载到远程服务器。 2.第三方服务器将使用一些get参数请求我的脚本。 3.我的脚本返回str,第三方服务器将检查str,如果str与get参数相同,则它是有效的,如果不是,则它是无效的。 现在,问题是: 当我使用自动加载()时,它会引发一些错误,当我用include/require替换自动加载时,它将准确运行。 public function __construct
1.我将脚本上载到远程服务器。
2.第三方服务器将使用一些get参数请求我的脚本。
3.我的脚本返回str,第三方服务器将检查str,如果str与get参数相同,则它是有效的,如果不是,则它是无效的。
现在,问题是:
当我使用自动加载()时,它会引发一些错误,当我用include/require替换自动加载时,它将准确运行。
public function __construct($options){
$this->_token=isset($options['token'])?$options['token']:'';
}
public function test(){
var_dump($this->_token);
}
private function checkSignature(){
$signature = isset($_GET["signature"])?$_GET["signature"]:'';
$timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:'';
$nonce = isset($_GET["nonce"])?$_GET["nonce"]:'';
$token = $this->_token;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function valid($return=false){
$echostr=isset($_GET['echostr'])?$_GET['echostr']:'';
if($return){
if($echostr){
if($this->checkSignature()){
return $echostr;
}else{
return false;
}
}else{
return $this->checkSignature();
}
}else{
if($echostr){
if($this->checkSignature()){
die($echostr);
}else{
die('no access');
}
}else{
if($this->checkSignature()){
return true;
}else{
die('no access');
}
}
}
}
}
?>
代码如下:wechat.class.php
public function __construct($options){
$this->_token=isset($options['token'])?$options['token']:'';
}
public function test(){
var_dump($this->_token);
}
private function checkSignature(){
$signature = isset($_GET["signature"])?$_GET["signature"]:'';
$timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:'';
$nonce = isset($_GET["nonce"])?$_GET["nonce"]:'';
$token = $this->_token;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function valid($return=false){
$echostr=isset($_GET['echostr'])?$_GET['echostr']:'';
if($return){
if($echostr){
if($this->checkSignature()){
return $echostr;
}else{
return false;
}
}else{
return $this->checkSignature();
}
}else{
if($echostr){
if($this->checkSignature()){
die($echostr);
}else{
die('no access');
}
}else{
if($this->checkSignature()){
return true;
}else{
die('no access');
}
}
}
}
}
?>
wechat.php
public function __construct($options){
$this->_token=isset($options['token'])?$options['token']:'';
}
public function test(){
var_dump($this->_token);
}
private function checkSignature(){
$signature = isset($_GET["signature"])?$_GET["signature"]:'';
$timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:'';
$nonce = isset($_GET["nonce"])?$_GET["nonce"]:'';
$token = $this->_token;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function valid($return=false){
$echostr=isset($_GET['echostr'])?$_GET['echostr']:'';
if($return){
if($echostr){
if($this->checkSignature()){
return $echostr;
}else{
return false;
}
}else{
return $this->checkSignature();
}
}else{
if($echostr){
if($this->checkSignature()){
die($echostr);
}else{
die('no access');
}
}else{
if($this->checkSignature()){
return true;
}else{
die('no access');
}
}
}
}
}
?>
<?php
function __autoload($classname){
include "./wechat/".$classname.".class.php";
}
$options=array(
'token'=>'tudouya',
);
$wechat=new Wechat($options);
$wechat->valid();
?>
另外,我的php版本是5.4。我对这个脚本进行了长时间的测试,发现_autoload()和include在PHP5.2中正常工作,但在PHP5.4中不正常。我甚至认为php中是否存在漏洞,但我无法决定。
希望我能清楚地描述我的问题,并为我糟糕的英语感到抱歉。答案是:
include "./wechat/".strtolower($classname).".class.php";
否则,它将尝试包含Wechat.class.php(大写字母为w,它不作为文件存在。最好将绝对路径与
\uuuu DIR\uuuu
指令一起使用。您的脚本应该运行良好…(我猜路径是正确的…)顺便说一下,我建议您使用spl\u autoload\u register
而不是\u autoload
@KeVin,@Debflav,我尝试了您所说的,它仍然无法工作。我认为这不是问题的关键。也许它不会解决您的问题。但是如果您迁移到PHP5.4,您应该注意:“spl\u autoload\u register()为自动加载类提供了更灵活的选择。因此,不鼓励使用uu autoload(),将来可能会被弃用或删除。“@Debflav,你说得对,谢谢,我会这么做。谢谢,我再次尝试你说的内容,它会起作用。但另一个问题提出。正如你所说,(大写字母w,不作为文件存在),如果是这样的话,为什么我可以新建一个对象而程序不会引发错误?也许你的错误报告没有设置为E\u ALL
,因为尝试实例化未知类会引发致命错误。@Debflav,首先我的错误报告设置为E\u ALL。此外,我可以实例化该类并调用成员函数。甚至,成员函数可以正确运行!@tudouya So,对不起,我误解了你的真正问题。类名是Wechat(大写W),但文件名是class.Wechat.php(小写W)。所以include('class.Wechat.php');(小写W)可以工作,而你必须使用new Wechat();(大写W)来创建类的实例。