Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8312525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:11:39+00:00 2026-06-08T20:11:39+00:00

I am trying to have a script start another script and put its data

  • 0

I am trying to have a script start another script and put its data into a session variable for the other script to use. The problem is that when the second script, data.php, runs it doesn’t seem to be able to access the session variables. They are blank and nothing gets written to data.txt. If I run data.php by itself it writes the last value that $_SESSION["data"] was set to properly, but not when it’s run with exec. I am not sure what the problem is. Any ideas?

input.php:

session_start();
$_SESSION["data"] = "Data!";
exec("/usr/bin/php /path/to/data.php > /dev/null 2>&1 &");

data.php:

session_start();
$fp = fopen('data.txt', 'w');
fwrite($fp, $_SESSION["data"]);
fclose($fp);

Edit: I am trying to start data.php from inside input.php and have the variables from input.php accessible in data.php.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:11:41+00:00Added an answer on June 8, 2026 at 8:11 pm

    You can pass data to PHP scripts running with the CLI as command line arguments. This data will be available to the child script in the $argv array.

    input.php:

    $arg = "Data!";
    exec("/usr/bin/php /path/to/data.php ".escapeshellarg($arg)." > /dev/null 2>&1 &");
    

    data.php

    $fp = fopen('data.txt', 'w');
    fwrite($fp, $argv[1]);
    fclose($fp);
    

    A couple of notes:

    • It is important to pass each argument through escapeshellarg() to ensure that users are not able inject commands into your shell. This will also stop special shell characters in arguments from breaking your scripts.
    • $argv is a global variable, not a superglobal like $_GET and $_POST. It is only available in the global scope. If you need to access it in a function scope, you can use $GLOBALS['argv']. This is about the only situation in which I consider the use of $GLOBALS acceptable, although it is still better to handle the arguments in the global scope on startup, and pass them through the scopes as arguments.
    • $argv is a 0-indexed array, but the first “argument” is in $argv[1]. $argv[0] always contains the path to the currently executing script, because $argv actually represents the arguments passed to the PHP binary, of which the path to your script is the first.
    • Values from command line arguments always have a string type. PHP is very promiscuous with its typing so with scalar values this doesn’t matter, but you (fairly obviously) can’t pass vector types (objects, arrays, resources) through the command line. It is possible to pass objects and arrays by encoding them with e.g. serialize() or json_encode(). There is no way to pass resources through the command line.

    EDIT When passing vector types I prefer to use serialize() because it carries with it information about the classes that objects belong to.

    Here is an example:

    input.php:

    $arg = array(
      'I\'m',
      'a',
      'vector',
      'type'
    );
    exec("/usr/bin/php /path/to/data.php ".escapeshellarg(serialize($arg))." > /dev/null 2>&1 &");
    

    data.php

    $arg = unserialize($argv[1]);
    $fp = fopen('data.txt', 'w');
    foreach ($arg as $val) {
      fwrite($fp, "$val\n");
    }
    fclose($fp);
    

    Here is a couple of functions from my clip collection I use to simplify this process:

    // In the parent script call this to start the child
    // This function returns the PID of the forked process as an integer
    function exec_php_async ($scriptPath, $args = array()) {
      $cmd = "php ".escapeshellarg($scriptPath);
      foreach ($args as $arg) {
        $cmd .= ' '.escapeshellarg(serialize($arg));
      }
      $cmd .= ' > /dev/null 2>&1 & echo $$';
      return (int) trim(exec($cmd));
    }
    
    // At the top of the child script call this function to parse the arguments
    // Returns an array of parsed arguments converted to their correct types
    function parse_serialized_argv ($argv) {
      $temp = array($argv[0]);
      for ($i = 1; isset($argv[$i]); $i++) {
        $temp[$i] = unserialize($argv[$i]);
      }
      return $temp;
    }
    

    If you need to pass a large amount of data (larger than the output of getconf ARG_MAX bytes) you should dump the serialized data to a file and pass the path to the file as a command line argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run a vb script using process.start() as another user that have
I'm trying to start perfmon and another program that have GUI's through a python
I have been trying to make an init script using start-stop-daemon. I am stuck
I'm trying to have a script automatically transform an xml file into several html
I have a script that I'm trying to get working. Basically, what I'm trying
I have a python script that is trying to create a directory tree dynamically
i have a short script where i'm trying to grab data from a URL
I have a batch script that outputs a file, and I'm trying to ensure
I have a background script that is responsible for getting and setting data to
I am trying to write a batch script that combines several css files into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.