WP不定义介绍functon函数

如果你的插件中某个函数或者类需要判断当前浏览者是否已经登录到Wordpress,就需要判断用户是否登录Wordpress了,这时候就用到了:

    <?php   
      
    if(is_user_logged_in()){   //This functon!!!!
        //current visitor is  logged in!   
    }else{   
        //current visitor is NOT logged in!   
    }  

is_user_logged_in()这个函数位于wp-includes/pluggable.php,从Wordpress2.0就开始有了。

可是,你会发现:这个函数在Wordpress插件中是不起作用的。浏览器丢出来一条报错信息:

    Fatal error: Call to undefined function is_user_logged_in() 

当你想获取当前已登录Wordpress的用户的用户信息的时候,也会出现报错:

    Fatal error:Call to undefined function wp_get_current_user()  ……
初步分析认为:is_user_logged_in()和wp_get_current_user()出错的根本原因应该是一致的,那就拿前者说事儿,自觉忽略后者。

为什么呢?在Wordpress.org官方的is_user_logged_in()函数的说明页面,没有污泥泵说明这个判断函数不能在插件中使用,但的确是不能使用的。只有一句描述:

    This Conditional Tag checks if the current visitor is logged in. This is a boolean function, meaning it returns either TRUE or FALSE.   

没有任何notice 啊、tip啊之类的。只是在该页面(http://codex.wordpress.org/Function_Reference/is_user_logged_in)的最后污泥泵的related中,有一个:

    Article: Introduction to WordPress conditional functions  (This is a link)

点击那个链接进去,是Wordpress的条件标签(Conditional Tags)综合说明页面,在这个页面上,有这么一句话:

    The Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches.   

可用于你的模板文件以怎么着,人家没说插件的事儿。
如何使用header()

在PHP4(估计现在没有主机商用这么老的版本了)以上的PHP版本中,可以用输出缓存来解决此问题。
    if($_POST['cwp_pm_send']){   
       $time = current_time( ’mysql’, $gmt = 0 );   
        $sql = “insert into $table_name (from_id,from_time,to_id,subject,content)  values (‘”.$data['from_id'].“‘,’”.$time.“‘,’”.$data['to_id'].“‘,’”.$data['subject'].“‘,’”.$data['content'].“‘)”;//The data has been filtered   
      
        if($wpdb->query($sql)){   
          coolwp_alp_message_LastUrl(‘save’);   
        }else{   http://www.zzws.gov.cn/
          cwp_alp_message_sql_error(‘save’);   
        }   
    }//Save END 

下面是函数  coolwp_alp_message_LastUrl(‘save’):

    /**  
    *return: Redirect.  
    *parameter:the action name.  
    *@author:Suifengtec suoling.net  
    */  
    function coolwp_alp_message_LastUrl($str){   
    /*
    php.ini中有一段话:  
    ; Output buffering allows you to send header lines (including cookies) even  
    ; after you send body content, at the price of slowing PHP’s output layer a  
    ; bit.  You can enable output buffering during runtime by calling the output  
    ; buffering functions.  You can also enable output buffering for all files by  
    ; setting this directive to On.  If you wish to limit the size of the buffer  
    ; to a certain size - you can use a maximum number of bytes instead of ’On’, as  
    ; a value for this directive (e.g., output_buffering=4096).  
    output_buffering = Off  
    ,我调试用的php环境的output_buffering是关闭的,output_buffering的作用是缓冲输出,如果是output_buffering=XXXX(比如是4096,那么,缓冲区大小是4096b,),那么就需要ob_end_clean();这一句了。  
    ob_end_flush()或者ob_end_clean()的作用是终止缓冲;  
    ob_flush()的作用是将缓冲输出出去,但是不会终止缓冲,所以在flush()之前使用;  
    如不想使用ob_end_clean()、ob_end_flush()、ob_flush(),就需要把php.ini里的output_buffering设得足够小,例如0,也可以关闭了,也就是output_buffering = Off。  
    注意:  
    ob_flush()和flush()是有区别的:前者是把数据从PHP的缓冲中释放出来,后者是把不在缓冲中的或者说是被释放出来的数据发送到浏览器。所以当缓冲存在的时候,我们必须ob_flush()和flush()同时使用。  
    当然flush()也并不是不可或缺的,你如果决定在有数据输出时,马上输出到浏览器的话,可以上去flush();  
    当你把output_buffering设为0或者Off的时候,连ob_flush()和ob_end_clean()都不需要了.  
    OVER  
    by Suifengtec 2014.02.23 Suoling.net  
    */  
      ob_end_clean();   //清空(擦除)缓冲区并关闭输出缓冲。此函数丢弃最顶层输出缓冲区的内容并关闭这个缓冲区。如果想要进一步处理缓冲区的内容,必须在ob_end_clean()之前调用ob_get_contents(),因为当调用ob_end_clean()时缓冲区内容将被丢弃。
        if(‘save’==$str){   
            header(“Location:”.$_SERVER ['HTTP_REFERER' ].’#send’);   
        }elseif(‘delete‘==$str||’update’==$str){   
         header(“Location:”.$_SERVER ['HTTP_REFERER' ].’#list’);   
        }   
      //正确下面两个函数的顺序是: 先ob_flush, 然后flush
      ob_flush();   //取出PHP buffering中的数据,放入server buffering
      flush();   // 取出Server buffering的数据,放入browser buffering,简单说,就是刷新输出缓冲
    }  
    //ob_start()函数:对于这个函数我现在了解的不是很清楚,因为开启后输出就会不受ob_flush()控制,即使使用ob_flush()和flush(),数据也不能立即输出在浏览器上.现在知道的是,如果output_buffering=Off,即使使用了ob_start(),也是无法将输出数据缓存的,而如果output_buffering=On的话,即使不用ob_start(),输出数据也可以被PHP缓存,所以觉得ob_start比较废,暂时不管他

PHP.net官方用例:

    <?php   
    ob_start();   
    echo ’Text that won\’t get displayed.’;   
    ob_end_clean();   
    ?> 
 
 3、PHP中的output_buffering

上述注释中的output_buffering如果设为0或者Off,输出顺序为:

    echo,print -> server buffering -> browser buffering -> browser display 

如果设置为大于0的一个整型数值(如果设置为On就表示打开无限大的输出缓存,但是浏览器端的输出缓存大小是有限制的,有网友提供的数据是:IE为256Bytes, Chrome与FireFox为1000Bytes),输出顺序将会变为:

    echo,print -> php output_buffring -> server buffering -> browser buffering -> browser display  

 
4、php.ini中的implicit_flush

    On: 表示每次输出(如echo,print)后自动调用flush()函数后,直接输出   
    Off: 与On相反,每次输出后不会调用flush(),需要等到server buffering满了才会输出,但是我们可以用flush()函数代替它,不开启也没关系,反而更加灵活 

5、小结

ob_flush()、flush()和header()的使用上面已经说到了。其它几个相关的:

    void ob_clean ( void ) 

用来丢弃输出缓冲区中的内容,它不会像 ob_end_clean() 函数会销毁输出缓冲区。

    bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] )  

PHP官方释义为此函数将打开输出缓冲。当输出缓冲是活跃的时候,没有输出能从脚本送出(除http标头外),相反输 出的内容被存储在内部缓冲区中。这样看,上面代码中的ob_end_clean();  用这个函数应该更好些。

PHP官方页面:http://www.php.net/manual/zh/function.ob-start.php

虽然是汉字,但是很晦涩。我虽然看了该函数的参数说明,但是还是不太清楚,因为开启ob_start ()后输出就会不受ob_flush()控制,就是使用ob_flush()和flush(),数据也不能立即输出到客户端浏览器上.现在知道的是,如果output_buffering=Off,即使使用了ob_start(),也是无法将输出数据缓存的,而如果output_buffering=On的话,即使不用ob_start(),输出数据也可以被PHP缓存,所以觉得ob_start比较废,暂时不管它。

注意:有些Apache的模块,比如mod_gzip,可能自己进行输出缓存,

这将导致flush()函数产生的结果不会立即被发送到客户端浏览器。

posted @ 2014-03-18 11:14  lanhe  阅读(335)  评论(0编辑  收藏  举报
数据中心