Jump to content

Need help with this


tartou2

Recommended Posts

Hello everybody

I was thinking of a page that once is run, it auto-login from a form and auto submit data to another form which need the user to be logged in.

I have created these 2 pages it works perfectly but i am having a problem when it comes to cronjob. Even if the auto-login page is run first, for some reason, the account doesn't continue to be logged in and the data are not submitted from the second page because the account is not logged-in.

 

Anyone know how to help to bypass this issue and to make the server to login using cronjob without my interference ?

 

Everyone help who know how to do this is appreciated

Link to comment
https://forums.phpfreaks.com/topic/220276-need-help-with-this/
Share on other sites

So let me get this straight:  You want a cron job to run a script which submits info to a form.  The best way to do this would be to have php generate a POST header with the information and send it to the script that handles the POST data.

 

http://in.answers.yahoo.com/question/index?qid=20081008042402AArWUf2

Link to comment
https://forums.phpfreaks.com/topic/220276-need-help-with-this/#findComment-1141490
Share on other sites

ok but there is still a problem

the login form is not on my website but on another website so how can i fis this code now

 

$post_data = 'userid=test1&password=test2';
$content_length = strlen($post_data);

header('POST login.php HTTP/1.1');
header('Host: http://thewebsite.com');
header('Connection: close');
header('Content-type: application/x-www-form-urlencoded');
header('Content-length: ' . $content_length);
header('');
header($post_data);

 

I am getting a 500 internal server error. How can i fix this ?

Link to comment
https://forums.phpfreaks.com/topic/220276-need-help-with-this/#findComment-1141541
Share on other sites

ok i am using this now

 

<?php
session_start();

$host = 'www.thewebsite.com';
$service_uri = '/login.php';
$vars ='userid=test1&password=test2';

$header = "Host: $host\r\n";
$header = "POST $service_uri  HTTP/1.1\r\n";
$header .= "User-Agent: PHP Script\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($vars)."\r\n";
$header .= "Connection: close\r\n\r\n";


$server = 'localhost';
$database = '*****';
$dbusername = '*****';
$dbpassword = '*****';
$connection = mysql_connect($server,$dbusername,$dbpassword);

if (!$connection) {
die('Could not connect to MySQL database, the server return the error: '.mysql_error());
}
$db = @mysql_select_db($database);

$check=mysql_query("SELECT * FROM  data WHERE id=1");
$row=mysql_fetch_array($check);
$add_id=$row['add_id'];

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  </head>
<body onLoad="document.formdata.submit();">
<script type="text/javascript">
j=<?php echo $add_id; ?>;
document.write("<form method=POST name=formdata action=http://thewebsite.com/members/editbanners.php>");
document.write("<input type=hidden name=name maxsize=30 value=somevalue><br>");
document.write("<input type=hidden name=bannerurl maxsize=70 value=someurl>");
document.write("<input type=hidden name=targeturl maxsize=70 value=someotherurl>");
document.write("<input type=hidden name=id value="+j+">");
document.write("<input type=hidden name=done value=YES>");
document.write("<input type=submit value=someothervalue>");
document.write("</form>");
</script>

<?php
mysql_query("UPDATE data SET add_id=add_id+1 WHERE id=1");
?>  	

  </body>
</html>

 

I know i can remove the javascript code but the code was edited lately and i still haven't remove the javascript code.

Anyway now i am not receiving anymore the error but when the form is submitted, it doesn't log to the account.

How can i fix this ?

Link to comment
https://forums.phpfreaks.com/topic/220276-need-help-with-this/#findComment-1141548
Share on other sites

typically, when a user is "logged in", a session is started for that user, and the user can continue browsing with the session.

 

if the server you are posting to uses sessions to maintain logged in status, you will not be able to log a user in on the other site as simply as you seem to expect.

 

but maybe I am mis-understanding the purpose of this project?

Link to comment
https://forums.phpfreaks.com/topic/220276-need-help-with-this/#findComment-1141551
Share on other sites

typically, when a user is "logged in", a session is started for that user, and the user can continue browsing with the session.

 

if the server you are posting to uses sessions to maintain logged in status, you will not be able to log a user in on the other site as simply as you seem to expect.

 

but maybe I am mis-understanding the purpose of this project?

 

yes the website use session.

So anyway i can make a cronjob to run this code and to handle the session or anyway i make the data to be submitted from the form ?

Link to comment
https://forums.phpfreaks.com/topic/220276-need-help-with-this/#findComment-1141553
Share on other sites

you won't be able to submit form data to a remote server and have a user be able be logged in that way unless (and only maybe) if you use curl functions to submit the data and manage session cookies. but you won't likely be able to set a cookie on the user's browser to tell the remote server that the user is logged in with an active session.

Link to comment
https://forums.phpfreaks.com/topic/220276-need-help-with-this/#findComment-1141556
Share on other sites

okay, i think i did misunderstand. so there will be no user/browser.

 

you want to run a script to:

1. log in to remote server

2. submit data to another form that requires the visitor to be logged in

 

You are just logging in and submitting data? if so, you could probably do that with curl. here is an example i found via google.com that might work for you:

 

http://www.knowledgesutra.com/forums/topic/38162-automatic-login-using-curl/

Link to comment
https://forums.phpfreaks.com/topic/220276-need-help-with-this/#findComment-1141570
Share on other sites

ok so this is the new code but it is not working

<?php
session_start();
$server = 'localhost';
$database = '*****';
$dbusername = '*****';
$dbpassword = '******';
$connection = mysql_connect($server,$dbusername,$dbpassword);

if (!$connection) {
die('Could not connect to MySQL database, the server return the error: '.mysql_error());
}
$db = @mysql_select_db($database);

$check=mysql_query("SELECT * FROM  data WHERE id=1");
$row=mysql_fetch_array($check);
echo $add_id=$row['add_id'];

// INIT CURL
$ch = curl_init();

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'http://www.thewebsite.com/login.php');

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'userid=test1&password=test2');

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);

// SET FILE TO DOWNLOAD
// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'http://www.thewebsite.com/members/editbanners.php');

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'name=somevalue&bannerurl=someurl&targeturl=someotherurl&id='.$add_id.'&done=YES');

// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
$content = curl_exec ($ch);

// CLOSE CURL
curl_close ($ch); 

?>

 

Anyone can help me to fix this code ?

Link to comment
https://forums.phpfreaks.com/topic/220276-need-help-with-this/#findComment-1141845
Share on other sites

Are you currently executing the script directly or are you waiting for the cron job to do it?  If you execute directly you can test to see whether or not you are getting errors in your script.  I noticed you have spaced between the function names and the braces.  Echo your sql query.  Echo the variables you are interested in.  Echo the result from the curl execution function.

Link to comment
https://forums.phpfreaks.com/topic/220276-need-help-with-this/#findComment-1141926
Share on other sites

Are you currently executing the script directly or are you waiting for the cron job to do it?  If you execute directly you can test to see whether or not you are getting errors in your script.  I noticed you have spaced between the function names and the braces.  Echo your sql query.  Echo the variables you are interested in.  Echo the result from the curl execution function.

 

i did echo the curl execution and the mysql query but nothing appeared.

the mysql query show 2442 but nothing from the curl appeared

Link to comment
https://forums.phpfreaks.com/topic/220276-need-help-with-this/#findComment-1141954
Share on other sites

...and you aren't getting any errors?  Do you have error_reporting set to 0 (OFF)?  Try var_dump($ch) and do the same for $content.  The only thing I can really suggest is to go line by line and verify that the script is doing what you want it to by echoing.

Link to comment
https://forums.phpfreaks.com/topic/220276-need-help-with-this/#findComment-1141996
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.