2013-08-28 61 views
1

我的问题是php的responseStatusCodes没有显示在iOS上。无论我在sendResponse()中返回的状态码是什么,当在iOS中读取responseStatusCode时,我都会得到输出500.为什么是这样以及如何修复它?我假设这是PHP的错误,并且与ASIHTTPRequest无关。但为了好的衡量,我已经包含了iOS端使用的代码。ASIHTTPRequest和PHP状态码总是返回200或500

让我知道如果他们是你需要帮助我的任何其他代码。

在此先感谢!

这是我用来启动到服务器的连接的代码。

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
request.shouldAttemptPersistentConnection = NO; 
[request setPostValue:name forKey:@"name"]; 
[request setPostValue:email forKey:@"email"]; 
[request setPostValue:phash forKey:@"phash"]; 
[request setDelegate:self]; 
[request startAsynchronous]; 

这是我在收到请求完成消息时运行的代码。

/ 
- (void)requestFinished:(ASIHTTPRequest *)request 
{ 
    NSLog(@"Request Finished"); 
    int statusCode = [request responseStatusCode]; 
    if (statusCode == 150) { 
     NSLog(@"User Already Registered."); 
    } else if (statusCode == 155){ 
     NSLog(@"User Not Registered"); 
    } else if (statusCode == 403) { 
     NSLog(@"Tester 2"); 
    } else if (statusCode == 200) { 
     if (registering == true){ 
     [UIView animateWithDuration:1.0 animations:^{ 
      _wheel.alpha = 0.0; 
     }]; 
     [UIView animateWithDuration:1.0 delay:1.0 options:0 animations:^{ 
      _clickToRegister.alpha=1.0; 
     }completion:nil]; 
     } else { 
      [UIView animateWithDuration:1.0 animations:^{ 
       _wheel.alpha = 0.0; 
      }]; 
      [UIView animateWithDuration:1.0 delay:1.0 options:0 animations:^{ 
       _clickToLogin.alpha=1.0; 
      }completion:nil]; 
     } 
     NSLog(@"Tester 3"); 
    } else if (statusCode > 1 && statusCode < 1000){ 
     NSLog(@"Test Worked"); 
     NSLog(@"%d",statusCode); 
    } else { 
     NSLog(@"%d",statusCode); 
     NSLog(@"Tester 4"); 
    } 
} 

这是应该发送responseStatusCode的PHP代码。

function sendResponse($status, $body = '', $content_type = 'text/html') 
{ 
    $status_header = 'HTTP/1.1 ' . $status . ' ' . 'error'; 
    header($status_header); 
    header('Content-type: ' . $content_type); 
    echo $body; 
} 

这里是调用这个函数的代码。

sendResponse(503, 'User not Registered'); 
return false; 

以下是其中包含sendResponse调用的整个文件的代码。

<?php 
require_once 'includes/main.php'; 
class dumb { 
function dumber(){ 
    echo "Hello, PHP!"; 
/*-------------------------------------------------- 
    Handle visits with a login token. If it is 
    valid, log the person in. 
---------------------------------------------------*/ 


if(isset($_GET['tkn'])){ 

    // Is this a valid login token? 
    $user = User::findByToken($_GET['tkn']); 

    if($user){ 

     // Yes! Login the user and redirect to the protected page. 

     $user->login(); 
     redirect('panic://success'); 
    } 

    // Invalid token. Redirect back to the login form. 
    redirect('panic://fail'); 
} 



/*-------------------------------------------------- 
    Handle logging out of the system. The logout 
    link in protected.php leads here. 
---------------------------------------------------*/ 


if(isset($_GET['logout'])){ 

    $user = new User(); 

    if($user->loggedIn()){ 
     $user->logout(); 
    } 

    redirect('index.php'); 
} 


/*-------------------------------------------------- 
    Don't show the login page to already 
    logged-in users. 
---------------------------------------------------*/ 


$user = new User(); 

if($user->loggedIn()){ 
    redirect('protected.php'); 
} 



/*-------------------------------------------------- 
    Handle submitting the login form via AJAX 
---------------------------------------------------*/ 

     if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["phash"])){ 
      rate_limit($_SERVER['REMOTE_ADDR']); 

      rate_limit_tick($_SERVER['REMOTE_ADDR'], $_POST['email']); 

      $message = ''; 
      $name = $_POST["name"]; 
      $email = $_POST["email"]; 
      $phash = $_POST["phash"]; 
      $subject = 'Your Login Link'; 

      if(!User::exists($email)){ 
       $subject = "Thank You for Registering!"; 
       $message = "Thank you for registering at our site!\n\n"; 
       // Attempt to login or register the person 
      $user = User::loginOrRegister($email, $name, $phash); 


      $message.= "You can login from this URL:\n"; 
      $message.= get_page_url()."?tkn=".$user->generateToken()."\n\n"; 

      $message.= "The link is going expire automatically after 10 minutes."; 

      $result = send_email($fromEmail, $_POST['email'], $subject, $message); 

      if(!$result){ 
      sendResponse(403, 'Error Sending Email'); 
      return false; 
      } 
      } 
      else{ 
       sendResponse(150, 'User Already Registered'); 
       return false; 
      } 
     } 
     else if(isset($_POST["email"]) && isset($_POST["phash"])){ 
      rate_limit($_SERVER['REMOTE_ADDR']); 

      rate_limit_tick($_SERVER['REMOTE_ADDR'], $_POST['email']); 

      $message = ''; 
      $name = ''; 
      $email = $_POST["email"]; 
      $phash = $_POST["phash"]; 
      $subject = 'Your Login Link'; 

      if(!User::exists($email)){ 
       sendResponse(155, 'User Not Registered'); 
       return false; 
      } 
      else{ 
      // Attempt to login or register the person 
      $user = User::loginOrRegister($email, $name, $phash); 


      $message.= "You can login from this URL:\n"; 
      $message.= get_page_url()."?tkn=".$user->generateToken()."\n\n"; 

      $message.= "The link is going expire automatically after 10 minutes."; 

      $result = send_email($fromEmail, $_POST['email'], $subject, $message); 

      if(!$result){ 
      sendResponse(403, 'Error Sending Email'); 
      return false; 
      } 
     } 
     } 

     die(json_encode(array(
      'message' => 'Thank you! We\'ve sent a link to your inbox. Check your spam folder as well.' 
     ))); 

/*-------------------------------------------------- 
    Output the login form 
---------------------------------------------------*/ 
} 
} 
$api = new dumb; 
$api->dumber(); 
?> 
+0

嗯,我试了PHP代码,它返回503.从一眼看来,它看起来像iOS代码是正确的。你确定PHP正在运行'sendResponse(503,'未注册用户');'?你可以在调用该行之前回应一些信息,以验证它是否被调用,并且由于之前出现了编码错误,PHP不会将它传递给其他人以返回其他内容。这是我犯的一个常见错误。 –

+0

如果我回应一些事情,那会有帮助吗?我如何查看回应的内容? –

+0

嗯,我确定'sendResponse(503 ...'在if语句中,对吗?“if($ userNotRegistered){sendResponse(503 ...)'如果在'sendResponse'被调用之前打印一些东西,你可以肯定的是,503被设置为PHP的状态码,设置503的PHP代码是正确的,并且iOS似乎是正确的,所以它必须是PHP文件另一部分的编码错误,必须把'echo“输出503”;'sendResponse'前面,然后检查iOS中的响应字符串,如果你给我这个网站的URL,我可以检查我自己 –

回答

0

我有一个语法错误。 A /关闭php标签后:O。对不起

相关问题