gpt4 book ai didi

php - 非常奇怪的 iOS/PHP - 'if' 语句适用于 iOS 但不适用于浏览器

转载 作者:行者123 更新时间:2023-11-28 20:21:42 24 4
gpt4 key购买 nike

我在这里很困惑。基本上,我有一个 iOS 应用程序可以将字符串上传到我的蓝色主机服务器上的 .php 文件。

-(IBAction)plus{
if (counter>=0 && counter<240) {
counter=counter+1;
count.text = [NSString stringWithFormat:@"%i",counter];

NSString* myString = [NSString stringWithFormat:@"%d",counter];

NSString *receipt1 = @"This should work?";

NSString *post =[NSString stringWithFormat:@"receipt=%@",receipt1];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL
URLWithString:@"http://www.ryanmediaworks.com/validation.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-
Type"];
[request setHTTPBody:postData];

NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);

if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 600)
{
NSLog(@"Response: %@", result);
}

}

}

这是我的 .php 文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>I hope this works!</title>
</head>
<body>
<?php

if(_POST)
{
if($_POST['receipt'] == 'This should work?')
{
echo "post successfull";
}
else
{
echo "not post";
}
}
?>
</body>
</html>

这是奇怪的部分......这是我浏览器上的内容:

<head>
<title>I hope this works!</title>
</head>
<body>
not post</body>
</html>

但这是 Xcode 中的输出:

<head>
<title>I hope this works!</title>
</head>
<body>
post successfull</body>
</html>

“if”语句似乎适用于 xcode 但不适用于浏览器?换句话说,正确的字符串被上传到 .php,因此返回了短语“post sucessfull”。但是,在浏览器中查看时该短语不存在?

我真的很感激任何帮助!

最佳答案

看起来很奇怪。试试这个

if(isset($_POST['receipt'])
{
if($_POST['receipt'] == 'This should work?')
{
echo "post successfull";
}
else
{
echo "not post";
}
}
?>

此外,当您检查浏览器时,是否是在您从同一浏览器发出发布请求之后(例如通过表单 POST)?

您的 iOS 代码正在执行一个帖子,这就是它返回成功的原因。如果您只是加载 .php 文件而没有在同一设备/浏览器上对该 .php 页面进行任何类型的发布,它将显示“未发布”。

关于php - 非常奇怪的 iOS/PHP - 'if' 语句适用于 iOS 但不适用于浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15643642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com