SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Sending cookie in cURL request

    Please take a look at the code below:

    PHP Code:
    $method $request$i ][ 'Method' ];
    $url $request$i ][ 'URI' ];

    $curl curl_init'http://' $_SERVER'SERVER_NAME' ] . $url );
    curl_setopt$curlCURLOPT_HEADER);
    //When I uncomment this line, I am not getting response, request timed out.
    //[B]curl_setopt( $curl, CURLOPT_COOKIE, session_name() . '=' .
    $_COOKIE[session_name()] . '; domain=192.168.0.1; path=/' );[/B]
    curl_setopt$curlCURLOPT_HTTPHEADER$request$i ][ 'Headers' ] );

    if ( 
    $method == 'POST' )
    {
        
    curl_setopt$curlCURLOPT_POST);
        
    curl_setopt$curlCURLOPT_POSTFIELDS$request$i ][ 'Content' ] );
    }
    else if ( 
    $method != 'GET' )
    {
        
    curl_setopt$curlCURLOPT_CUSTOMREQUEST$method );
        
    curl_setopt$curlCURLOPT_POSTFIELDS$request$i ][ 'Content' ] );
    }

    curl_setopt$curlCURLOPT_RETURNTRANSFER);
    $strResponse curl_exec$curl ); 

    When I uncomment the line
    PHP Code:
    curl_setopt$curlCURLOPT_COOKIEsession_name() . '=' .
    $_COOKIE[session_name()] . '; domain=192.168.0.1; path=/' ); 
    I am not getting response, Timed out.

    When I observed normal Ajax requests, a header is sending as:

    Cookie PHPSESSID=goio5r15cqost5hvu64u7p28k0
    So, I tried adding
    PHP Code:
    header("Cookie: " session_name() . '=' $_COOKIE[session_name()]); 
    Again, timed out.

  2. #2
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,321
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    don't know but might this help?:

    Code:
    # Location of your cookie file. (Must be fully resolved local address)
    define("COOKIE_FILE", "c:\cookie.txt");
    
    ...
    
    	curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);   // Cookie management.
    	curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
    
    ...
    taken from the "LIB_http.php" file available from here: http://www.schrenk.com/nostarch/webb...P_download.php

  3. #3
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My doubt is, how can I get the cookie file from client?

    How can I create cookie file on the server to send in cURL requests?

    I tried this also. Created a file with content

    PHPSESSID=q42abapevipjtttnfom10va5a2; path=/; domain=localhost
    and I sent it in the cURL request. But the same result, timed out.

  4. #4
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    When I try:

    PHP Code:
    $strCookie session_name() . '=' $_COOKIEsession_name() ] . '; path=/';
    curl_setopt$curlCURLOPT_COOKIE'PHPSESSID=7b8kv5u75rg8io3mm3lcenljd1; path:/' );//Current session ID 
    I am able to get data.

    But when I use:
    PHP Code:
    $strCookie session_name() . '=' $_COOKIEsession_name() ] . '; path=/';
    curl_setopt$curlCURLOPT_COOKIE$strCookie ); 
    it is not working. In both the cases, I am sending a string. But why it is not working?

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Pretty sure you should be using...

    PHP Code:
    $strCookie 'PHPSESSID=' session_name() . '; path=/';
    curl_setopt$curlCURLOPT_COOKIE$strCookie ); 
    SilverB.

  6. #6
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No.

    I am using:

    PHP Code:
    $strCookie session_name() . '=' $_COOKIEsession_name() ] . '; path=/';

    curl_setopt$curlCURLOPT_COOKIE$strCookie ); 
    Value in $strCookie is:
    PHPSESSID=jpo09e37uikmqqth3rkd38v3s2; path=/

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Echo'ing $strCoookie will output 'PHPSESSID=CURRENT_SESS_ID; path=/'.

    Isn't this what you want passed to cURL?

    SilverB.

  8. #8
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Exactly.

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    So why doesn't my code work? Are you ensuring you're starting the session too with session_start() ?

    I don't understand while you're using the $_COOKIE super global.

    SilverB.

  10. #10
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    If I have not started the session, how can I get the $strCookie value???

    Quote Originally Posted by ArunB View Post

    Value in $strCookie is:

    PHPSESSID=jpo09e37uikmqqth3rkd38v3s2; path=/

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Before you can access any session vars you needs to have a valid session, a session can be started with session_start();

    PHP Code:
    session_start();
    $strCookie 'PHPSESSID=' session_id() . '; path=/';
    curl_setopt$curlCURLOPT_COOKIE$strCookie ); 
    Sorry, but if this doesn't help, I'm going to need you to explain what exactly you are trying to do here...

    It's just not sinking in.

    SilverB.

  12. #12
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  13. #13
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ArunB View Post
    When I try:

    PHP Code:
    $strCookie session_name() . '=' $_COOKIEsession_name() ] . '; path=/';
    curl_setopt$curlCURLOPT_COOKIE'PHPSESSID=7b8kv5u75rg8io3mm3lcenljd1; path:/' );//Current session ID 
    I am able to get data.

    But when I use:
    PHP Code:
    $strCookie session_name() . '=' $_COOKIEsession_name() ] . '; path=/';
    curl_setopt$curlCURLOPT_COOKIE$strCookie ); 
    it is not working. In both the cases, I am sending a string. But why it is not working?
    Well, if that works what about...

    PHP Code:
    <?php
    $strCookie 
    'PHPSESSID=' $_COOKIE['PHPSESSID'] . '; path=/';
    curl_setopt$curlCURLOPT_COOKIE$strCookie );
    ?>
    SilverB.

  14. #14
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is also not working.

  15. #15
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Only a single script may access session data at a time. php puts an exclusive lock on the session file. Since your curl script is waiting on the script it's calling via url to access session data, it waits forever because the script called via the url is waiting for the curl script to close the session, so that it may open it. This obviously never happens.

    call session_write_close() before sending the request.

  16. #16
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey!

    That's working fine.............

    Thank you very much.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •