2015-08-21 14 views
1
<?php 

define('PATH', dirname(dirname(__FILE__)).'/'); 
require_once (PATH.'./wp-blog-header.php'); 
global $wpdb; 




// if(!isset($_POST['username'])){ 
// //echo $_POST['submit']; 
// exit('非法訪問!'); 

// } 

$username = $_POST["username"]; 
$password = $_POST["password"]; 
$email  = $_POST["email"]; 
$checkcode = $_POST["checkcode"]; 



$query_pwd_str = "SELECT password FROM yjhy_users_custom WHERE username=%s"; 


if($wpdb == null) 
    echo "wpdb is null"; 

$pwd_query_result = $wpdb->get_results($wpdb->prepare($query_pwd_str, $username)); 





$pwd_query_result_count = count($pwd_query_result); 

if($pwd_query_result_count == 1){ 

    //已經存在該用戶名,返回數據 
    // error here 
    header('Content-type:text/json'); 
    $json_ouput = '{"status":"success","errormsg":"'.$username.'"}'; 
    echo $json_ouput; 


} 


?> 

通過上面的代碼獲得404錯誤,我用的方法post指PHP文件,它總是與404錯誤的回覆。爲什麼當PHP回聲位置改變

但是,當我修改如下PHP代碼:

<?php 
    //****************** move the function header() here 
    header('Content-type:text/json'); 
    //****************** echo "{" here 
    echo "{"; 

?> 
<?php 

define('PATH', dirname(dirname(__FILE__)).'/'); 
require_once (PATH.'./wp-blog-header.php'); 
global $wpdb; 




// if(!isset($_POST['username'])){ 
// //echo $_POST['submit']; 
// exit('非法訪問!'); 

// } 

$username = $_POST["username"]; 
$password = $_POST["password"]; 
$email  = $_POST["email"]; 
$checkcode = $_POST["checkcode"]; 



$query_pwd_str = "SELECT password FROM yjhy_users_custom WHERE username=%s"; 


if($wpdb == null) 
    echo "wpdb is null"; 

$pwd_query_result = $wpdb->get_results($wpdb->prepare($query_pwd_str, $username)); 





$pwd_query_result_count = count($pwd_query_result); 

if($pwd_query_result_count == 1){ 

    //已經存在該用戶名,返回數據 
    // error here 

    $json_ouput = '"status":"success","errormsg":"'.$username.'"'; 
    echo $json_ouput; 


} 


?> 

<?php 
    //****************** echo "}" here 
    echo "}"; 

?> 

,然後它的工作原理,服務器響應狀態是200!

我對這個問題感到困惑,並且一整天都在搜索,找不到答案!

爲什麼代碼是這樣工作的?

+0

什麼是'require_once的內容(PATH。 '/可溼性粉劑博客 - header.php文件');'?它顯示的東西或簡單的PHP代碼沒有輸出? – zeflex

+0

require_once(PATH。'。/ wp-blog-header.php'); 我使用WordPress框架,並使用WP提供的$ wpdb對象,所以需要該行。 @zeflex –

+0

好的,但這個文件的內容是什麼?它是否也顯示HTML代碼? – zeflex

回答

1

用了1天的時間,我發現了! 我直接請求與文件路徑http://localhost:7770/api/register.php的鏈接,而不是通過在儀表板中創建page來請求鏈接,因此該鏈接不在WP的數據庫中。 和WP框架需要在require_once (PATH.'./wp-blog-header.php');執行wp();時初始化本身,而是當初始操作的文件/wp-includes/class-wp.php在執行與下面的代碼:

public function main($query_args = '') { 
     $this->init(); 
     **$this->parse_request($query_args);** 
     $this->send_headers(); 
     $this->query_posts(); 
     $this->handle_404(); 
     $this->register_globals(); 
     do_action_ref_array('wp', array(&$this)); 
    } 

} 

聲明$this->query_posts();需要檢查鏈接$this->parse_request();。在FUNC parse_request()

public function parse_request($extra_query_vars = '') { 
     global $wp_rewrite; 

     /** 
     * Filter whether to parse the request. 
     * 
     * @since 3.5.0 
     * 
     * @param bool   $bool    Whether or not to parse the request. Default true. 
     * @param WP   $this    Current WordPress environment instance. 
     * @param array|string $extra_query_vars Extra passed query variables. 
     */ 
     if (! apply_filters('do_parse_request', true, $this, $extra_query_vars)) 
      return; 

     $this->query_vars = array(); 
     $post_type_query_vars = array(); 

     if (is_array($extra_query_vars)) { 
      $this->extra_query_vars = & $extra_query_vars; 
     } elseif (! empty($extra_query_vars)) { 
      parse_str($extra_query_vars, $this->extra_query_vars); 
     } 
     // Process PATH_INFO, REQUEST_URI, and 404 for permalinks. 

     // Fetch the rewrite rules. 
     $rewrite = $wp_rewrite->wp_rewrite_rules(); 

     if (! empty($rewrite)) { 
      // If we match a rewrite rule, this will be cleared. 

      ***$error = '404';*** 
      $this->did_permalink = true; 

      $pathinfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : ''; 
      list($pathinfo) = explode('?', $pathinfo); 
      $pathinfo = str_replace("%", "%25", $pathinfo); 

      list($req_uri) = explode('?', $_SERVER['REQUEST_URI']); 
      $self = $_SERVER['PHP_SELF']; 
      $home_path = trim(parse_url(home_url(), PHP_URL_PATH), '/'); 

和鏈接是不是在數據庫中,所以WP設置錯誤404這裏下面:

然後我得到了***$error = '404';***error 404

解決方案:

  1. 創建儀表板裏的page
  2. 修改page你想要的網址。
  3. 把你的代碼放在page template
  4. page中使用this template
  5. 和Ok
相關問題