Login and get sesskey via php file

Login and get sesskey via php file

by Alejandro Hinojosa -
Number of replies: 7

Hi, I need user credentials (sesskey, moodlesession) for a php file that it's executed once a month automatically.

I've tried using POST (curl)  to the login/index.php page but it returns de login page instead of actually login in. 

I need to storage the sesskey and moodlesession into variables for be able to execute some scripts automatically.

Can someone please help me? 

Average of ratings: -
In reply to Alejandro Hinojosa

Re: Login and get sesskey via php file

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
No - you can't do that. sesskey is created as a random string on every login, it is invalid as soon as that login session expires. moodlesession is not valid to store either.

Maybe it would be better to describe what you are trying to achieve with these scripts, and then someone can suggest a better way to achieve what you want.
Average of ratings: Useful (1)
In reply to Alejandro Hinojosa

Re: Login and get sesskey via php file

by Benjamin Ellis -
Picture of Particularly helpful Moodlers
Hi,

For CLI (cron) scripts, you can use cron_setup_user() (sessionlib.php) to set the user.  I assume you are including /config.php in the script?


Average of ratings: Useful (1)
In reply to Benjamin Ellis

Re: Login and get sesskey via php file

by Alejandro Hinojosa -
Yes, Im including it. I assume that i need to call cron_setup_user() before i use the  sessionkey & moodlesession right?
In reply to Alejandro Hinojosa

Re: Login and get sesskey via php file

by Benjamin Ellis -
Picture of Particularly helpful Moodlers
Hi,

That function will set up the necessary session so your script runs as if it is the actual user running it. As Davo replied, you should not be storing the session variables as they change for every session.
In reply to Benjamin Ellis

Re: Login and get sesskey via php file

by Alejandro Hinojosa -
Ok, but is the moodlesession cookie generated too? Thank you.
In reply to Alejandro Hinojosa

Re: Login and get sesskey via php file

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
There is no valid reason, that I can think of, why a scheduled script should need a moodlesession cookie - that is not a sensible way of automating anything on a Moodle site.

If you want to be able to call a function on a Moodle site from outside, then you want to do this via a webservice (either an existing webservice or create a new custom webservice, if there isn't already code to do what you want).

If, instead, you want an internal action to take place within Moodle, then create a plugin with a scheduled task, that runs your code internally, without needing to mess around with session cookies, etc.

Both those methods are covered in the Moodle developer docs, but feel free to ask further questions if you are stuck.
Average of ratings: Useful (3)
In reply to Davo Smith

Re: Login and get sesskey via php file

by Alejandro Hinojosa -
Hi Davo, thanks for your answer, I think that I have to reconsider what I really need, keeping in mind what you said. Thank you.