[Tutorial] Curl Login

This is a discussion on [Tutorial] Curl Login within the PHP forums, part of the Program Your Site category; you need to save it as .php and you upload it on your server....


Go Back   Scriptasy > Program Your Site > PHP
63 Online Register Projects Products Members List Calendar Today's Posts

Reply

 

LinkBack (19) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #11 (permalink)  
Old 07-05-2007, 02:24 AM
[Administrator]
 
Join Date: May 2007
Posts: 383
you need to save it as .php and you upload it on your server.
Reply With Quote
  #12 (permalink)  
Old 07-05-2007, 02:53 AM
Junior Member
 
Join Date: Jul 2007
Posts: 2
can this be uploaded somewhere else or do i need to make my own website to run it?
Reply With Quote
  #13 (permalink)  
Old 07-06-2007, 02:26 PM
Junior Member
 
Join Date: Jul 2007
Posts: 1
Hey,

First, just want to say thanks a million for posting this tutorial. Your site singlehandedly got me going with this stuff and I almost have some really cool/useful internal tools set up.

After about 14 hours straight of workin' on this I've run into a problem. Basically, I have a website where I place orders very very regularly. I have to log in, and then check out on their site. I am trying to do this using CURL and with your guidance I have been able to log in and get to the final stages of checkout.

I'm running into a problem on one of the last pages where I have to submit a new "ship to" address. I'm trying to do this with curl and for some reason it doesn't want to work. I have my $data 100% correct I know because I ended up literally cutting and pasting it from firefox to be extra safe (I can switch to dynamic stuff later but for testing I'm doing it this way).

The message I get is:

Quote:
Object Moved
This object may be found here.
And it looks to my own site.

What is the basic process for logging in to a site with curl and then submitting more data in forms that are only accessible to a logged in user?

And also, is there an easier way to error check? Needless to say, the message I'm getting isn't very descriptive.

I go to a page like "secure/shipto/change_shipto.asp" and then have to click a link that takes me to secure/shipto/add.asp?url=change_shipto.asp.

I've submitted this form manually and used the firefox extension you mention to check the headers and I am sending all the right data now using curl.

HTML Code:
<

form method="post" action="add1.asp" name="new_account1">

	
	<input type=hidden name="url" value="change_shipto.asp">


	<input type=hidden name="acct" value="0020102">

<input type="submit" name="B1" value="Cancel" class="valor_www_button">

 <input type="submit" name="B1" value="Add Ship To">

</form>



There are some input fields within the forum that I had to cut out because they are so long, but that should give the gist of it.

I know I haven't been as clear as I should have, but it's kind of hard when the site actually requires a log in so I can't even show the URL here.

Any help you can provide would be greatly appreciated. I get the problem when submitting the form above.
Reply With Quote
  #14 (permalink)  
Old 07-06-2007, 02:54 PM
[Administrator]
 
Join Date: May 2007
Posts: 383
Quote:
Originally Posted by kkibak View Post
Hey,

First, just want to say thanks a million for posting this tutorial. Your site singlehandedly got me going with this stuff and I almost have some really cool/useful internal tools set up.

After about 14 hours straight of workin' on this I've run into a problem. Basically, I have a website where I place orders very very regularly. I have to log in, and then check out on their site. I am trying to do this using CURL and with your guidance I have been able to log in and get to the final stages of checkout.

I'm running into a problem on one of the last pages where I have to submit a new "ship to" address. I'm trying to do this with curl and for some reason it doesn't want to work. I have my $data 100% correct I know because I ended up literally cutting and pasting it from firefox to be extra safe (I can switch to dynamic stuff later but for testing I'm doing it this way).

The message I get is:



And it looks to my own site.

What is the basic process for logging in to a site with curl and then submitting more data in forms that are only accessible to a logged in user?

And also, is there an easier way to error check? Needless to say, the message I'm getting isn't very descriptive.

I go to a page like "secure/shipto/change_shipto.asp" and then have to click a link that takes me to secure/shipto/add.asp?url=change_shipto.asp.

I've submitted this form manually and used the firefox extension you mention to check the headers and I am sending all the right data now using curl.

HTML Code:
<

form method="post" action="add1.asp" name="new_account1">

    
    


    



 



There are some input fields within the forum that I had to cut out because they are so long, but that should give the gist of it.

I know I haven't been as clear as I should have, but it's kind of hard when the site actually requires a log in so I can't even show the URL here.

Any help you can provide would be greatly appreciated. I get the problem when submitting the form above.
ok so you already logged in but you want to actually send more data?

you can use the login function for that too. the login function doesn't really login it basically sends data to the url, It can be used for many other things.
Reply With Quote
  #15 (permalink)  
Old 07-13-2007, 03:07 PM
Junior Member
 
Join Date: Jul 2007
Posts: 1
Thank you Chad for such a useful functions and support for beginners.

I have 2 main problems now.

1. The login doesnt work.
I was trying to detect the error, and I think it is because the cookie file keeps being empty. I have the test files on a XAMPP directory on my desktop PC, with WinXP SP2, on a NTFS partition. I have Administrative privileges. As you will see, I open 2 files, one for error reporting and the other one for cookies. The error reporting file is being opened without problems by curl, because I give it a handle to an open file. However for cookies I give a filename, so I dont know if curl can write to that file.

2. The images on the target site have relative paths, so they wont show on my browser via curl. I think this is not so difficult to fix, but I am new to curl, so please enlighten me where should I look for a solution.


Besides, I would like to know if it is possible to give curl a variable instead of a filename for cookie options. Because I would like to redirect the cookies to the end user, instead of storing them on the server. Something like this:

SITE ---- [login_cookie]----> PHP/CURL setrawcookie() ---- [login_cookie] ----> END USER



Thank you very much again, I would appreciate a lot if you can give me some advice.

Roman.

Here is my test code:
PHP Code:
<?php
define
(DEBUG_MODETRUE);
error_reporting(E_ALL E_STRICT);


$my_mundo_travian 'http://s1.travian.net';
$my_login_url "$my_mundo_travian/dorf1.php";

/* below the original test page has a string with valid header data copied as
    shown in the tutorial using live http headers */
$my_login_data "";
$my_proxy '';
$my_proxy_status 'off';

/* after a successful login, this url shows the initial game control panel.
    If login fails, it shows the login page */
$my_grab_page 'http://s1.travian.net/dorf1.php';

$fp_err fopen("curl_errors.txt""w");
curl_login($my_login_url$my_login_data$my_proxy$my_proxy_status);
echo 
curl_grab_page($my_grab_page$my_proxy$my_proxy_status);
fclose($fp_err);


/* login functions by Chad from http://codersshack.com/forums/php_11/tutorial-curl-login_44.html */
/* functions are slightly modified in order to send curl verbose report to a file */

function curl_login($url,$data,$proxy,$proxystatus){
    global 
$fp_err;

    
$fp fopen("cookie_travian.txt""w");
    
fclose($fp);
    
$login curl_init();
    if (
DEBUG_MODE) {
      
curl_setopt($loginCURLOPT_VERBOSETRUE);
      
curl_setopt($loginCURLOPT_STDERR$fp_err);
    }
    
curl_setopt($loginCURLOPT_COOKIEJAR"cookie_travian.txt");
    
curl_setopt($loginCURLOPT_COOKIEFILE"cookie_travian.txt");
    
curl_setopt($loginCURLOPT_USERAGENT"Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4");
    
curl_setopt($loginCURLOPT_REFERER"http://s1.travian.net/dorf1.php");
    
curl_setopt($loginCURLOPT_TIMEOUT30);
    
curl_setopt($loginCURLOPT_RETURNTRANSFERTRUE);
    if (
$proxystatus == 'on') {
        
curl_setopt($loginCURLOPT_SSL_VERIFYHOSTFALSE);
        
curl_setopt($loginCURLOPT_HTTPPROXYTUNNELTRUE);
        
curl_setopt($loginCURLOPT_PROXY$proxy);
    }
    
curl_setopt($loginCURLOPT_URL$url);
    
curl_setopt($loginCURLOPT_HEADERTRUE);
    
curl_setopt($loginCURLOPT_USERAGENT$_SERVER['HTTP_USER_AGENT']);
    
curl_setopt($loginCURLOPT_FOLLOWLOCATIONTRUE);
    
curl_setopt($loginCURLOPT_POSTTRUE);
    
curl_setopt($loginCURLOPT_POSTFIELDS$data);
    
ob_start();      // prevent any output
    
return curl_exec ($login); // execute the curl command
    
ob_end_clean();  // stop preventing output
    
curl_close ($login);
    unset(
$login);    
}                  

function 
curl_grab_page($site,$proxy,$proxystatus){
    global 
$fp_err;

    
$ch curl_init();
    if (
DEBUG_MODE) {
      
curl_setopt($chCURLOPT_VERBOSETRUE);
      
curl_setopt($chCURLOPT_STDERR$fp_err);
    }
    
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
    if (
$proxystatus == 'on') {
        
curl_setopt($chCURLOPT_SSL_VERIFYHOSTFALSE);
        
curl_setopt($chCURLOPT_HTTPPROXYTUNNELTRUE);
        
curl_setopt($chCURLOPT_PROXY$proxy);
    }
    
curl_setopt($chCURLOPT_COOKIEFILE"cookie_travian.txt");
    
curl_setopt($chCURLOPT_URL$site);
    
ob_start();      // prevent any output
    
return curl_exec ($ch); // execute the curl command
    
ob_end_clean();  // stop preventing output
    
curl_close ($ch);
}

?>
here is what i get in the curl log file "curl_errors.txt":
Quote:
Originally Posted by curl_errors.txt
* About to connect() to s1.travian.net port 80 (#0)
* Trying 88.198.59.126... * connected
* Connected to s1.travian.net (88.198.59.126) port 80 (#0)
> GET /dorf1.php HTTP/1.1
Host: s1.travian.net
Accept: */*

< HTTP/1.1 200 OK
< Date: Fri, 13 Jul 2007 21:36:52 GMT
< Server: Apache
< X-Powered-By: PHP/5.2.1
< Expires: Mon, 26 Jul 1997 05:00:00 GMT
< Last-Modified: Fri, 13 Jul 2007 21:36:52 GMT
< Cache-Control: no-store, no-cache, must-revalidate
< Cache-Control: post-check=0, pre-check=0
< Pragma: no-cache
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=UTF-8
* Closing connection #0
che, must-revalidate
< Cache-Control: post-check=0, pre-check=0
< Pragma: no-cache
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=UTF-8
* Closing connection #0

Last edited by r0manoff; 07-13-2007 at 03:26 PM.
Reply With Quote
  #16 (permalink)  
Old 08-12-2007, 12:44 AM
Junior Member
 
Join Date: Aug 2007
Posts: 1
Thanks!

Hi Chad,

I just wanted to thank you for the very informative cUrl tutorial. I've been searching for hours on how to get cUrl to work with my site to no avail. That is until I stumbled upon your tutorial.

Thanks so much. It works perfectly.

Do you have some kind of RSS feed where I can get regular updates from you?
Reply With Quote
  #17 (permalink)  
Old 10-25-2007, 03:20 AM
Junior Member
 
Join Date: Oct 2007
Posts: 2
thanks this helped me out alot!
Reply With Quote
  #18 (permalink)  
Old 11-08-2007, 09:54 PM
Junior Member
 
Join Date: Nov 2007
Posts: 11
Quote:
Originally Posted by Chad View Post

PHP Code:
function curl_login($url,$data,$proxy,$proxystatus){
    
$fp fopen("cookie.txt""w");
    
fclose($fp);
    
$login curl_init();
    
curl_setopt($loginCURLOPT_COOKIEJAR"cookie.txt");
    
curl_setopt($loginCURLOPT_COOKIEFILE"cookie.txt");
    
curl_setopt($loginCURLOPT_USERAGENT"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    
curl_setopt($loginCURLOPT_TIMEOUT40);
    
curl_setopt($loginCURLOPT_RETURNTRANSFERTRUE);
    if (
$proxystatus == 'on') {
        
curl_setopt($loginCURLOPT_SSL_VERIFYHOSTFALSE);
        
curl_setopt($loginCURLOPT_HTTPPROXYTUNNELTRUE);
        
curl_setopt($loginCURLOPT_PROXY$proxy);
    }
    
curl_setopt($loginCURLOPT_URL$url);
    
curl_setopt($loginCURLOPT_HEADERTRUE);
    
curl_setopt($loginCURLOPT_USERAGENT$_SERVER['HTTP_USER_AGENT']);
    
curl_setopt($loginCURLOPT_FOLLOWLOCATIONTRUE);
    
curl_setopt($loginCURLOPT_POSTTRUE);
    
curl_setopt($loginCURLOPT_POSTFIELDS$data);
    
ob_start();      // prevent any output
    
return curl_exec ($login); // execute the curl command
    
ob_end_clean();  // stop preventing output
    
curl_close ($login);
    unset(
$login);    
}                  

function 
curl_grab_page($site,$proxy,$proxystatus){
    
$ch curl_init();
    
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
    if (
$proxystatus == 'on') {
        
curl_setopt($chCURLOPT_SSL_VERIFYHOSTFALSE);
        
curl_setopt($chCURLOPT_HTTPPROXYTUNNELTRUE);
        
curl_setopt($chCURLOPT_PROXY$proxy);
    }
    
curl_setopt($chCURLOPT_COOKIEFILE"cookie.txt");
    
curl_setopt($chCURLOPT_URL$site);
    
ob_start();      // prevent any output
    
return curl_exec ($ch); // execute the curl command
    
ob_end_clean();  // stop preventing output
    
curl_close ($ch);

Hope you liked.
Great Great Great tutorial man...

Thanks allot!!! It was very helpful. Just what I needed... Now, all I need is a run through of how to go about parsing and/or stripping out things I don't need, as well as have the end result display in a nice, neat designed layout on the page I will view it on...

Any suggestions? Maybe another wonderful tutorial? ;)


Thanks again,
Chris
Reply With Quote
  #19 (permalink)  
Old 11-08-2007, 10:07 PM
[Administrator]
 
Join Date: May 2007
Posts: 383
Quote:
Originally Posted by chrishawkins View Post
Great Great Great tutorial man...

Thanks allot!!! It was very helpful. Just what I needed... Now, all I need is a run through of how to go about parsing and/or stripping out things I don't need, as well as have the end result display in a nice, neat designed layout on the page I will view it on...

Any suggestions? Maybe another wonderful tutorial? ;)


Thanks again,
Chris
preg_match and preg_replace will be your best friends, also read up on Regular Expressions if you aren't familiar with them.
Reply With Quote
  #20 (permalink)  
Old 11-09-2007, 08:27 PM
Junior Member
 
Join Date: Nov 2007
Posts: 11
Question

Quote:
Originally Posted by Chad View Post
preg_match and preg_replace will be your best friends, also read up on Regular Expressions if you aren't familiar with them.
Hello Chad,

I sent a prvt message or two your way about maybe getting a little bit of help with the end result of what I have after I've successfully logged in ans pulled the data I need. What I need out of the source code is not too terribly much, however, I was looking at having it before the beginning of next week.

Let me know if you would be interested..

Thanks again,

Chris
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://scriptasy.com/php_11/tutorial-curl-login_44.html

Posted By For Type Date
Can a website block the use of file_get_contents ? - PHWinfo This thread Refback 08-22-2008 01:13 AM
Can a website block the use of file_get_contents ? - comp.lang.php | Google Groups This thread Refback 04-26-2008 11:12 AM
Can a website block the use of file_get_contents ? - comp.lang.php | Google Groupes This thread Refback 04-23-2008 04:43 AM
Live website statistieken - Ulco.nl This thread Pingback 03-31-2008 11:37 AM
Can a website block the use of file_get_contents ? - comp.lang.php | Google Groups This thread Refback 03-31-2008 08:03 AM
Can a website block the use of file_get_contents ? - comp.lang.php | Google Groups This thread Refback 03-29-2008 03:21 AM
Can a website block the use of file_get_contents ? - Object Mix This thread Refback 03-29-2008 03:10 AM
Can a website block the use of file_get_contents ? - Usenet Forums This thread Refback 03-28-2008 10:45 AM
Web Link This thread Refback 09-05-2007 07:15 PM
amob005's bookmarks tagged with "php_curl_video_tutorial" on del.icio.us This thread Refback 08-12-2007 12:41 AM
digg / blizzedout / news / dugg This thread Refback 06-17-2007 02:14 AM
digg / beasttoast / news / submitted This thread Refback 06-02-2007 02:02 PM
YouTube - Curl Login Tutorial This thread Refback 05-31-2007 04:37 PM
Digg / Programming / Upcoming This thread Refback 05-31-2007 01:36 PM
Digg / Programming / Upcoming This thread Refback 05-31-2007 09:16 AM
Digg / News / Upcoming This thread Refback 05-30-2007 10:08 PM
Digg / News / Upcoming This thread Refback 05-30-2007 10:05 PM
Digg / News / Upcoming This thread Refback 05-30-2007 10:00 PM
Digg - [Tutorial] PHP Curl Login This thread Refback 05-30-2007 10:00 PM


All times are GMT -8. The time now is 02:22 PM.

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
scriptasy girl by HitmanN

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152