Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. function login($username, $password, $url = 'http://moodle.tsrs.org/login/index.php')
  4. {
  5.     // Create base headers for use with both requests
  6.     $parts = parse_url($url);
  7.     $baseHeaders = array(
  8.         'Origin: ' . $parts['scheme'] . '://' . $parts['host'],
  9.         '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',
  10.         'Accept: */*',
  11.     );
  12.  
  13.     // Temporary file path for the cookie jar
  14.     $cookieJar = tempnam('/tmp', $username . '-cookie');
  15.  
  16.     // Make an initial GET request to fetch a fresh set of session cookies
  17.     $ch = curl_init($url);
  18.  
  19.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  20.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  21.  
  22.     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);
  23.     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
  24.  
  25.     curl_setopt($ch, CURLOPT_HTTPHEADER, $baseHeaders);
  26.  
  27.     curl_exec($ch);
  28.     curl_close($ch);
  29.  
  30.     // Make the actual POST request to attempt login
  31.     $data = array(
  32.         'username' => $username,
  33.         'password' => $password,
  34.         'testcookies'=> '1',
  35.     );
  36.     $headers = array(
  37.         'Referer: ' . $url,
  38.     );
  39.  
  40.     $ch = curl_init($url);
  41.  
  42.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  43.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
  44.  
  45.     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);
  46.     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
  47.  
  48.     curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($baseHeaders, $headers));
  49.     curl_setopt($ch, CURLOPT_POST, true);
  50.     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  51.  
  52.     $output = curl_exec($ch);
  53.     curl_close($ch);
  54.  
  55.     // Remove the temp cookie store
  56.     unlink($cookieJar);
  57.  
  58.     echo "\n" . $output . "\n";
  59. }
  60.  
  61. login($_POST['username'], $_POST['password']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement