Skip to content

Instantly share code, notes, and snippets.

@mattattui
mattattui / gist:4352338
Created December 21, 2012 11:47
Zend Mail and Swift Mailer examples
<?php
// Zend Mail
use Zend\Mail;
$mail = new Mail\Message();
$mail->setBody('This is the text of the email.');
$mail->setFrom('web@example.org', 'My site');
$mail->addTo('recipient@example.com', 'Their name');
$mail->setSubject('Test Subject');
@mattattui
mattattui / example.php
Created December 21, 2012 11:45
Sample Twig setup and template
<?php
require __DIR__.'/../vendor/autoload.php';
$loader = new Twig_Loader_Filesystem(__DIR__.'/../templates');
$twig = new Twig_Environment($loader, array(
// Uncomment the line below to cache compiled templates
// 'cache' => __DIR__.'/../cache',
));
$name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_STRING);
@mattattui
mattattui / gist:4246470
Created December 9, 2012 18:52
Custom validators (regex and callback) with the Symfony HTTP Foundation component
<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
// Real request data
// $request = Request::createFromGlobals();
// Or fake it (for testing)
// e.g. http://example.com/script.php?page=home
@mattattui
mattattui / app.yml
Created September 13, 2012 11:50
Symfony 1.4 versioned stylesheets/css
app:
# ...
resource_version: <?php echo date_create()->format('YmdHis');
@mattattui
mattattui / index.html
Last active November 17, 2023 13:55
Count online users
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Pinger test</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
@mattattui
mattattui / Lorem.php
Created December 16, 2011 12:06
Generate fake text with optional paragraphs and html
<?php
function lorem($length = 5, $paragraphs = false, $html = false)
{
$corpus = array('ad', 'adipisicing', 'aliqua', 'aliquip', 'amet', 'anim', 'aute', 'cillum', 'commodo', 'consectetur', 'consequat', 'culpa', 'cupidatat', 'deserunt', 'do', 'dolor', 'dolore', 'duis', 'ea', 'eiusmod', 'elit', 'enim', 'esse', 'est', 'et', 'eu', 'ex', 'excepteur', 'exercitation', 'fugiat', 'id', 'in', 'incididunt', 'ipsum', 'irure', 'labore', 'laboris', 'laborum', 'lorem', 'magna', 'minim', 'mollit', 'nisi', 'non', 'nostrud', 'nulla', 'occaecat', 'officia', 'pariatur', 'proident', 'qui', 'quis', 'reprehenderit', 'sed', 'sint', 'sit', 'sunt', 'tempor', 'ullamco', 'ut', 'velit', 'veniam', 'voluptate');
// If HTML tag given, figure out the closing tag
$open_tag = $closing_tag = false;
if ($html)
{
@mattattui
mattattui / hash_pbkdf2.php
Last active May 17, 2016 09:23
PHP PBKDF2 implementation
<?php
/* PHP PBKDF2 implementation
*
* PBKDF2 is a key derivation function defined in RFC2898. It's used to
* generate longer and more secure passwords from short, human-entered
* passwords. The number of rounds can be increased to keep ahead of
* improvements in CPU/GPU performance.
*
* You should use a different salt for each password (it's safe to store it
@mattattui
mattattui / gist:1130644
Created August 7, 2011 18:49
Starting a Symfony2 project with Git
  1. Download the Symfony2 standard distribution without vendors

  2. Unzip/untar the distribution. It will create a folder called Symfony with your new project structure, config files, etc. Rename it to whatever you like.

  3. Create a new file called .gitignore and paste the following into it:

      web/bundles/  
      app/bootstrap*  
      app/cache/*  
      app/logs/*  
      build/  
    

vendor/

@mattattui
mattattui / JobRouteExtension.php
Created July 20, 2011 17:27
Sample Object Route Extension
<?php
namespace Me\MyBundle\Extension;
use Me\MyBundle\Entity\Job;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class JobRouteExtension extends \Twig_Extension
{
private $generator;
@mattattui
mattattui / gist:879303
Created March 21, 2011 11:03
Safely handling a PHP file upload
<?php
if (isset($_FILES['myfile'])) {
if ($_FILES['myfile']['error'] != UPLOAD_ERR_OK) {
// Handle your error here, the upload didn't work.
my_upload_error();
} elseif (!is_uploaded_file($_FILES['myfile']['tmp_name'])) {
// File was not uploaded legitimately. Handle as you see fit.
my_upload_error();