**Where I am:**
I have Wordpress and Moodle up and running. Also I have LDAPserver. Users are able to login using same name and password to both systems successfully.
----------
**What I am trying to achieve:**
When users log in to Wordpress, they automatically log in to Moodle (SSO) too.
----------
**Current code:**
On Wordpress user authentication I get his username and password.
add_filter('authenticate', 'zg_sso_auth_signon', 30, 3);
function zg_sso_auth_signon($user, $username, $password) {
if ($username != "") {
$data = array(
'username' => $username,
'password' => $password,
'Submit' => 'Login'
);
$response = post_to_url("http://domain.com/moodle/login/index.php", $data);
update_option('moodle_response', $response);
}
return $user;
}
And post it using cURL to Moodle login page.
function post_to_url($url, $data) {
$EMAIL = $data['username'];
$PASSWORD = $data['password'];
$cookie_file_path = plugin_dir_path(__FILE__) . 'cookie.txt';
$agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";
$ch = curl_init();
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
// set postfields using what we extracted from the form
$POSTFIELDS = http_build_query($data);
// change URL to login URL
curl_setopt($ch, CURLOPT_URL, $url);
// set post options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
// perform login
$result = curl_exec($ch);
preg_match('/^Set-Cookie:\s*([^;]*)/mi', $result, $m);
parse_str($m[1], $cookies);
setcookie("MoodleSession",$cookies['MoodleSession'],"Session","/moodle/","domain.com");
//return $cookies['MoodleSession'];
}
**And I stuck** with last line (setcookie...). How I should set moodle session coockie, that when I open moodle in my browser I would be logged in?