PHP + cURL + session id

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lookee

    PHP + cURL + session id

    Hello all,
    I have simple PHP application that on one page strarts session and
    write some information to it. On another page program tryes to fetch
    information from a third page using cURL. On that third page program
    needs to retrive information that were written to the session and use
    them. Problem is that when I call the third page and call
    session_start() it starts a new session where there are no infromation
    I need. Code below.

    <code>
    <?
    /**
    * file: t1.php
    */
    session_start() ;
    $_SESSION['user'] = 'lukas';
    echo '<html>
    <body>
    <a href="t2.php">' .$_SESSION['user'].' 2 >> </a>
    </body>
    </html>';
    ?>

    <?
    /**
    * file: t2.php
    */
    session_start() ;
    $_SESSION['user'].=" add some info.";
    $url="http://www.myserver.co m/t3.php"
    $ch = curl_init();
    curl_setopt($ch , CURLOPT_URL, $url);
    curl_setopt($ch , CURLOPT_TIMEOUT , 2);
    //curl_setopt($ch , CURLOPT_COOKIE,
    session_name(). "=".session_id( ).";"); //when I uncomment that line
    operation times out
    $output = curl_exec($ch);
    echo "output: ".$output." <br/>";
    echo "error no: ".curl_errno($c h)."<br/>";
    echo "error: ".curl_error($c h)."<br/>";
    curl_close($ch) ;
    ?>

    <?
    /**
    * file: t3.php
    */
    session_start() ;
    if(isset($_SESS ION['user']))
    echo "yeah<br/>";
    else
    echo "nope<br/>";
    ?>
    </code>

    My PHP Version 4.4.2-1

    My php.ini session settings:
    Session Support enabled
    Registered save handlers files user
    session.auto_st art Off Off
    session.bug_com pat_42 On On
    session.bug_com pat_warn On On
    session.cache_e xpire 180 180
    session.cache_l imiter nocache nocache
    session.cookie_ domain no value no value
    session.cookie_ lifetime 0 0
    session.cookie_ path / /
    session.cookie_ secure Off Off
    session.entropy _file no value no value
    session.entropy _length 0 0
    session.gc_divi sor 100 100
    session.gc_maxl ifetime 1440 1440
    session.gc_prob ability 0 0
    session.name PHPSESSID PHPSESSID
    session.referer _check no value no value
    session.save_ha ndler files files
    session.save_pa th /var/lib/php4 /var/lib/php4
    session.seriali ze_handler php php
    session.use_coo kies On On
    session.use_onl y_cookies Off Off
    session.use_tra ns_sid Off Off

    My php.ini cURL settings
    CURL support enabled
    CURL Information libcurl/7.15.3 OpenSSL/0.9.8a zlib/1.2.1.1
    libidn/0.5.18

    Any help would be appreciated.

    Kind regards,
    Luke

  • Jiri Fogl

    #2
    Re: PHP + cURL + session id

    Problem is in session identification.
    When you start a session, it gets unique identifier, which is passed to
    your browser (in cookie or, if cookies are off, in GET parameter). Value
    stored in the session is saved on the server and next time you call
    session_start, PHP finds it on the server according to this identifier.

    For the third script the calling client is not your browser, but cURL
    client on the server. However, it doesn't know the session identifier,
    so the session is empty.

    What you need is to pass session identifier to the URL called by cURL in
    second script as a GET parameter.

    lookee wrote:[color=blue]
    > Hello all,
    > I have simple PHP application that on one page strarts session and
    > write some information to it. On another page program tryes to fetch
    > information from a third page using cURL. On that third page program
    > needs to retrive information that were written to the session and use
    > them. Problem is that when I call the third page and call
    > session_start() it starts a new session where there are no infromation
    > I need. Code below.
    >
    > <code>
    > <?
    > /**
    > * file: t1.php
    > */
    > session_start() ;
    > $_SESSION['user'] = 'lukas';
    > echo '<html>
    > <body>
    > <a href="t2.php">' .$_SESSION['user'].' 2 >> </a>
    > </body>
    > </html>';
    > ?>
    >
    > <?
    > /**
    > * file: t2.php
    > */
    > session_start() ;
    > $_SESSION['user'].=" add some info.";
    > $url="http://www.myserver.co m/t3.php"
    > $ch = curl_init();
    > curl_setopt($ch , CURLOPT_URL, $url);
    > curl_setopt($ch , CURLOPT_TIMEOUT , 2);
    > //curl_setopt($ch , CURLOPT_COOKIE,
    > session_name(). "=".session_id( ).";"); //when I uncomment that line
    > operation times out
    > $output = curl_exec($ch);
    > echo "output: ".$output." <br/>";
    > echo "error no: ".curl_errno($c h)."<br/>";
    > echo "error: ".curl_error($c h)."<br/>";
    > curl_close($ch) ;
    > ?>
    >
    > <?
    > /**
    > * file: t3.php
    > */
    > session_start() ;
    > if(isset($_SESS ION['user']))
    > echo "yeah<br/>";
    > else
    > echo "nope<br/>";
    > ?>
    > </code>
    >
    > My PHP Version 4.4.2-1
    >
    > My php.ini session settings:
    > Session Support enabled
    > Registered save handlers files user
    > session.auto_st art Off Off
    > session.bug_com pat_42 On On
    > session.bug_com pat_warn On On
    > session.cache_e xpire 180 180
    > session.cache_l imiter nocache nocache
    > session.cookie_ domain no value no value
    > session.cookie_ lifetime 0 0
    > session.cookie_ path / /
    > session.cookie_ secure Off Off
    > session.entropy _file no value no value
    > session.entropy _length 0 0
    > session.gc_divi sor 100 100
    > session.gc_maxl ifetime 1440 1440
    > session.gc_prob ability 0 0
    > session.name PHPSESSID PHPSESSID
    > session.referer _check no value no value
    > session.save_ha ndler files files
    > session.save_pa th /var/lib/php4 /var/lib/php4
    > session.seriali ze_handler php php
    > session.use_coo kies On On
    > session.use_onl y_cookies Off Off
    > session.use_tra ns_sid Off Off
    >
    > My php.ini cURL settings
    > CURL support enabled
    > CURL Information libcurl/7.15.3 OpenSSL/0.9.8a zlib/1.2.1.1
    > libidn/0.5.18
    >
    > Any help would be appreciated.
    >
    > Kind regards,
    > Luke
    >[/color]

    Comment

    • lookee

      #3
      Re: PHP + cURL + session id

      Thanks for your reply.
      I did what you had suggested and still no luck. I appended
      session_name(). "=".session_id( ) to the URL I wanted to fetch and the
      results were the same as when I set this line in the second script:
      curl_setopt($ch , CURLOPT_COOKIE, session_name(). "=".session_id( ).";");
      "Operation timed out with 0 out of -1 bytes received". If you have any
      other suggestions let me know.

      Thanks.

      Best regards,
      Luke

      Comment

      Working...