Automatic log in

Automatic log in

by Dave Cuttriss -
Number of replies: 7
My client has a requirement for an automatic log in straight to a specific course.
Initially it was just to log onto the site automatically from their main site which was achieved with a URL in the form: http://www.thesite/moodle/post.php?username=****&password=*****

This connected to a form (post.php) with the code:
<?php
$username = stripslashes($_GET['username']);
$password = stripslashes($_GET['password']);
?>
<form action="http://www.thesite/moodle/login/index.php" method="post" name="login" id="form">
<p><input type="text" name="username" value="<?php echo $username ?>">
<p><input type="hidden" name="password" value="<?php echo $password ?>">
<script Xlanguage="JavaScript">
function Validate()
{
document.login.submit();
}
Validate();
</script>
</form>';
?>

I've read all the posts (I think) on this subject but couldn't find a satisfactory answer so I thought that if I passed a new URL in the form:
http://www.thesite/moodle/post.php?username=****&password=*****&requiredpage=**** and recoded post.php to:

<?php
$username = stripslashes($_GET['username']);
$password = stripslashes($_GET['password']);
$requiredpage = stripslashes($_GET['requiredpage']);
?>
<form action="http://www.thesite/moodle/login/index.php" method="post" name="login" id="form">
<p><input type="text" name="username" value="<?php echo $username ?>">
<p><input type="hidden" name="password" value="<?php echo $password ?>">
<script Xlanguage="JavaScript">
function Validate()
{
document.login.submit();
}
Validate();
</script>
</form>';
?>
Then the variable would be passed to login/index.php where I could use it.

I then went to config.php and created an additional entry: $CFG->wwwroot1 = 'http://www.thesite/moodle/course/view.php?id=10';

Then on to login/index.php line 172... where I thought I would put code to determine that if $requiredpage was a certain value then $urltogo = $CFG->wwwroot1.'/'; and the appropriate page would load.
The URL now becomes
http://www.thesite/moodle/post.php?username=***&password=w***&requiredpage=ten

Thus I now have code:
/// Prepare redirection

if ($requiredpage == 'ten'){
$urltogo = $CFG->wwwroot1.'/';
unset($SESSION->wantsurl);

} if (user_not_fully_set_up($USER)) {
$urltogo = $CFG->wwwroot.'/user/edit.php';
// We don't delete $SESSION->wantsurl yet, so we get there later

} else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0)) {
$urltogo = $SESSION->wantsurl; /// Because it's an address in this site
unset($SESSION->wantsurl);

}

This throws me back to the log in page.

However if I use the code: if ($requiredpage != 'ten') then the line
$urltogo = $CFG->wwwroot1.'/'; executes.

I know I'm doing something very stupid (PHP is not my scene) but I can't see the wrong logic.

Any comments, have I made sense?

Dave
Average of ratings: -
In reply to Dave Cuttriss

Re: Automatic log in

by Colin Fraser -
Picture of Documentation writers Picture of Testers
Would this be better off in the Developer forum?
In reply to Colin Fraser

Re: Automatic log in

by Dave Cuttriss -
Hmm..not sure...It seems a bit trifling for developers..Can I leave it overnight and repost if there a\re no answers or will that be against protocol?

Dave
In reply to Dave Cuttriss

Re: Automatic log in

by Matt Jenner -
What do they actually want to achieve with the automated login?

I am assuming guest access is no good, and the presumption is because at some point the automatically logged in user will have to take part in an activity or enter some data somewhere - is this correct?

Sometimes tweaks to the roles and capabilities can do some wonderful things.
In reply to Matt Jenner

Re: Automatic log in

by Dave Cuttriss -
Thanks Matt: It's a long story but yes it's what they want (need logging etc, I won't bore you with why they want to go direct to a course...it's aesthetics on their main site).....I really thought I'd cracked it (although somewhat clumsily) it's just that I can't get the code to work....
In reply to Dave Cuttriss

Re: Automatic log in

by Dave Cuttriss -
There is a slight error in the code of the first post: the new post.php should read:

<?php
$username = stripslashes($_GET['username']);
$password = stripslashes($_GET['password']);
$requiredpage = stripslashes($_GET['requiredpage']);
?>
<form action="http://www.thesite/moodle/login/index.php" method="post" name="login" id="form">
<p><input type="text" name="username" value="<?php echo $username ?>">
<p><input type="hidden" name="password" value="<?php echo $password ?>">
<p><input type="hidden" name="requiredpage" value="<?php echo $requiredpage ?>">
<script Xlanguage="JavaScript">
function Validate()
{
document.login.submit();
}
Validate();
</script>
</form>';
?>

Dave
In reply to Dave Cuttriss

Re: Automatic log in

by Dave Cuttriss -
I've sorted this..I'll post what I did later today.

Dave
In reply to Dave Cuttriss

Re: Automatic log in

by Dave Cuttriss -
So I decided on a URL format to attach to menu items on the main site:
http://www.thesite.com/moodle/post.php?username=*****&password=#####&requiredpage=~~~~~
Where user name and password are derived from a session variable on the main site and required page is a variable to hold the ID number for the page

Post.php contains the code:

<?php
$username = stripslashes($_GET['username']);
$password = stripslashes($_GET['password']);
$requiredpage = stripslashes($_GET['requiredpage']);
?>
<form action="http://www.thesite.com/moodle/login/index.php" method="post" name="login" id="form">
<p><input type="text" name="username" value="<?php echo $username ?>">
<p><input type="hidden" name="password" value="<?php echo $password ?>">
<p><input type="hidden" name="requiredpage" value="<?php echo $requiredpage ?>">
<script language="JavaScript">
function Validate()
{
document.login.submit();
}
Validate();
</script>
</form>';
?>

and passes the 3 variables to login/index.php

I then created a list of potential wwwroot variables in config.php (would probably be better in another file and use include once?) which contained the true URL for the page e.g.

$CFG->wwwroot2 = 'http://www.thesite.com/moodle/course/view.php?id=3'; //Section 1:Beer Types & Their Raw Materials:
$CFG->wwwroot1 = 'http://www.thesite.com/moodle/course/category.php?id=2'; //List of brewing courses
I modified login/index.php ar around line 174:
} else if
// no wantsurl stored or external - go to homepage

to determine the value passed in requiredpage (don't forget to include $requiredpage = $_POST["requiredpage"]; before the IF's) in a series of IF's such as

} else if
// no wantsurl stored or external - go to homepage

($requiredpage == "2") { //List of brewing courses
$urltogo = $CFG->wwwroot1.'/';
unset($SESSION->wantsurl);

//Section 1:Beer Types & Their Raw Materials:
} else if ($requiredpage == "3"){
$urltogo = $CFG->wwwroot2.'/';
unset($SESSION->wantsurl);

It's crude but seems to work.

Dave