Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

How to render page as normal if a condition isn't met


Reply


4 Posts   527 Views

Avatar
kinglozzer

Community Member, 171 Posts

5 July 2012 at 9:51pm

Edited: 09/07/2012 8:18pm

Hi guys,

EDIT:

Sorted the first question!

First question:

I was creating an imaginary customer login section on my site, and wanted to show them a login form if they didn't have cookies set for username and password, or show them their personal page if they'd logged in & had the credentials stored correctly. Just as a learning task.

The problem I had was, while I knew how to render a page different from the one I was currently in the controller for (in this case, if they were logged in), but I didn't know how to render the original page manually (my 'if' statement needed an 'else'). Here was the solution:

if (($email = $cookie->get('ClientEmail')) && ($pass = $cookie->get('ClientPassword')))
{
   $where = "Client.ClientEmail = '" . $email . "'";
   $where .= " AND Client.ClientPassword = '" . $pass . "'";
   
   if($client = DataObject::get_one("Client", $where, "", "", ""))
   {
      return $this->customise(array('Client' => $client))->renderWith(array('ClientPage', 'Page'));
   }
   else
   {
      $cookie = new Cookie;
      $cookie->forceExpiry('ClientEmail');
      $cookie->forceExpiry('ClientPassword');
      $this->setMessage('bad', 'Invalid session');
      Controller::redirectBack();
   }
}
else
{
// This is the code I was originally asking for, ClientHolder is the template & controller I am currently working in
   $page = Director::get_current_page();
   return $this->renderWith(array('ClientHolder', 'Page'));
}

Bonus question:

In this 'if' statement

if (($email = $cookie->get('ClientEmail')) && ($pass = $cookie->get('ClientPassword')))

I had to evaluate both conditions separately, whereas normally I would remove the extra brackets like this

if ($email = $cookie->get('ClientEmail') && $pass = $cookie->get('ClientPassword'))

However, with the brackets removed, $email always returned '1'. Any idea why this would be? Thanks

Avatar
jak

Community Member, 46 Posts

6 July 2012 at 5:41am

Edited: 06/07/2012 5:41am

Hi!
It would have been nice if you had left the original question in place and just added the answer - that way other people can find it.

Regarding your bonus question: I think it has to do with operator precedence. In addition to the well known "multiplication before addition", there are rules as to which operators are evaluated first. The assignment operator (=) is evaluated after &&. It is one of the last ones, see http://at2.php.net/manual/en/language.operators.precedence.php

Without brackets, php probably interprets your code as:
if ($email = ($cookie->get('ClientEmail') && ($pass = $cookie->get('ClientPassword'))))

If you really want to loose the brackets, you can use "and" instead of "&&", but I would strongly recommend to keep them. If someone else (or you yourself) later comes across the code, he will have no clue why the "and" is there.

Avatar
kinglozzer

Community Member, 171 Posts

9 July 2012 at 8:20pm

Edited: 09/07/2012 8:22pm

Hi Jak,

Thanks for clearing that up, I always had a feeling there was a difference between '&&' and 'and', but was never sure what! I usually use 'AND' (capitalised for readability) but it seems this would've been below '&&' in the order of precedence anyway.

I've also edited my original post and readded the first question :)

Thanks

Avatar
jak

Community Member, 46 Posts

11 July 2012 at 8:29pm

Your welcome, and thanks for putting the question and answer back!

I actually think it's a really bad idea that PHP has these subtle differences. It's just not something you would expect if you don't look it up.