Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function login($username, $password, $url = 'http://moodle.tsrs.org/login/index.php')
- {
- // Create base headers for use with both requests
- $parts = parse_url($url);
- $baseHeaders = array(
- 'Origin: ' . $parts['scheme'] . '://' . $parts['host'],
- 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36',
- 'Accept: */*',
- );
- // Temporary file path for the cookie jar
- $cookieJar = tempnam('/tmp', $username . '-cookie');
- // Make an initial GET request to fetch a fresh set of session cookies
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);
- curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $baseHeaders);
- curl_exec($ch);
- curl_close($ch);
- // Make the actual POST request to attempt login
- $data = array(
- 'username' => $username,
- 'password' => $password,
- 'testcookies'=> '1',
- );
- $headers = array(
- 'Referer: ' . $url,
- );
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);
- curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($baseHeaders, $headers));
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- $output = curl_exec($ch);
- curl_close($ch);
- // Remove the temp cookie store
- unlink($cookieJar);
- echo "\n" . $output . "\n";
- }
- login($_POST['username'], $_POST['password']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement