Archive for the ‘PHP’ Category

I recently discovered, that push notifications containing the word “(null)” (without the quotes) do not get pushed. They don’t break your APNS connection, simply never reach the recipient’s device.
Most likely, Apple wants to avoid some injection and I’m pretty sure, there are more words out there, that will not push.

In PHP, you can take simple precautions, to work around this:

$message = "(null) bla bla bla bla bla"; //this is our message
$messageFiltered = str_replace("(null)", "", $message);

I discovered this, when trying to push an iCal calendar event. Sometimes, iCal does not add the sender’s name, but (null) instead.

Hope, this is helpful to someone out there.
In case, you know more “forbidden words”, please leave a comment.

When your PHP-MYSQL implementation has to deal with special characters (umlauts), you might wanna pay close attention to your setup. Usually, it doesn’t work right out of the box and if only one tiny piece of your puzzle is missing, chances are high your characters won’t get displayed as desired.

Let’s assume we have some data in our MYSQL database we simply wanna get via PHP. Standard procedure would be something like this:

Connect to database:

<?
$conn = mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db('databaseName', $conn) or die(mysql_error());
?>

Get data and print them on screen:

$sql = mysql_fetch_array(mysql_query("SELECT * FROM myTable"), MYSQL_ASSOC);
echo $sql['fieldInTable'];

In case you are using characters in english language only, the above will work as expected.

But what if your data contain umlauts, like “Ö”, “Ü”, “ß”, etc…? Well, you might see some weird letters instead and that’s what probably brought you here, right? :)

If you do some quick internet research on this topic, you will find countless discussions and possible solitions. However, if none of them worked for you so far, try the following:

Connect to database:

<?
$conn = mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db('databaseName', $conn) or die(mysql_error());
mysql_query("SET NAMES 'utf8'"); //this is new
mysql_query("SET CHARACTER SET 'utf8'"); //this is new
?>

Get data and print them on screen:

$sql = mysql_fetch_array(mysql_query("SELECT * FROM myTable"), MYSQL_ASSOC);
echo utf8_decode($sql['fieldInTable']); //this is new

In case it still doesn’t work, make sure your PHP document is UTF-8 encoded. Depending on the code editor you are using, you should easily be able to change encoding.
For example, in CODA it works like this:

It took me quite some time to figure this out, so hopefully you might find it helpful.

The following script connects to a mailbox, reads an email, fetches some important data and then deletes the email.

This is probably useful, if you want to automatically empty your mailbox on a regular basis and store data in a database.


<?
$mailbox = imap_open("{localhost:143/notls}INBOX", "your@emailaddress.com", "mailboxPassword");  //connects to mailbox on your server

if ($mailbox == false) {
 echo "<p>Error: Can't open mailbox!</p>";
 echo imap_last_error();
}
else {

 //Check number of messages
 $num = imap_num_msg($mailbox);

 //if there is a message in your inbox
 if( $num > 0 ) { //this just reads the most recent email. In order to go through all the emails, you'll have to loop through the number of messages
 $email = imap_fetchheader($mailbox, $num); //get email header

 $lines = explode("\n", $email);

 // data we are looking for
 $from = "";
 $subject = "";
 $to = "";
 $splittingheaders = true;

 for ($i=0; $i < count($lines); $i++) {
 if ($splittingheaders) {
 // this is a header
 $headers .= $lines[$i]."\n";

 // look out for special headers
 if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
 $subject = $matches[1];
 }
 if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
 $from = $matches[1];
 }
 if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
 $to = $matches[1];
 }

 }
 }

 //We can just display the relevant information in our browser, like below or write some method, that will put that information in a database
 echo "FROM: ".$from."<br>";
 echo "TO: ".$to."<br>";
 echo "SUBJECT: ".$subject."<br>";
 echo "BODY: ".imap_qprint(imap_body($mailbox, $num));

 //delete message
 imap_delete($mailbox,$num);
 imap_expunge($mailbox);
 }
 else {
 echo "No more messages";
 }

 imap_close($mailbox);
}
?>

An App, you might like:
We recommend…
You might also like…
Get Adobe Flash playerPlugin by wpburn.com wordpress themes