Skip to content

Instantly share code, notes, and snippets.

@cyavu
Last active February 2, 2019 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyavu/21fd22953de81bfeb65604e5a0414842 to your computer and use it in GitHub Desktop.
Save cyavu/21fd22953de81bfeb65604e5a0414842 to your computer and use it in GitHub Desktop.
Plesk auto login script in PHP
<?php
/* Plesk auto-login PHP-script */
/* This script is using a predefined IP address for which this auto login script will work for */
/* Tested on Plex Onyx 17.8.11 */
/* Written by Cihad Yavuzel, out of frustration */
// Set Plesk XML-API Target
$ch = curl_init('https://<hostname.tld>:8443/enterprise/control/agent.php');
// Elements user_ip and source_server are base64 encoded. First is the client's IP address and second is the hostname of where this request is coming from
$xml_data ='<packet version="1.6.3.5">
<server>
<create_session>
<login>admin</login>
<data>
<user_ip>MTI3LjAuMC4x</user_ip>
<source_server>bG9jYWxob3N0</source_server>
</data>
</create_session>
</server>
</packet>';
// curl -H headers, you need the change the HTTP_AUTH_PASSWD to your Plesk admin password
$headers = array();
$headers[] = 'Content-Type: text/xml';
$headers[] = 'HTTP_AUTH_LOGIN: admin';
$headers[] = 'HTTP_AUTH_PASSWD: PASSWD';
$headers[] = 'HTTP_PRETTY_PRINT: TRUE';
// We are ready to POST our data to our Plesk XML-API endpoint
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Executes our curl command and store output in variable
$output = curl_exec($ch);
// Load into DOM
$var = "$xmlstr$output";
// Uncomment for debugging, saves the response to test.php
// file_put_contents('test.php', $var);
// Close curl connection to free up resources
curl_close($ch);
?>
<?php
// SimpleXML, here we are reading the session token Plesk has given us back
$xml=simplexml_load_string($var) or die("Error: Cannot create object");
$auth = $xml->server->create_session->result->id;
// Our magical auto-login link which will redirect you (optionally to a different page within Plesk as well)
header("Location: https://<hostname.tld>:8443/enterprise/rsession_init.php?PHPSESSID=$auth&success_redirect_url=admin/subscription/list?context=subscriptions");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment