2

Details: I have a php file that runs some sql and retrieve a list of information. I run through a loop of that information and want to call another php page and pass it some parameters from the data I am looping through.

Question: How do I execute another php page from within my php?

What I Have Tried: This is the php code that should be calling the second php page (the while loop should be calling the simplepush.php page for each result I get):

<?php
require_once "../database/config.php";
header("Content-type: application/json");
$sql = "SELECT user_ip_address FROM ft_users";
$res = mssql_query($sql);
if (mssql_num_rows($res)) {

    while ($op = mssql_fetch_assoc($res)) {
        exec('simplepush.php?token = ' . $op . '');
        $arr[] = $op;
    }

    echo json_encode($arr);

    //$op = mssql_fetch_assoc($res);
    //$op['response'] = 200;
} else {
    http_response_code(420);
    $op = array(
        'response' => 420
    );
    echo json_encode($op);
}

mssql_close();
?>

I have tried the following:

include ('simplepush.php?token = '.$op.'');
exec ('simplepush.php?token = '.$op.'');
require ('simplepush.php?token = '.$op.'');
shell_exec ('simplepush.php?token = '.$op.'');
MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
  • did you try this: `include ('simplepush.php?token = '.$op);` what result you get? – kakajan Sep 25 '15 at 17:43
  • When I use that the code in my other php file does not get executed. – MSU_Bulldog Sep 25 '15 at 17:45
  • check this [link](http://stackoverflow.com/questions/8450696/execute-a-php-script-from-another-php-script) and [this](http://stackoverflow.com/questions/2841093/execute-php-file-from-another-php) one – kakajan Sep 25 '15 at 17:46
  • I've actually already seen those links and none of those options are working. You can see where I put I have tried the following: it shows all of those options except for the shell_exec one and it does not work. – MSU_Bulldog Sep 25 '15 at 17:51
  • can you share simplepush.php? maybe your php code is not working – kakajan Sep 25 '15 at 17:55
  • You need to reconsider how you are trying to achieve your requirement. I suggest you to create function in the second php file , then include and then call it from your main php. Anyway, try this to see what is happening after you execute `$result = exec('simplepush.php?token = ' . $op . '');` `var_dump($result); – dejjub-AIS Sep 25 '15 at 18:07

1 Answers1

2

Just do:

include ('simplepush.php');

Now $op will be available in simplepush.php. Consider this example:

//index.php
while ($op = mssql_fetch_assoc($res)) {
    include ('simplepush.php');
    $arr[] = $op;
}

//simplepush.php
print_r($op);

The contents of $op will be output each time through the loop.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Great, it is now calling my other php file. How do I get $op in my other php file, though. I do not understand what you mean by renaming $token. This is how I declare $token..... $deviceToken = trim(strip_tags($_REQUEST['token'])); – MSU_Bulldog Sep 25 '15 at 18:35
  • I beginning to think I need to delete this :-( – AbraCadaver Sep 25 '15 at 18:40
  • I don't think you should. This helped me get it working. I changed my include statement to what you suggested and found a solution after using the print_r($op); – MSU_Bulldog Sep 25 '15 at 19:22