SyntaxHighlighter

Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Friday, 7 December 2012

UTF8 / Unicoding Body of PHP PEAR Email

Further to the previous article I wrote about unicoding an email subject in PHP, I was getting issues with the body not UTF8 encoding correctly either. When I used the Mail_mime object (as I was sending attachements), the standard Content-Type of text/html; charset=utf-8 in the headers was not enough.

I hunted around and came across a comment posted on the PEAR manual site. So courtesy of fredrik@krafftit.se I modded my code as follows and all worked well :)

$mimeParams = array(
 "text_encoding" => "8bit",
 "text_charset" => "UTF-8",
 "html_charset" => "UTF-8",
 "head_charset" => "UTF-8"
);

$body = $mime->get($mimeParams);

Friday, 15 June 2012

Process Different File Types with PHP in Plesk

So after having got PHP to process different files types in IIS7, the Plesk version of the site and using the .htaccess was giving me some grief. I came across this article on the parallels forum and got it work  by using a slightly modified version of the working example given.

AddHandler php-script .js


I needed PHP to process JavaScript files.

Tuesday, 22 May 2012

Installing libmcrypt / mcrypt Module on Plesk

Had a major ball ache today getting the mcrypt module working on a plesk hosted VPS. This article was a god send!!


A lot less hair was lost :)

The crux of it is the that from the shell do the following:

> yum install libmcrypt-devel
If 32 bit do:
> yum install php-mcrypt
If 64 bit do:
> yum install php-mcrypt.x86_64

Process Different File Types with PHP in IIS7

A slight follow on from an earlier post I made about custom error pages in IIS7, I needed to get IIS to process JavaScript files in PHP. This is really useful if you need to do some pre-processing before the JS hits the browser. In order to get this to work I added the following to the Web.config file in the root of the site:


    
     
      
      
     
    


Note that I am using PHP in FastCGI mode.

Also, make sure you set scriptProcessor to the path of your php-cgi.exe. The other thing I had to do, as Chrome was reporting a Resource interpreted as Script but transferred with MIME type text/html warning when the JavaScript file was loaded, was to add header("Content-type: text/javascript; charset: UTF-8"); to the js file.

One thing you can do to speed things up is specify a specific file in the path parameter of the Web.config. For example to only process my_js_file.js use path="my_js_file.js". And of course, you can add Web.config files in sub-folders so only certain files types are processed with PHP in those folders.

Tuesday, 15 May 2012

Configure IIS7 for Custom Error Pages

I've recently (and finally!) moved to Windows 7 and with it IIS 7. I got PHP installed (using FastCGI) all fine, but was having major issues getting my custom 404 handlers to work with PHP. I found loads of articles all over the web that directed me in the right direction. But no one single article worked for me so this is what worked for me...

All the following settings can be done via the IIS Manager (Control Panel > Administrative Tools), but for me, I created the following Web.config file and put that in the root of the site that I wanted the 404 handler to be working on.


    
        
            
            
        
    


Note the use of errorMode="Custom" and existingResponse="Replace". Hope this works out for you :)

Friday, 9 March 2012

How To Call PHP From a Cron in Plesk

Like a lot of people I needed to run a PHP script from a cron (crontab) via the Plesk server admin tool. Simply putting the path to the file (from root) does not run it. The way to do this is by calling the php processor with some parameters. So do the following:

php -q /var/www/vhosts/mywebsite.com/httpdocs/cron/my-file.php

Obviously use the path that relates to your file.

This seems to work for a lot of people, but as my script had includes in it I was getting failure to open stream errors when it was called. So to get round this, I changed the directory prior to the call. So it now looks like this:

cd /var/www/vhosts/ mywebsite.com /httpdocs/cron; php -q  my-file .php

Hey presto, it all works a treat now!



You may need to set permissions on the script to be executed. By default, the task will email the output of the script to you (click the Settings option on the Scheduled Tasks page to change the address - see image below). This can be turned off by adding 2>&1 to the end of the command. making the it look like this:

cd /var/www/vhosts/ mywebsite.com /httpdocs/cron; php -q  my-file .php /dev/null 2>&1

Credit to http://daipratt.co.uk/crontab-plesk-php/ and http://stackoverflow.com/questions/3140675/php-cron-job-including-file-not-working for helping to work this mess out ;)

Friday, 2 March 2012

PHP: Last Modified Date of Folder / Directory

It turns out it's really easy to get the the last modified date of a folder/directory using PHP. You use the filemtime() function. It returns a timestamp for us so we would do something like:

$lastModifed = filemtime($dir);
print "Last modified on ".date("Y-m-d H:i:s", $lastModifed );

Wednesday, 14 December 2011

PHP header 200 Status Not Being Sent

I'm having a real mare with this PHP site I am working at the moment. Having constant configuration problems - grrr!

I am using a .htaccess file to handle the a 404 (ErrorDocument 404 /404.php). It's used to handle friendly URLs mainly. One of the other things I use it for is allowing JavaScript files to be PHP processed before they are served to the browser. This is dead useful as I can modify and customise the scripts where required.

Anyway...
In the code I use header("HTTP/1.1 200 OK") and in nearly all instances that works fine. Except for when it is processing the the JavaScript files. After massive annoyance and trying loads of different things, it turns out there is a bug in PHP 5. As they suggest at the bottom, the workaround is:

header("Status: 200 OK");

Fixed!

Tuesday, 18 October 2011

Unicode Characters in an Email Subject using PHP and PEAR

I've been working on a PHP site recently where I have had to cater for all languages and more importantly Unicode languages.

So after quite a lot of messing about and hunting around I found that I needed to do the following with the subject:

if (function_exists("mb_internal_encoding"))
 mb_internal_encoding("UTF-8");
if (function_exists("mb_encode_mimeheader"))
 $subject = mb_encode_mimeheader($subject, "UTF-8", "B", "\n");

Obviously if you know if the mb_* functions are available, then you can do away with the if statements.

This works using the PEAR Mail_Mime library. I haven't tested it with the built-in PHP mail function. I would assume it does not work.

Monday, 31 January 2011

PHP: String StartsWith / EndsWith

In case you need the PHP equivalent to the .NET String.StartsWith and String.EndsWith, here they are:


public static function StringStartsWith($string, $search) {
 $ret = false;
 if (strlen($string) >= strlen($search)) {
  $ret = (substr($string, 0, strlen($search)) == $search);
 }
 return $ret;
}

public static function StringEndsWith($string, $search) {
 $string = strrev($string);
 $search = strrev($search);
 return Utils::StringStartsWith($string,$search);
}

Hope it helps :)

Tuesday, 16 November 2010

PHP File / Page Names Containing Only Numbers

Just had a pretty cunning one where having moved a site to a new server running PHP version 5.3.3 pages/files that were named with only numbers (e.g. 123.php) raised a 500 internal server error page.

This had clearly not been a problem on the previous Windows (IIS) server running PHP 5.2, but was on this one. Now whether or not this was a PHP version difference or a server difference, I don't know.

I fixed it by renaming the file to contain at least 1 letter. So it is now named 123a.php.

Wednesday, 11 August 2010

Call to undefined function mcrypt_module_open()

I was getting a Call to undefined function mcrypt_module_open() in PHP 5. All my Google searches were giving me the usual "make sure php_mcrypt.dll is uncommented in your php.ini". Mine was and still no luck :(

Solution for me was to add libmcrypt.dll into the system32 folder - sweeet! My libmcrypt.dll was located in the root of my PHP install folder.

Happy encrypting :)

Labels

.net (7) ajax (1) android (7) apache (1) asp.net (3) asus (2) blogger (2) blogspot (3) c# (16) compact framework (2) cron (1) css (1) data (1) data recovery (2) dns (1) eclipse (1) encryption (1) excel (1) font (1) ftp (1) gmail (5) google (4) gopro (1) html (1) iis (3) internet explorer IE (1) iphone (1) javascript (3) kinect (1) linux (1) macro (1) mail (9) mercurial (1) microsoft (3) microsoft office (3) monitoring (1) mootools (1) ms access (1) mssql (13) mysql (2) open source (1) openvpn (1) pear (2) permissions (1) php (12) plesk (4) proxy (1) qr codes (1) rant (4) reflection (3) regex (1) replication (1) reporting services (5) security (2) signalr (1) sql (11) sqlce (1) sqlexpress (1) ssis (1) ssl (1) stuff (1) svn (2) syntax (1) tablet (2) telnet (3) tools (1) twitter (1) unix (3) vb script (3) vb.net (9) vba (1) visual studio (2) vpc (2) vpn (1) windows (4) woff (1) xbox 360 (1)