2017-01-08 29 views
1

你好發佈,PHP返回500,僅當與AJAX

我工作的一個網站的兩步認證的一部分,而且喜歡用AJAX後進行檢查。但是,AJAX文章將始終返回500錯誤。當我運行AJAX發佈的代碼時,它似乎工作。這是爲什麼?

ajax.php?行動= startauth

 session_start(); 

     $username = $_SESSION['user']['id']; 
     $auth = htmlentities($_POST['auth']); 

     // Get User Information 
     $get_userdata = mysql_query("SELECT * FROM users WHERE id = '".$username."'"); 
     $userdata = mysql_fetch_assoc($get_userdata); 

     // Get the Secret and save this 
     $secret = $userdata['auth_secret']; 

     // Initiate the Library 
     require_once ('twostep/library/demo/loader.php'); 
     Loader::register('../lib','RobThree\\Auth'); 
     use \RobThree\Auth\TwoFactorAuth; 

     $tfa = new TwoFactorAuth('Worldhotel.nu'); 
     $code = $tfa->getCode($secret); 

     if(!isset($_SESSION['user']['id'])){ 
      exit("1"); 
     }else if(strlen($auth) <= 5 || strlen($auth) >= 7){ 
      exit("2"); 
     }else if($tfa->verifyCode($secret, $auth) === true){ 
      $insertAuth = mysql_query("UPDATE users SET authentication = '1' WHERE id = '".$userdata['id']."' LIMIT 1"); 
      exit("0"); 
     }else if($tfa->verifyCode($secret, $auth) === false){ 
      exit("3"); 
     }else{ 
      exit("4"); 
     } 

general.js

var requestLogin = false; 
$(".submit_auth").on("click", function() { 
    if(requestLogin == false) { 
     requestLogin = true; 

     var auth = $('.authcode').val(); 

     $.ajax({ 
      type: "POST", 
      url: "codemountain/startauth", 
      async: true, 
      data: {"auth": auth}, 
      success: function (data) { 
       requestLogin = false; 

       if (data == "0") { 
        speelWorld.createAlert("Hij klopt. We schakelen Tweestapsverificatie nu in, en herladen je pagina!", "green", 800); 
       } else if (data == "1") { 
        speelWorld.createAlert("Je bent ingelogd.", "red", 800); 
       } else if (data == "2") { 
        speelWorld.createAlert("Deze code is geen 6 nummers lang.", "red", 800); 
       } else if (data == "3") { 
        speelWorld.createAlert("Deze code is niet (meer) geldig. Probeer het opnieuw.", "red", 800); 
       } else { 
        speelWorld.createAlert("Iets gaat hier niet goed. Het spijt ons voor het ongemak.", "red", 800); 
       } 
      } 
     }); 
    } 
}); 

Codemountain被重寫。我已經嘗試了網址,但沒有重寫,但結果相同。我發佈AJAX的東西時不能運行此代碼,或者我做錯了什麼?

UPDATE 我在日誌中發現了以下錯誤:PHP Parse error: syntax error, unexpected 'use' (T_USE) in ajax.php on line 78是以下行:use \RobThree\Auth\TwoFactorAuth;

我是否包括這個錯了嗎?

+0

使用類似Chrome開發工具(網絡選項卡)的東西,你可以發佈AJAX請求的結果(屏幕截圖)嗎? – phirschybar

+0

@phirschybar是的。 http://prntscr.com/dt2b42 – Synthiatic

+0

是你的發源於同一個域的POST嗎? – phirschybar

回答

1

問題是與use \RobThree\Auth\TwoFactorAuth;

這需要在腳本的最外層範圍內宣稱:http://php.net/manual/en/language.namespaces.importing.php

我會把這句話在這個腳本開幕<?php之後。

+0

你好。謝謝。不過,我已經嘗試過了。然後會顯示另一個錯誤。 PHP致命錯誤:require_once():打開所需的失敗'/authenticator/library/demo/loader.php'(include_path ='。.; C:\ php \ pear')'。但是,路徑是正確的。他只是不能讀出來。 – Synthiatic

+0

也許會將require語句移動到腳本的頂部? – phirschybar

+0

我做過了。但是,之後我發現了這個問題,我們正在使用一個Case。刪除PHP開關後,它工作。謝謝你的幫助! – Synthiatic