0

I know that similar questions have been asked but I think they are a little bit different or they have not solved my problem. First of all, I started to learn about PHP/curl and websites in general only a few weeks ago, so I am sure I am doing a lot of mistakes. For this reason, I would ask understanding from you.

Okay, thanks to stackoverflow I have been able to "write" an script whose function is:

  • Login to www.example.com/login.php
  • "Fill a form" on www.example.com/submit.php?h=test

The code is the following:

code.php

<?php

error_reporting (E_ALL | E_STRICT);
set_time_limit(0);

function curl_login($url,$data,$proxy,$proxystatus){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_URL, $url);
    ob_start();      // prevent any output
    $temp=curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output



    $fp = fopen("cookie.txt", "w");
    fclose($fp);
     $ch = curl_init();
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    ob_start();      // prevent any output
    return curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output
    curl_close ($ch);
    unset($ch);    
}                   

$test1 = curl_login('http://www.example.com/login.php','username=itsme&password=abcdedg&login=Login','','off');


function curl_submit($url,$data,$proxy,$proxystatus){
        $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_URL, $url);
    ob_start();      // prevent any output
    $temp=curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output
    $fp = fopen("cookie.txt", "w");
    fclose($fp);
     $ch = curl_init();
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_TIMEOUT, 130);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    ob_start();      // prevent any output
    return curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output
    curl_close ($ch);
    unset($ch);
}       

$test2 = curl_submit('http://www.example.com/submit.php?h=test','url=www.heythere.com&username=xyz&submit=submit','','off');
echo $test2;


?>

The strange thing (for me) is that when I run it on Xampp everything works perfect, but when I run it from my hosting (in this example would be http://www.example.com/code.php) it is not able to login and the header is:

HTTP/1.1 301 Moved Permanently Date: Sat, 15 Aug 2015 10:50:26 GMT Server: Apache Expires: Mon, 26 Jul 1997 05:00:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Set-Cookie: HEY=1342453645645645645; path=/ Last-Modified: Sat, 15 Aug 2015 10:50:26 GMT Location: http://www.example.com/login.php Vary: Accept-Encoding,User-Agent Content-Length: 0 Content-Type: text/html HTTP/1.1 200 OK Date: Sat, 15 Aug 2015 10:50:26 GMT Server: Apache 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 Last-Modified: Sat, 15 Aug 2015 10:50:26 GMT Vary: Accept-Encoding,User-Agent Transfer-Encoding: chunked Content-Type: text/html

I hope everything is clear and that you don't see so much mistakes!

Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
karl
  • 11
  • 4
  • Are you sure curl is installed on your hosted server? Check [this article][1] [1]: http://stackoverflow.com/questions/13433946/how-to-check-if-curl-is-enabled-or-disabled – HarveyEV Aug 15 '15 at 11:01
  • Yes, it is installed, I have checked it! From the applications path: curl 7.19.7 – karl Aug 15 '15 at 11:06
  • Its cuz of your site has been redirect before run your code. It can be cuz of www to non-www . or anything else. please control your redirecting. You can use that link : http://www.redirect-checker.org/ – Muhammet Arslan Aug 15 '15 at 11:50
  • Well, now that I see phpinfo.php it says: cURL support: enabled cURL Information: 7.38.0, I don't know why – karl Aug 15 '15 at 11:52
  • Thank you Muhammet Arslan. It says CONGRATULATION. Everything seems to be fine. When I write url without www, it says: http://example.com 301 Moved Permanently http://www.example.com/200 OK. What is odd to me is the fact that it is working when I run it in xampp (it doesn't say anything about 301) but not when I run it from the file uploaded to my hosting. – karl Aug 15 '15 at 11:56
  • Almost automatically said use follow redirects but then saw that you have it enabled already. It would probably help if you edited your question and put in the real url instead of exampel – e4c5 Aug 15 '15 at 11:56

1 Answers1

1

Yesterday I found a solution (thanks to this post: How can I make cURL set a cookie relative to the script?), so I am going to post my final code in order to help anyone with the same problem. Briefly: it was a problem with cookies and their defined path. I have also deleted two lines that, I don't know why, made code not working.

Okay, code.php:

<?php

error_reporting (E_ALL | E_STRICT);
set_time_limit(0);

function curl_login($url,$data,$proxy,$proxystatus){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $cookiefile = dirname(__FILE__)."/cookies.txt" ;
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_URL, $url);
    ob_start();      // prevent any output
    $temp=curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output


    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    ob_start();      // prevent any output
    return curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output
    curl_close ($ch);
    unset($ch);    
}                   

$test1 = curl_login('http://www.example.com/login.php','username=itsme&password=abcdedg&login=Login','','off');


function curl_submit($url,$data,$proxy,$proxystatus){
        $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $cookiefile = dirname(__FILE__)."/cookies.txt" ;
    curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_URL, $url);
    ob_start();      // prevent any output
    $temp=curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_TIMEOUT, 130);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    ob_start();      // prevent any output
    return curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output
    curl_close ($ch);
    unset($ch);
}       

$test2 = curl_submit('http://www.example.com/submit.php?h=test','url=www.heythere.com&username=xyz&submit=submit','','off');
echo $test2;


?>

Thank you all for trying to help!

Community
  • 1
  • 1
karl
  • 11
  • 4