Setting up PEAR mail on Scotch Box

You have Vagrant and Scotch Box installed. You still need to install Pear.

$ vagrant ssh
$ sudo apt-get -y install php-pear
$ sudo pear install mail_mime

In your PHP script

include_once('Mail.php');
include_once('Mail/mime.php');

Parameters to define SMTP Scotch Box

$params["host"] = "127.0.0.1";
$params["port"] = 1025;
$params["Date"] = date('r',time());
$mail = Mail::factory("smtp",$params);
...

Catch mails in Mailhog at http://192.168.33.10:8025

Getting started with WAMP: setting up

Memory exhausted problem

“Allowed memory size of 134217728 bytes exhausted”
I encountered this problem a few times, and setting memory_limit to a higher value didn’t seem to work

In php.ini:
memory_limit = -1;

Changing www root on WAMP

Open httpd.conf

Change

Listen 0.0.0.0:80
Listen [::0]:80

to

Listen 127.0.0.1:80
Listen [::1]:80

Change

DocumentRoot "C:/wamp/www/"

to

DocumentRoot "C:/Users/name/Sites/localhostwamp/"

Change

</pre>
<Directory "C:/wamp/www/">
<pre>

to

</pre>
<Directory "C:/Users/.../Sites/localhostwamp/">
<pre>

Setting mySQL password

go to shell (right click mysql shell in taskbar)

update mysql.user set Password = password('yourPasword') where User = 'root';
flush privileges;

go to phpmyadmin directory c:\…\wamp2.5\apps\phpmyadmin4.1.14\config.inc.php

replace:

$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';

with

$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['user'] = '';
$cfg['Servers'][$i]['password'] = '';

Or watch this dummy video tutorial:

Set login session to something less restrictive for testing purposes

in your config.inc.php file (apps/phpmyadmin…)

$cfg['LoginCookieValidity'] = 604800; /* 1 week */
$cfg['MaxNavigationItems'] = 1000;

mysql error logging

add these lines to my.ini

log-queries-not-using-indexes
log-warnings
long_query_time = 2
log=C:/Webservers/wamp2/logs/querylog.log
log-slow-queries=C:/Webservers/wamp2/logs/SlowQueryLog.log

import sql-data into mySQL database

Right click Wamp icon in taskbar > mySQL

show databases;
use <databasename>;
source c:\Temp\queries.sql;

mysqldump in wamp (not via mysql console!)

Go to the wamp mysql directory via cmd prompt

cd c:\Webservers\wamp2\bin\mysql\mysql5.6.17\bin\

Now ‘all’ mysql commands are available:

mysqldump -u root -p dbname table1 table2 > output.sql

Multiline regular expression problem in PHP 5.3.3

Recently I came across a very strange behaviour in a regular expression I wrote years ago.

The webserver upgraded to PHP 5.3.3 and it suddenly stopped working.

Case as follows: I use PHP to read a text-file and with the aid of PCRE (preg_match & preg_split) I look for certain matches in that text-file. The program splits different paragraphs into seperate records. But suddenly preg_split stopped working:

<?php
$paragraphs = preg_split("/\n\n/",$contents_of_file);
?>

Multiple newlines could not be detected, whereas

<?php
$lines = preg_split("/\n/",$contents_of_file);
?>

generated no problem at all. At first I thought I had to use the multiline modifier /m but that didn’t work out either.

Although a HEX editor didn’t indicate any carriage returns in the text-file I managed to get things working by doing this:

<?php
$paragrapgs = preg_split("/\r?\n\r?\n/",$contents_of_file);
?>

Curious thing: previous and newer versions of PHP (e.g. 5.3.8 in a XAMPP test environment) did not require to look for possible occurrences of carriage returns (\r).

Flush buffers

(post was updated on 2014/07/31)

Sometimes you want to output HTML to the browser before a script has finished completely. In some browsers it takes a while before ‘some’ content is displayed. Flush buffers can help to actively force output to be shown.

A combination of PHP functions is useful to please all kinds of browsers. However the code below might no longer work in recent Apache/PHP environments. Still works locally on XAMPP

function flush_buffers(){
	ob_end_flush();
	ob_flush();
	flush();
	ob_start();
}

You should read http://stackoverflow.com/questions/4870697/php-flush-that-works-even-in-nginx. Maybe these adjustments will help you to implement the PHP flush functions.

Test code

ob_implicit_flush(1);
for($i=0; $i<10; $i++){
    echo $i;
    //this is for the buffer achieve the minimum size in order to flush data
    echo str_repeat(' ',1024*64);
    sleep(1);
}

Aan de slag met Mercury mail voor XAMPP

Om lokaal wat programmeerwerk als voorbereiding op een live omgeving te verrichten, is XAMPP de ideale omgeving. XAMPP komt pas echt tot zijn recht met de opstelling van een eigen mailserver (Mercury Mail) waarmee je lokaal het verzenden van mails kan testen.

De uitgebreide versie van XAMPP bevat zo’n SMTP service. Bij een eerste kennismaking word je overstelpt door een hele reeks vensters die kris kras op het scherm staan. Om hierin wat wegwijs te raken, is er een uitstekende videotutorial op YouTube:

Een van de belangrijkste zaken die je moet weten bij dit programma is dat voor quasi elke aanpassing het programma opnieuw moet opstarten omdat Mercury Mail in XAMPP geen service is!

De basisinstellingen

  • Configuration: volgende protocul modules aanvinken:
    • Mercury S SMTP server
    • Mercury P POP3 server
    • Mercury E, SMTP end to end client delivery client
    • Mercury D Distributing POP3 client,
    • Mercury I Iamp4 server
  • Configuration
    • Core module
    • Local domains > add local domain
      • Localhost
      • mydomain.com
  • Configuration
    • Mercury S
    • Connection control
    • Uncheck “do not permit SMTP relaying of non-local mail”
  • Configuration
    • Mercury E
    • DNS Servers (open DNS): 208.67.222.222,208.67.220.220
  • Configuration
    • Manage local users: een testgebruik definiëren
      Het is best dat je geen gebruik maakt van postmaster@localhost, omdat deze standaard alle foutberichten opvangt.
    • Username: root
      Personal name: root
      Password: pass

Het enige wat je nu nog moet doen is de configuratie van deze local mail account in je favoriete e-mailprogramma. Daarin volg je dezelfde werkwijze alsof je een remote mailserver zou aanspreken, maar bij de serversettings plaats je gewoon ‘localhost’.

De vooraf gedefinieerde accounts hebben volgende paswoorden:

  • Postmaster: Postmaster (postmaster@localhost)
  • Administrator: Admin (admin@localhost)

User: newuser
Password: wampp

Posting HTML forms with special characters, while keeping your database clean.

The best practice when storing data in a database is to store it in its most purest form.
When allowing users to edit data through HTML webpages you need to encode some characters so your HTML-forms won’t break. You can do this by using htmlspecialchars (or htmlentities). Below is an example with htmlspecialchars where only the double quotes are escaped (ENT_COMPAT flag).

Mind the accept-charset in the form value: I try to work with UTF-8 and UTF-8 only. (see your collation in mySQL is also set to UTF-8!)

You can run this on code on localhost (e.g. XAMPP)

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>htmlspecialchars (utf-8 encoding)</title>
</head>
<body>
<h1>htmlspecialchars (utf-8 encoding)</h1>
<h2>Before form post</h2>
<?php
$dbvalue = "String: ? < > ' - \" `´& % ‰ € ® 2011";
$formvalue = htmlspecialchars($dbvalue, ENT_COMPAT,"UTF-8");
?>
<p><strong>String</strong> is a value coming from a database record in its cleanest form: <span style="color:green;"><?php echo htmlspecialchars($dbvalue); ?></span> </p>
<p>For use in a text form, especially the double quotes, must be encoded so the <em>value=&quot;&quot;</em> doesn't break. We use <strong>htmlspecialchars</strong> (ENT_COMPAT) function. ENT COMPAT only forces double quotes to be changed into &amp;quot; (besides < > ? &)</p>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" accept-charset="UTF-8">
	<label>String:
	<input name="string" type="text" value="<?php echo $formvalue; ?>" size="50" /></label>
	<br />
	The value of title inside this form looks like <span style="color:red;"><?php echo htmlspecialchars($formvalue); ?></span><br />
	<input name="submit" type="submit" value="submit this form" />
</form>
<?php if($_POST){ ?>
<h2>Yes, the form was posted</h2>
<p>When the form is <strong>submitted</strong>, the <strong>string</strong> field will again have a value in its purest form (no &amp;quot; values but &quot;) (not the htmlspecialchars formatting)</p>
<p><strong>String</strong> has submitted value: <span style="color:green;">
	<?php echo $_POST['string']; ?>
	</span></p>
<?php } ?>
</body>
</html>