PHP

9
Jul

PHP: Increase PHP Memory Limit in htaccess, apache or in a php script

Most of us have come across this error: “Fatal error: Allowed memory size of xxxxxx bytes exhausted (tried to allocate yyy bytes) in ….” in the php error logs.

In php, running out of memory in usually a sign that your php script is not too well written or has a bug or two in it; However sometimes even the best of our php scripts still require more memory than usual especially if it is handling large amounts of data.

Luckily in php, increasing the memory limit is an easy task to do and can be done in a number of ways.

Read more

9
Jul

PHP: Calculate Time Difference Between your Users And Your Server

People access your site aren’t always in the same timezone as you are. Now perhaps you would like to determine what the time is for a user in a different country. Then here is a quick way to find this information out.

This example is very useful if you simply want to fetch your clients date and time or timezone; but also good to see what time of day (their time, not server time) your clients access your website.

Now before I should you the code, remember that PHP is server side code – ie. it runs on the server and as such doesn’t have access to the clients local time. Javascript however is client side code which means it can see the localtime but the servers time. So a combination of the two will allow us to calculate the difference.

The way the below example works, is that the client will click a submit button which sets the value of the hidden input field to their local time stamp and that gets submitted to the same php script. Then the php script checks if that value has been sent and if it has, then it gets the local time (of the server) and takes the clients times and shows a message depicting the difference.

Read more

9
Jul

PHP: Complete List Of File Mime-Types

Below is a list of mime-types I come across most often. Suggest others that should be a part of the list by leaving a comment below. You can also view my article on finding the mime-type of a file by check out my article on PHP Function To Get File Mime Type.

Text

PHP – text/html
HTML – text/html
HTM – text/html
CSS – text/css

Read more

9
Jul

.htaccess : Redirect non www to www

It occurred to me that just maybe you don’t want to display your site with the www subdomain prefix. In that case you could deploy the following code in your .htaccess file to redirect www to non-www. By doing so, users will access your site at http://domain.com not http://www.domain.com.

.htaccess

If your website is hosted on an apache server, a redirect from www to non-www is a simple implementation. Add the following to your .htaccess file

[php toolbar=”true” classname=”one_line”] RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L] [/php]

Add the above code to you .htaccess file and replace domain.com with your domain name.

9
Jul

Using XML: A PHP Developer’s get data

This series of articles will focus on XML, its applications in modern day web development and how PHP fits into this niche. In this article, we will focus specifically on the tools provided to us by PHP which enable us to manipulate XML data sources.

What is XML and Why Use it?

XML (eXtensible Markup Language) is a W3C standard designed to allow the easy exchange, storage and use of data between web applications and services.

Read more

9
Jul

iPad Detection Using JavaScript or PHP

Here are a few methods by which you can detect iPad browser.

Given that string we can create a few code snippets to determine if the user is being a smug, iPad-using bastard.

1.The PHP

This time we look for the position of “iPad” in the user agent string.

[php toolbar=”true” classname=”one_line”] $isiPad = (bool) strpos($_SERVER[‘HTTP_USER_AGENT’],’iPad’);
[/php]

Read more

9
Jul

Android Detection with JavaScript or PHP

Here are a few methods by which you can detect Android browser.

1.The PHP

Again, we’ll use PHP’s strstr function to search for Android in the user agent:

[php toolbar=”true” classname=”one_line”] $ua = strtolower($_SERVER[‘HTTP_USER_AGENT’]);
if(stripos($ua,’android’) !== false) { // && stripos($ua,’mobile’) !== false) {
header(‘Location: http://android.davidwalsh.name’);
exit();
}
[/php]

Read more