I am storing user information (firstname,lastname,username,email,password) in custom object.If user enter valid username and password in login page then user redirect to home page with username as parameter in URL. In home page, I am getting user details based on parameter.

String username = ApexPages.currentPage().getParameters().get('uname');
Sobject s=[Select fields from sObject Where username__c=:username];

If user remove the uname parameter from URL and refresh the page, then its not possible to get user details. But still user is logged in.

How to maintain logged in user username until user logout from the page (if user refresh the page without uname parameter).

Is there any way to achieve this using apex coding and custom object?

  • A new mechanism that might be of help is the Session Cache. Also see Store and Retrieve Values from the Session Cache.
    – Keith C
    Commented Oct 7, 2015 at 11:16
  • If you are writing your own login code, you've got a few bigger problems than people dropping URL params from their query string. First, you're storing passwords in plaintext which is a BAD IDEA. Even more concerning, by implementing your own authentication on top of Force.com Guest Users, you are in blatant violation of your Master Subscription Agreement and Salesforce can shut you down at a moments notice with no recourse. Commented Oct 12, 2015 at 16:00

1 Answer 1


you can use cookies to store information and once user click on Logout button then simply clear the cookies

// A Visualforce controller class that creates a cookie
// used to keep track of how often a user displays a page
public class CookieController {
public CookieController() {
    Cookie counter = ApexPages.currentPage().getCookies().get('counter');

    // If this is the first time the user is accessing the page, 
    // create a new cookie with name 'counter', an initial value of '1', 
    // path 'null', maxAge '-1', and isSecure 'false'. 
    if (counter == null) {
        counter = new Cookie('counter','1',null,-1,false);
    } else {
    // If this isn't the first time the user is accessing the page
    // create a new cookie, incrementing the value of the original count by 1
        Integer count = Integer.valueOf(counter.getValue());
        counter = new Cookie('counter', String.valueOf(count+1),null,-1,false);

    // Set the new cookie for the page
    ApexPages.currentPage().setCookies(new Cookie[]{counter});

// This method is used by the Visualforce action {!count} to display the current 
// value of the number of times a user had displayed a page. 
// This value is stored in the cookie.
public String getCount() {
    Cookie counter = ApexPages.currentPage().getCookies().get('counter');
    if(counter == null) {
        return '0';
    return counter.getValue();

} You can also use Javascript cookies but keep in mind that

The setCookies method adds the prefix “apex__” to the cookie names. so in javascript cookies append apex__ .

javascript Cookies Example

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
  • In my scenario user not registered, just storing user data in custom object. Commented Oct 7, 2015 at 9:13
  • so you can store uname in cookies and on logout clear the cookies or set some expiration time. Commented Oct 7, 2015 at 9:15
  • Is below way correct to set cookie? Cookie unameCookie = new Cookie('uname',uname,null,-1,false); ApexPages.currentPage().setCookies(new Cookie[]{unameCookie }); If yes, then once user remove the uname parameter from URL, then from where i will get uname value. Commented Oct 7, 2015 at 9:27
  • just read my code sample . In first line i am queryng cookie if it is null then its firstn otherwise you will get the value. You don't need to store it in the URl Commented Oct 7, 2015 at 9:34
  • @Sharma- I have set cookie value in login page for successful login and page redirect to another vf page. How can i pass that cookie value to another page. Commented Oct 7, 2015 at 12:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .