Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:31:10+00:00 2026-06-17T09:31:10+00:00

I’m trying to add and if/else to a login action in CakePHP. The login

  • 0

I’m trying to add and if/else to a login action in CakePHP. The login action has a number of lines and when I add my if/else after the LoginValidate, the brackets for the login action now don’t close properly.

The session does get written but, but when using the bracket highlighter in sublime text2, the top most bracket doesn’t get highlighted. Here’s the code. What I’m trying to do is write a session variable for KCFinder to ‘true’ if the user is not in the UserGroup ‘Admin’ and false if the user is in the UserGroup ‘Admin’.

public function login() {
    print_r($this -> Session -> read());
    if ($this->request -> isPost()) {
        $this->User->set($this->data);                                  
        if($this->User->LoginValidate()) {
            $email  = $this->data['User']['email'];
            $password = $this->data['User']['password'];
            $user = $this->User->findByUsername($email);
            $UserGroup = $this->User->UserGroup;

            if (empty($user)) {
                $user = $this->User->findByEmail($email);
                if (empty($user)) {
                    $this->Session->setFlash(__('Incorrect Email/Username or Password'));
                    return;
                }
            }
                //write session value for kcfinder
            if ($user['UserGroup']['name']='Admin') {
                $this -> Session -> write("kcfinder", "false");
                $_SESSION['KCFINDER']['disabled']=false; //config from ckfinder
            } else {
                $this -> Session -> write("kcfinder", "true");
                return;
            }   

            // check for inactive account
            if ($user['User']['id'] != 1 and $user['User']['active']==0) {
                $this->Session->setFlash(__('Sorry your account is not active, please contact to Administrator'));
                return;
            }
            // check for verified account
            if ($user['User']['id'] != 1 and $user['User']['email_verified']==0) {
                $this->Session->setFlash(__('Your registration has not been confirmed please verify your email or contact to Administrator'));
                return;
            }
            if(empty($user['User']['salt'])) {
                $hashed = md5($password);
            } else {
                $hashed = $this->UserAuth->makePassword($password, $user['User']['salt']);
            }

            if ($user['User']['password'] === $hashed) {
                if(empty($user['User']['salt'])) {
                    $salt=$this->UserAuth->makeSalt();
                    $user['User']['salt']=$salt;
                    $user['User']['password']=$this->UserAuth->makePassword($password, $salt);
                    $this->User->save($user,false);
                }
                $this->UserAuth->login($user);
                $remember = (!empty($this->data['User']['remember']));
                if ($remember) {
                    $this->UserAuth->persist('2 weeks');
                }
                $OriginAfterLogin=$this->Session->read('Usermgmt.OriginAfterLogin');
                $this->Session->delete('Usermgmt.OriginAfterLogin');
                $redirect = (!empty($OriginAfterLogin)) ? $OriginAfterLogin : LOGIN_REDIRECT_URL;
                $this->redirect($redirect);
            } else {
                $this->Session->setFlash(__('Incorrect Email/Username or Password'));
                return;
            }

        }

    }
}

additional points:

1) this code comes from a user management plugin from: http://usermgmt.ektasoftwares.com

2) the section that I added is:

                //write session value for kcfinder
        if ($user['UserGroup']['name']='Admin') {
            $this -> Session -> write("kcfinder", "false");
            $_SESSION['KCFINDER']['disabled']=false; //config from ckfinder
        } else {
            $this -> Session -> write("kcfinder", "true");
            return;
        }

The result of adding my session section is that the two top most brackets don’t properly highlight (again using the highlight plugin in sublime text2)

Thanks for the comments and input.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:31:10+00:00Added an answer on June 17, 2026 at 9:31 am

    Check your comparison

    This is an assignment – not a comparison:

    if ($user['UserGroup']['name']='Admin') {
    

    I.e. after this the value of $user['UserGroup']['name'] is set to true, and the code will always enter this if block. This is probably the intention:

    if ($user['UserGroup']['name'] === 'Admin') {
    

    Errors like this are a lot easier to spot with consistent whitespace (the code in the question is quite varied). You can also avoid them using this style:

    if ('Admin' === $user['UserGroup']['name']) {
    

    Which “works” because if you make the same mistake you get a parse error rather than an assignment:

    if ('Admin' = $user['UserGroup']['name']) {
    

    Some bonus comments

    That’s a lot of code

    This login function is pretty big (and it references more code, that isn’t shown). It could be a lot simpler. For example from the book (be sure to compare to the book for the version of cake you’re using):

    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }
    }
    

    There are some additional bits of logic in the code in the question – but there are also chunks of code duplicating what the Auth login function does.

    Use CakePHP

    if you’re using CakePHP – use CakePHP. This line:

    $_SESSION['KCFINDER']['disabled']=false; //config from ckfinder
    

    Is equivalent to:

    $this->Session->write('KCFINDER.disabled', false);
    

    Being consistent, again, makes code easier to read and ultimately easier to maintain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.