0

I'm pretending to pass a COOKIE_FILE to another script because I'm using php Curl to make some petitions and I need some of the SESSION variables there, in the main script I have:

       define("COOKIE_FILE", "cookie.txt");
        $options = array(
            CURLOPT_HEADER => false,
            CURLOPT_TIMEOUT => 40000
        );

        $params=['Variable1'=>$initial, 'Variable2'=>$this->V2];
        $defaults = array(
            CURLOPT_URL => 'localhost/socket/src/myApp/socket2.php',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($params),
            CURLOPT_COOKIEJAR => COOKIE_FILE,
            CURLOPT_COOKIEFILE => COOKIE_FILE,
            );
        $ch = curl_init();
        curl_setopt_array($ch, ($options + $defaults));
        curl_exec($ch);

This is the complete code of the script that calls socket2:

<?php
namespace MyApp;
use funciones;
use Agente;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

define("COOKIE_FILE", "cookie.txt");
$_SESSION["running"] = time();

class Chat implements MessageComponentInterface {
    protected $clients;
    protected $data_clients;
    protected $target = "999999"; 


    public function __construct() {
        $this->clients = new \SplObjectStorage;
        $this->data_clients = new \SplObjectStorage;
        $this->send_message('testing from php');
        $this->Listen(true);
    }

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);
        echo "New connection! ({$conn->resourceId})\n";
    }


    public function Listen($initial){
       $band=false;

       $options = array(
            CURLOPT_HEADER => true,
            CURLOPT_TIMEOUT => 40000
        );

        $params=['initial'=>$initial, 'target'=>$this->target'];
        $defaults = array(
            CURLOPT_URL => 'localhost/socket/src/myApp/socket2.php',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($params),
            CURLOPT_COOKIEJAR => COOKIE_FILE,
            CURLOPT_COOKIEFILE => COOKIE_FILE,
            );

        //Tell cUrl about the cookie file
        //curl_setopt($ch,CURLOPT_COOKIEJAR, $this->cookieFile);  //tell cUrl where to write cookie data
        //curl_setopt($ch,CURLOPT_COOKIEFILE, $this->cookieFile); //tell cUrl where to read cookie data from

        $ch = curl_init();
        curl_setopt_array($ch, ($options + $defaults));
        curl_exec($ch);

        if(curl_errno($ch)){
            $band=true;
        }
        curl_close($ch);

        if(!$band)
            $this->Listen(false);
    }//fin Listen

    public function get_messages(){

        $band=false;
        $options = array(
            CURLOPT_HEADER => false,
            CURLOPT_TIMEOUT => 1000
        );

        $params=['method'=>'pollMessages'];
        $defaults = array(
            CURLOPT_URL => 'localhost/socket/src/myApp/ajax2.php',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($params),
        );
        $ch = curl_init();
        curl_setopt_array($ch, ($options + $defaults));
        curl_exec($ch);

        if(curl_errno($ch)){
            $band=true;
        }
        curl_close($ch);

        if(!$band)
            $this->get_messages();
    }//fin de get messages

 public function send_message($mensaje){

        $band=false;
        $options = array(
            CURLOPT_HEADER => false,
        );

        $params=['method'=>'sendMessage','target'=>$this->target,'message'=>$mensaje];
        $defaults = array(
            CURLOPT_URL => 'localhost/socket/src/myApp/ajax2.php',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($params),
        );
        $ch = curl_init();
        curl_setopt_array($ch, ($options + $defaults));
        curl_exec($ch);
      /*  $headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT ); // request headers
    echo $headerSent;*/
        curl_close($ch);

    }//fin de enviar mensaje

in socket2.php I have:

<?php
    require  '../../vendor/autoload.php';
    set_time_limit(10); //1 minute
    session_start();
    session_write_close();

    $time = $_SESSION["running"];
?>

I'm trying to read the variable running But I still get in the other side (socket2.php) Undefined index:running for the SESSION variable that I'm needing. So, What am I missing? How I check that the file cookie.txt is created sucesfully? Thanks

Sredny M Casanova
  • 4,735
  • 21
  • 70
  • 115

0 Answers0