This is a statement used in a function named 'handlejoin' in my api which lets users in my app join a chat.
$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']));
I want to pass the $token
value to a new file. I want to do this using an include statement like this.
<?php
include: 'api.php'
echo "$token";
?>
The only thing is that the handleJoin statement is in a file (api.php) that has a lot of code and I'm not yet sure how to comprehend the current variable scope of the $token
variable enough to successfully pass its value to a new file. There seem to be a lot of nuances of the include statement pertaining to variable definitions etc. Can someone help me parse the api/php file in its entirety to learn how to successfully pass the $token
variable's contents to another file?
Source: http://php.net/manual/en/function.include.php
Here is the whole file: https://github.com/tonycrencren/api-file/blob/master/api.php
The handleJoin statement is on line 192
Edit: more info
My app uses push notifications. The $token variable I want to pass stores the device token. I can echo out the device token in the NSLog like this.
- (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);
}
Output in NSLog
The device token also gets posted to the database when the join function is called. Note that its the same device token.
testfile.php
<?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";
?>
Question: how can I make a new file and successfully echo the $token
variable from api.php out in the new file using an include statement? I want to be able to see the device token string when I open the new file in my web browser. Right now its a blank web page. I get the hunch that the new file isn't getting the variable at all.
I also get the hunch that even if it is getting the $token
value, I'd have to run the app simultaneously so that method is called and also keep refreshing the new web page in order to see the token value. Correct?
Since the included file is executed as if it was part of the caller file, just create a global variable within the api.php file and make sute it is set, e.g. call the token()
function within the api.php.
api.php
//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();
You can use your testfile.php with the above code.
However, I would not do it this way. I would create the generate_token() function within the api.php that returns the token. In the test php you include the api.php and call the function:
api2.php:
//function that generates the token
function generate_token() {
...
return ...; //returns the token
}
//end api2.php
test.php:
include api2.php;
echo generate_token();