Curl PHP and ajax remote login prob :(

Discussion in 'PHP' started by cornetofreak, May 31, 2008.

  1. #1
    hey doods i have an issuesbeen stuck on this for days now really could use ur help

    im trying to login a site using curl then retrieve contents that are only visable once logged in

    the prog is ajax (i think) i just cant get it to login :) please help

    my code

    <?php
    $url = "url here";
    function file_get_the_contents($url) {
      $ch = curl_init();
      $timeout = 40;
      curl_setopt ($ch, CURLOPT_URL, $url);
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt ($ch, CURLOPT_POST, 1);
      curl_setopt ($ch, CURLOPT_HEADER, 1);
      curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt ($ch, CURLOPT_COOKIEJAR, "/tmp/cookie");
      curl_setopt ($ch, CURLOPT_POSTFIELDS,
    "login_name=username&login_password=password&login"); 
      curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
      $file_contents = curl_exec($ch);
      curl_close($ch);
      return $file_contents;
    }
    
    $text = file_get_the_contents($url) ;
    preg_match ('/<td align="left" valign="top" class="news">(.*)<center><\/center><br><\/td>/is', $text, $temp);
    
    preg_match ('/<span class="category"><strong><a href="(.*?)">(.*)<\/a><\/strong><\/span>/is', $text, $cat);
    preg_match ('/<span class="ntitle">(.*)<\/span>/',$text,$name); 
    
    PHP:
    ......... NOW THE POST FORM .........

    
                                <tr>
                                  <td height="122" align="right" valign="top">
                                    <form method="post" onsubmit="javascript:showBusyLayer()" action=''>
                                      <table width="170" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                          <td align="left" class="ltitle_small">Username:</td>
                                          <td width="5" height="18" align="left">&nbsp;</td>
                                          <td align="right"><input name="login_name" type="text" class="a_field" style="width:90px" /></td>
                                        </tr>
                                        <tr>
                                          <td colspan="3" align="left"><img src="{THEME}/images/spacer.gif" width="1" height="3" /></td>
                                        </tr>
                                        <tr>
                                          <td align="left" class="ltitle_small">Password:</td>
                                          <td width="5" height="18" align="left">&nbsp;</td>
                                          <td align="right"><input name="login_password" type="password" class="a_field" style="width:90px" /></td>
                                        </tr>
                                        <tr>
                                          <td colspan="3" align="left"><img src="{THEME}/images/spacer.gif" width="1" height="4" /></td>
                                        </tr>
                                        <tr>
                                          <td align="left">&nbsp;</td>
                                          <td width="5" height="18" align="left">&nbsp;</td>
                                          <td align="right"><input onclick="submit();" name="image" type="image" src="{THEME}/images/dlet_bttn_login.gif" style="width:50px; height:18px; border:0" /><input name="login" type="hidden" id="login" value="submit" /></td>
                                        </tr>
    
    HTML:
    ++ REP for help :)
     
    cornetofreak, May 31, 2008 IP
  2. AFitch

    AFitch Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Been a while since I messed with cURL but try to echo $file_contents; it will show you the page as cURL sees it, so you can see what part it is getting stuck on. It will even show you the fields, and if they are getting filled in or not.

    Replace return $file_contents; with echo $file_contents; and let me know what it is showing(or not showing) you.
     
    AFitch, May 31, 2008 IP
  3. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for tip didnt think like that but yea helps a lil, i can see its setting the session but not loggin in heres the header!

    HTTP/1.1 200 OK Date: Sun, 01 Jun 2008 01:44:17 GMT Server: Apache Set-Cookie: PHPSESSID=a41151644d41377ecfeabaa6e437d957; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Set-Cookie: dle_onl_session=a41151644d41377ecfeabaa6e437d957; expires=Mon, 01-Jun-2009 01:44:17 GMT; path=/ Last-Modified: Sat, 31 May 2008 15:44:17 +0000 GMT Transfer-Encoding: chunked Content-Type: text/html

    cheers
     
    cornetofreak, May 31, 2008 IP
  4. AFitch

    AFitch Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Looks like you're probably logged in, but cURL isn't redirecting you.

    Try this:

    
    $post_fields = $this->array_to_http(array(
    			'login_name'	=> 'whatever', // This too
    			'login_password'	=> 'password', // Edit this too
    			'login'		=> 'Log In', // Make sure to edit this part
    		));
    
                   curl_setopt ( $ch, CURLOPT_URL, $url );
    		curl_setopt ( $ch, CURLOPT_POST, true );
    		curl_setopt ( $ch CURLOPT_POSTFIELDS, $post_fields );
    		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    		curl_setopt ( $ch, CURLOPT_HEADER, false );
    		curl_setopt ( $ch, CURLOPT_COOKIE, "/tmp/cookie.tmp" );
    		curl_setopt ( $ch, CURLOPT_COOKIEJAR, "/tmp/cookie.tmp" );
    		curl_setopt ( $ch, CURLOPT_COOKIEFILE, "/tmp/cookie.tmp" );
                    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, "40");
    
    PHP:
    Probably won't fix it, but best I can do with the info you gave me =P
     
    AFitch, May 31, 2008 IP
  5. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    nope its i bugger init im getting an error posted beloe

    Fatal error: Using $this when not in object context in C:\xampp\htdocs\index.php on line 7

    and becouse its ajax you wont be redirected will you seen as its 'on demand' kinda thing?
     
    cornetofreak, May 31, 2008 IP
  6. AFitch

    AFitch Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I completely forgot you said it's AJAX.

    Add
    header( $url );

    Before
    $text = file_get_the_contents($url) ;

    It should hold the session even after refreshing the page, but maybe not.
     
    AFitch, May 31, 2008 IP
  7. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    nope its aint doin nuffin just the same as it was :(:(:(
     
    cornetofreak, May 31, 2008 IP