tartou2 Posted November 30, 2010 Share Posted November 30, 2010 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 Quote Link to comment Share on other sites More sharing options...
JakeTheSnake3.0 Posted November 30, 2010 Share Posted November 30, 2010 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 Quote Link to comment Share on other sites More sharing options...
tartou2 Posted November 30, 2010 Author Share Posted November 30, 2010 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 ? Quote Link to comment Share on other sites More sharing options...
tartou2 Posted November 30, 2010 Author Share Posted November 30, 2010 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 ? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 30, 2010 Share Posted November 30, 2010 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? Quote Link to comment Share on other sites More sharing options...
tartou2 Posted November 30, 2010 Author Share Posted November 30, 2010 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 ? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 30, 2010 Share Posted November 30, 2010 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. Quote Link to comment Share on other sites More sharing options...
tartou2 Posted November 30, 2010 Author Share Posted November 30, 2010 can we at least try this method?? Can you update the code in my latest post? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 30, 2010 Share Posted November 30, 2010 i didn't mean to imply that i can do it. i don't think it can be done. 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. Quote Link to comment Share on other sites More sharing options...
tartou2 Posted November 30, 2010 Author Share Posted November 30, 2010 but i don't want it to use a user browser. I want it to be run from a cronjob Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 30, 2010 Share Posted November 30, 2010 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/ Quote Link to comment Share on other sites More sharing options...
tartou2 Posted December 1, 2010 Author Share Posted December 1, 2010 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 ? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 1, 2010 Share Posted December 1, 2010 Describe "not working". Ken Quote Link to comment Share on other sites More sharing options...
tartou2 Posted December 1, 2010 Author Share Posted December 1, 2010 i mean that nothing is submitted from this code. This script should submit ads but nope nothings is submitted from this code Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 1, 2010 Share Posted December 1, 2010 Have you done any debugging? You really shouldn't use the "@" when developing code. It suppresses error messages. You should be checking that all of your mysql calls work. Ken Quote Link to comment Share on other sites More sharing options...
JakeTheSnake3.0 Posted December 1, 2010 Share Posted December 1, 2010 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. Quote Link to comment Share on other sites More sharing options...
tartou2 Posted December 1, 2010 Author Share Posted December 1, 2010 Have you done any debugging? You really shouldn't use the "@" when developing code. It suppresses error messages. You should be checking that all of your mysql calls work. Ken all mysql syntax are working fine. the problem is in the curl syntax Quote Link to comment Share on other sites More sharing options...
tartou2 Posted December 1, 2010 Author Share Posted December 1, 2010 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 Quote Link to comment Share on other sites More sharing options...
JakeTheSnake3.0 Posted December 1, 2010 Share Posted December 1, 2010 Well, what about the spaces between the function names and their respective ( ) braces? Quote Link to comment Share on other sites More sharing options...
tartou2 Posted December 1, 2010 Author Share Posted December 1, 2010 Well, what about the spaces between the function names and their respective ( ) braces? i removed the spaces and still nothing is shown from the curl execution Quote Link to comment Share on other sites More sharing options...
JakeTheSnake3.0 Posted December 1, 2010 Share Posted December 1, 2010 ...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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.