[PHP]cURL help needed

Skilled Illusionist
Joined
Jan 14, 2005
Messages
395
Reaction score
2
hello,
i was trying to make a login script for my own game.
i want that i login and goto domain.be/?p=ranking
when it logged but it must remember the username aswell how i can do this?

i got his so far

PHP:
<?php
//extract data from the post
extract($_POST);

//set POST variables
$url = 'http://nhc.hostei.com/?p=login';
$fields = array(
                      'username'=>urlencode("testaccount"),
                      'password'=>urlencode("test123") ,
                      'method' => 'POST'
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);
echo $result;

//close connection
curl_close($ch);
?>

Thanks
passie
 
I am not entirely sure what you want to accomplish here: do you want to create a 'bot' that can automatically login on a webpage and be redirected? Or do you want 'normal' users to login and be redirected, while they stay logged in?

In the first case, you will have to resend the session cookie with each cURL request, provided your login is session based.

In the second case, forget about cURL and simply store their username/password in a session and verify it from there.
 
I am not entirely sure what you want to accomplish here: do you want to create a 'bot' that can automatically login on a webpage and be redirected? Or do you want 'normal' users to login and be redirected, while they stay logged in?

In the first case, you will have to resend the session cookie with each cURL request, provided your login is session based.

In the second case, forget about cURL and simply store their username/password in a session and verify it from there.
well first case is what i wanna do but not idea how anyone got a tip/example?

~passie
 
As I said, you must use curl_setopt to store and resend the session cookie for each request. The easiest way to do this is set a temp file location where curl can store the session cookie. Either of these should work:
PHP:
curl_setopt($ch, CURLOPT_FILE, '/cookie.txt'); 
curl_setopt($ch, CURLOPT_COOKIEJAR, '/cookie.txt');

If it's your own game you're probably easier off using server-to-server protocols like SOAP or XML-HTTP though.
 
As I said, you must use curl_setopt to store and resend the session cookie for each request. The easiest way to do this is set a temp file location where curl can store the session cookie. Either of these should work:
PHP:
curl_setopt($ch, CURLOPT_FILE, '/cookie.txt'); 
curl_setopt($ch, CURLOPT_COOKIEJAR, '/cookie.txt');

If it's your own game you're probably easier off using server-to-server protocols like SOAP or XML-HTTP though.
well Thanks!

~passie
 
Back