karl
karl

Reputation: 11

Php curl script working on localhost (xampp) but not when I upload it to hosting

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:

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!

Upvotes: 0

Views: 1775

Answers (1)

karl
karl

Reputation: 11

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!

Upvotes: 1

Related Questions