it's not so easy, because login.php would set a cookie on the logged user's browser, and while it's true that you can capture and parse the cookie with CURL, unless you try to do this from the same domain you won't be able to replicate an useful cookie.
If you're on the same domain, even in a subdomain, then it's doable tough not very straightforward.
I've tried sending login vars with ajax too, but login.php seems unwilling to accept them