Ive use the code below to create session to use the auto login function for users, but im having trouble sending the complete url from a form, it seems to cut off everything after login/?
If I just use a link in the page, it works fine, any ideas what im missing?
If I just use a link in the page, it works fine, any ideas what im missing?
PHP:
// This can also be the reseller who owns the cPanel user.
$whmusername = "name";
$whmpassword = "password";
$hostname = "example.com";
// The user on whose behalf the API call runs.
$cpanel_user = "username"; //under reseller
$query = "https://$hostname:2087/json-api/create_user_session?api.version=1&user=$cpanel_user&service=cpaneld";
$curl = curl_init(); // Create Curl Object.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); // Allow self-signed certificates...
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // and certificates that don't match the hostname.
curl_setopt($curl, CURLOPT_HEADER, false); // Do not include header in output
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Return contents of transfer on curl_exec.
$header[0] = "Authorization: Basic " . base64_encode($whmusername . ":" . $whmpassword) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // Set the username and password.
curl_setopt($curl, CURLOPT_URL, $query); // Execute the query.
$result = curl_exec($curl);
if ($result == false) {
error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
return response()->json(['error' => 'There is a problem, Please try again.']);
}
$decoded_response = json_decode( $result, true );
//Access denied
if (isset($decoded_response['cpanelresult'])){
if($decoded_response['cpanelresult']['data']['result'] == 0)
return response()->json(['error' => 'Action Failed Unable to auto-login. Please contact support']);
//}
//Invalid username
if ($decoded_response['metadata']['result'] == 0) return response()->json(['error' => 'Unable to login']);
}
curl_close($curl);
$resultw = $decoded_response['data']['url'];
//echo "Echo Result: " . $resultw;
//removes extra characters after create_user_account
$resulty = substr($resultw, 0, strpos($resultw, 'create_user_session') + 19);
//echo "RESULTY: " . $resulty;
// form not working
echo '<form action=' . $resulty . 'method="post">';
echo '<p class= exchange_text> Login to cPanel</p>';
echo '<input type="submit" value="Login to cpanel" >';
echo '</form>';
echo "</br>";
// link works
echo '<a href=' . $resulty . '>cPanel Login</a> ';
echo "</br>";
echo "</br>";
Last edited by a moderator: