Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. function retrieveConnection($file) {
  2. // takes a filename where heroku connection parameters are stored and returns a connection object
  3.  
  4. $handle = fopen($file, "r") or die("Unable to open file!\r\n");
  5. $c_string = fread($handle, filesize($file));
  6.  
  7. // the connection string from heroku is formatted as follows
  8. // username is between the first // and the first :
  9. // password is after the first : and before the first @
  10. // server is after the first @ and before the second :
  11. // port is after the second : and before the last /
  12. // database name is after the last /
  13.  
  14. $p1 = strpos($c_string, "//");
  15. $p2 = strpos($c_string, ":", $p1);
  16. $p3 = strpos($c_string, "@", $p2);
  17. $p4 = strpos($c_string, ":", $p3);
  18. $p5 = strpos($c_string, "/", $p4);
  19.  
  20. print "Connection string: \r\n" . $c_string . "\r\n";
  21.  
  22. $username = substr($c_string, $p1 + 2, ($p2-$p1) - 2);
  23. $password = substr($c_string, $p2 + 1, ($p3-$p2) - 1);
  24. $server = substr($c_string, $p3 + 1, ($p4 - $p3) - 1);
  25. $port = substr($c_string, $p4 + 1, ($p5 - $p4) - 1);
  26. $database = substr($c_string, $p5 + 1);
  27.  
  28. print "username: " . $username . "\r\n";
  29. print "password: " . $password . "\r\n";
  30. print "server: " . $server . "\r\n";
  31. print "port: " . $port . "\r\n";
  32. print "database: " . $database . "\r\n";
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement