登录到其他站点时自动登录moodle

wm9*_*m90 3 php cookies curl moodle

我想知道是否可以在登录其他网站后自动登录moodle?我尝试使用curl但似乎没有真正起作用.

这是我的测试代码:

//set POST variables
$url = "http://moodlesite.com/login/index.php";

$fields = array(
    'username' => 'username',
    'password' => 'password'
);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)

我注意到在moodle登录系统后,他们再次将其重定向到http://moodlesite.com/login/index.php?testsession=userid,他们在这种情况下需要cookie.

小智 8

您可以创建一个自定义页面登录moodle.

该页面的代码.

<?php
require('config.php');
$name=$_REQUEST['name'];
$password=$_REQUEST['password'];
$dashboard = $CFG->wwwroot;
$user = authenticate_user_login($name, $password);
if(complete_user_login($user))
{
echo "login";
}
else
{
   echo "not login";
}



?>
Run Code Online (Sandbox Code Playgroud)

创建该页面后,您可以传递名称和密码进行登录.