Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login to Opencart's admin with curl of PHP

I want to login to OpenCart CMS with curl automatically and add things to it automatic too
but I can't login to it.I searched and found some result but couldn't help me and I don't get any result. This is my code:

<?php
include_once "simple_html_dom.php";
$username = 'active';
$password = '123active';
$loginUrl = 'http://localhost:100/mywebsite/admin/';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $loginUrl);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$username.'&password='.$password);

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$store = curl_exec($ch);
$html=new simple_html_dom();
$html->load($store);
foreach($html->find("li#dashboard") as $dash){
    echo $dash->innertext;
}
?> 

In this code first I initialize username and password and my URL of linking to
to admin of my website then I use CURLOPT_POST request and CURLOPT_POSTFIELDS
and then execute the $ch but now I don't have any result on cookie.txt and didn't
login to website's admin.

like image 678
MEAM Avatar asked Nov 10 '22 05:11

MEAM


1 Answers

I looked at http://demo.opencart.com/admin/ and saw that the action url in the form is not only /admin. Try this one:

<?php
include_once "simple_html_dom.php";
$username = 'active';

$password = '123active';
//$loginUrl = 'http://localhost:100/mywebsite/admin/';
//new url
$loginUrl = 'http://localhost:100/mywebsite/admin/index.php?route=common/login';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $loginUrl);

curl_setopt($ch, CURLOPT_POST, true );

// follows a location header redirect
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );

curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$username.'&password='.$password);

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$store = curl_exec($ch);
$html=new simple_html_dom();
$html->load($store);
foreach($html->find("li#dashboard") as $dash){
    echo $dash->innertext;
}
?>

I didnt't test if the action url depends on a specific version, so just inspect the form with your browser developer tools

like image 81
swidmann Avatar answered Nov 14 '22 22:11

swidmann