Code snippets for symfony 1.x

Navigation

Snippets by user Romain Dorgueil

Schema for Creole/DB Session Storage

Here is the schema you need to set up database session storage.

CREATE TABLE `session` (
  `sess_id` varchar(32) NOT NULL,
  `sess_data` text NOT NULL,
  `sess_time` int(11) NOT NULL
);
by Romain Dorgueil on 2006-05-31, tagged cookie  creole  database  mysql  session  storage  user 
(3 comments)

Little trick to randomize results

More a PHP trick than symfony's one, but as you can't with propel randomize order of results, just do:

$c = new Criteria()
... fill your criteria there ...
$result = MyTablePeer::doSelect($c);
shuffle($result);

obvious? sorry, seen someone asking on IRC once :D

by Romain Dorgueil on 2006-05-29, tagged column  criteria  database  order  propel  random 
(8 comments)

How to add HTTP Auth to symfony

Here is a little hack to use http auth when credentials or auth is insufficient:

public function executeSecure()
  {
    if (!$this->getUser()->hasAttribute("secure_referer"))
        $this->getUser()->setAttribute("secure_referer", $this->getRequest()->getReferer());
 
    if (!isset($_SERVER['PHP_AUTH_USER']))
    {
      header('WWW-Authenticate: Basic realm="Member Area"');
      header('HTTP/1.0 401 Unauthorized');
 
      return sfView::NONE;
    }
    else
    {   
        if ($this->getUser()->tryLogin($_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']))
        {
            return $this->redirect($this->getUser()->getAttribute("secure_referer"));
        }
        else
        {
          header('WWW-Authenticate: Basic realm="Member Area"');
          header('HTTP/1.0 401 Unauthorized');
 
          return sfView::NONE;
        }
    }
  }

No template is needed, as everytime you access it will redirect to the referer. Then change in app/yourapp/config/settings.yml the secure_module and secure_action to match this module.

You will need a myUser::tryLogin function that returns a boolean saying "auth is ok" or "bad auth"

And then you're done :p

[from my Wiki Post ab out that]

by Romain Dorgueil on 2006-05-25, tagged authentication  credentials  http  user 
(7 comments)

How to choose domain and path for session cookie...

<?php
class mySessionStorage extends sfMySQLSessionStorage
{
    public function initialize ($context, $parameters = null)
    {
        session_set_cookie_params ( 90*24*3600 , "/", ".domain.tld" );
        parent::initialize($context, $parameters);
    }
}
?>

Then you have to put in factories.yml

all:
  storage:
    class: mySessionStorage
    param:
      database: ...
      db_table: session
      db_id_col: sess_id
      db_data_col: sess_data
      db_time_col: sess_time
      session_name: ...
by Romain Dorgueil on 2006-05-22, tagged cookie  domain  factory  session  storage 
(6 comments)