php

PHP Language Detection

Posted by on 15 Oct 2011 at 2:56 am

There is a very simple method to detect what language the users web browser is using. The following code: //session variable ensures it’s available //throughout current session $_SESSION['lang'] = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); echo $_SESSION['lang']; Will give you the correct two letter ISO code for the language the browser is using.

Copy file from remote server

Posted by on 20 Aug 2011 at 2:07 am

Use this script to copy a file from a remote server and save on your own server. <?php $feed = “http://www.domain.com/thefile.pdf”;// url to original $copydir = “/home/webdev/public_html/copies/”; //directory to copy to (CHMOD 777) $data = file_get_contents($feed); $file = fopen($copydir . “thefile.pdf”, “w+”); fputs($file, $data); fclose($file); ?>

Use php in .xml files

Posted by on 6 Aug 2011 at 7:18 pm

Using PHP and MySQL in XML sitemaps is quite easy. The first step is to use the AddType directive. The second step is to add the following code to the top of your .xml file: <?php session_start(); unset($_SESSION['aErrors']); header(‘Content-type: text/xml’); echo ‘<?xml version=”1.0″ encoding=”UTF-8″ ?>’; ?>

AddType Directive

Posted by on 3 Aug 2011 at 9:47 pm

The AddType directive is added to the .htaccess file in the directory, to modify the default mimetypes of the files you wish to be effected. A common use of the AddType directive is if you wish to use PHP in .xml files for dynamic xml sitemaps or .html files. The syntax is quite simple: AddType [...]

PHP/MySQL connection script

Posted by on 31 Jul 2011 at 6:26 am

Many of us use more than one server during the entire project lifecycle. To ensure that you are able to connect to the various versions of the mysql database on each server, use this php connection script: <?php $host = $_SERVER['HTTP_HOST']; // which server is it running on switch ($host) { case ‘localhost’: $server=”localhost”; $user=”root”; [...]