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


4 Posts   455 Views


5 July 2012 at 9:51pm (Last edited: 9 July 2012 8:18pm), Community Member, 146 Posts

Hi guys,


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'));
      $cookie = new Cookie;
      $this->setMessage('bad', 'Invalid session');
// 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


6 July 2012 at 5:41am (Last edited: 6 July 2012 5:41am), Community Member, 46 Posts

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

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.


9 July 2012 at 8:20pm (Last edited: 9 July 2012 8:22pm), Community Member, 146 Posts

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 :)



11 July 2012 at 8:29pm Community Member, 46 Posts

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.