<p>You're probably looking for a combination of:</p>

<p><a href="https://docs.craftcms.com/api/v2/craft-usersessionservice.html#method-login" rel="nofollow noreferrer">craft()->userSession->login()</a></p>

<p>or</p>

<p><a href="https://docs.craftcms.com/api/v2/craft-usersessionservice.html#method-loginbyuserid" rel="nofollow noreferrer">craft()->userSession->loginByUserId()</a></p>

<p>along with setting the <a href="https://docs.craftcms.com/v2/config-settings.html#usersessionduration" rel="nofollow noreferrer">userSessionDuration</a> config setting to a really high value.</p>

CC BY-SA 4.0
2018-10-03T20:44:57.867Z
Have these linked resources moved to a new URL?
buck1112
CC BY-SA 4.0
2018-10-06T08:28:20.683Z
@buck1112 yup... updated!
Brad Bell
CC BY-SA 4.0
2015-02-25T19:17:38.327Z
2018-10-06T08:28:08.890Z
2018-10-06T08:28:08.890Z
Brad Bell
<p>In my Controller I created a script to add a user to the database, is there a way I can automatically:</p>

<p>1) Log them in and 
2) Set a permanent session cookie or similar?</p>

<p>I basically don't want the user to have to ever log in and we will be creating the accounts for them.</p>

<plugin-development><users><cookies>
Automatically Log in User
# Automatically Log in User

(Asked by: username_0 on 2015-02-25)

In my Controller I created a script to add a user to the database, is there a way I can automatically:

1) Log them in and
2) Set a permanent session cookie or similar?

I basically don't want the user to have to ever log in and we will be creating the accounts for them.

**Tags:** plugin-development, users, cookies

---

## 1 answers

### Answer by username_1 on 2015-02-25. Score: 3

You're probably looking for a combination of:

[craft()->userSession->login()](https://docs.craftcms.com/api/v2/craft-usersessionservice.html#method-login)

or

[craft()->userSession->loginByUserId()](https://docs.craftcms.com/api/v2/craft-usersessionservice.html#method-loginbyuserid)

along with setting the [userSessionDuration](https://docs.craftcms.com/v2/config-settings.html#usersessionduration) config setting to a really high value.

> **Comments:**
> **username_2**: Have these linked resources moved to a new URL?  
> **username_1**: @buck1112 yup... updated!  

---

