首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用包含语句传递$_POST数据

使用包含语句传递$_POST数据
EN

Stack Overflow用户
提问于 2016-02-03 06:36:51
回答 1查看 285关注 0票数 3

这是api中一个名为“handlejoin”的函数中使用的语句,它允许我的应用程序中的用户加入聊天。

代码语言:javascript
运行
复制
$stmt = $this->pdo->prepare('INSERT INTO active_users (user_Id, device_token, nickname, secret_code, ip_address) VALUES (?, ?, ?, ?, ?)');
$stmt->execute(array($userId, $token, $name, $code, $_SERVER['REMOTE_ADDR']));

我想将$token值传递给一个新文件。我想使用像这样的include语句来完成这个任务。

代码语言:javascript
运行
复制
<?php
include: 'api.php'
echo "$token";
?>

唯一的问题是,handleJoin语句位于一个包含大量代码的文件(api.php)中,我还不知道如何理解$token变量的当前变量范围,以便成功地将其值传递给一个新文件。对于变量定义等,包含语句似乎有很多细微差别。有人能帮我解析整个api/php文件,学习如何成功地将$token变量的内容传递给另一个文件吗?

来源:http://php.net/manual/en/function.include.php

以下是整个文件:https://github.com/tonycrencren/api-file/blob/master/api.php

handleJoin语句位于第192行

编辑:更多信息

我的应用程序使用推送通知。我想传递的$token变量存储设备令牌。我可以像这样回显NSLog中的设备令牌。

代码语言:javascript
运行
复制
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{ 
NSString* newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];

NSLog(@"My token is: %@", newToken);

}

NSLog输出

当调用join函数时,设备令牌也会被提交到数据库。注意,它是相同的设备令牌。

testfile.php

代码语言:javascript
运行
复制
<?php

//tells you whats going on in the browser at at what line if there needs to be an improvement to the structure of the function

error_reporting(E_ALL);
ini_set("display_errors", 1);


function token()
 {
include 'file://localhost/Users/user/Desktop/PushChatServer/api/api.php';

echo "A $token";

return $token;

}

token();  
echo "$token"; 


?>

问:如何创建一个新文件,并使用$token语句成功地将api.php变量从新文件中回显出来?当我在浏览器中打开新文件时,我希望能够看到设备令牌字符串。现在这是一个空白的网页。我预感到新文件根本没有得到变量。

我还预感到,即使它获得了$token值,我也必须同时运行该应用程序,以便调用该方法,并不断刷新新网页以查看令牌值。对,是这样?

EN

回答 1

Stack Overflow用户

发布于 2016-02-03 09:49:48

由于包含的文件是像调用方文件的一部分一样执行的,所以只需在api.php文件中创建一个全局变量并设置它,例如在api.php中调用token()函数。

api.php

代码语言:javascript
运行
复制
//declare global $token variable
$token='';

//function that generates the token
function generate_token() {
...
}

//use one of the following methods
//code to execute the generate_token() function if generate_token() returns the token
$token=generate_token();

//code to execute the generate_token() function if generate_token() sets the global $token within the function's body
//put a declaration within generate_token() in order to set $token global variable from the function: global $token;
generate_token();

您可以在上面的代码中使用testfile.php。

不过,我不会这样做。我将在返回令牌的generate_token()函数中创建api.php ()函数。在test php中,包含api.php并调用函数:

api2.php 2.php:

代码语言:javascript
运行
复制
//function that generates the token
function generate_token() {
...
return ...; //returns the token
}
//end api2.php

test.php:

代码语言:javascript
运行
复制
include api2.php;
echo generate_token();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35170121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档