Caching is one of the most important things to keep in mind when you are developing a website which is expected to get large amount of visitors. Most web developers neglect caching, some even don’t know correct ways to implement it.
In this tutorial, I’ll show you how to enable browser cache of static files simply using htaccess file. Basically server adds the Expires header to file-type you specify. Expires header will make sure the browser stores the file locally and use the same file until the time mentioned in expires header has passed.
Copy the following code and paste it into .htaccess file in your website’s root directory.
# Turn on the Expires engineExpiresActiveOn# Expires after a month client accesses the fileExpiresByType image/jpeg A2592000
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType text/plain A2592000
# Good for one weekExpiresByType application/x-javascript M604800
ExpiresByType text/css M604800
ExpiresByType text/html M604800
Explanation:
ExpiresActive On turns on the expires engine. ExpiresByType image/jpeg tells server to check if MIME-type of file is image/jpeg. If yes, set the expires header. Read the rest of this entry »
You can enable or disable any apache module using a2enmod and a2dismod. You don’t need to edit the conf file for that unless you are having some problem with this method. The syntax of these commands is really simple: To enable a module:
We can check the php configuration, apache’s loaded modules and all other kinds of PHP configuration using a simple function called phpinfo(). Create a file called infophp.php in your web’s root directory and put the following code in it:
<?phpinfo();?>
This would output all the php configuration info and apache information. You can pass on an argument to phpinfo() to limit what information is displayed. You can checkout the phpinfo documentation here.
PS: This is a beginner tutorial for absolute beginners.
There’ve been lots of times when I wanted to use the mail() function on my local server. I am sure lots of you would’ve been wanting it too but most of you would’ve settled for PHPMailer or just used a web host to test the code instead. I myself had been doing the same until recently when I finally decided to do some research and get it to work.
Here are the steps in short for the geeks who like to do things on their own: All I did was used smtp client called msmtp, configured it to work with my gmail account and configured PHP to use msmtp to send emails.
This tutorial is only applicable for linux users. I’ll write another article for windows users soon when I get my hands on a windows box. All the commands used in the instructions are for Ubuntu, however you may use corresponding commands for your distro (for eg; you can use yum install instead of apt-get install on fedora, redhat, centos.
Here’s a function which thoroughly checks email-address and returns true for a valid email address and returns false for invalid one. I thought it was worth sharing so I posted it on my blog. You will find the source link at the bottom.
/**
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email
address format and the domain exists.
*/function validEmail($email){$isValid=true;$atIndex=strrpos($email,"@");if(is_bool($atIndex)&&!$atIndex){$isValid=false;}else{$domain=substr($email,$atIndex+1);$local=substr($email,0,$atIndex);$localLen=strlen($local);$domainLen=strlen($domain);if($localLen<1||$localLen>64){// local part length exceeded$isValid=false;}elseif($domainLen<1||$domainLen>255){// domain part length exceeded$isValid=false;}elseif($local[0]=='.'||$local[$localLen-1]=='.'){// local part starts or ends with '.'$isValid=false;}elseif(preg_match('/\\.\\./',$local)){// local part has two consecutive dots$isValid=false;}elseif(!preg_match('/^[A-Za-z0-9\\-\\.]+$/',$domain)){// character not valid in domain part$isValid=false;}elseif(preg_match('/\\.\\./',$domain)){// domain part has two consecutive dots$isValid=false;}elseif(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',str_replace("\\\\","",$local))){// character not valid in local part unless // local part is quotedif(!preg_match('/^"(\\\\"|[^"])+"$/',str_replace("\\\\","",$local))){$isValid=false;}}if($isValid&&!(checkdnsrr($domain,"MX")||checkdnsrr($domain,"A"))){// domain not found in DNS$isValid=false;}}return$isValid;}
My host recently started allowing remote connections to its mysql database. It was a really useful feature – I could not change database settings from my own computer. Since it started allowing remote connections from my IP, I could use any mysql client to connect to it. I fired up terminal and tried to connect using the command:
mysql --host=69.89.31.242 --user=username --password=password
It connected. This was just to test the connection. I wanted to connect my phpmyadmin with this remote host. For that I edited /etc/phpmyadmin/config-db.php file and changed:
$dbserver=”;
to
$dbserver=’69.89.31.242′;
I loaded the url http://localhost/phpmyadmin, entered the database username and password and I got in. I could administer my database from my local phpmyadmin installation.
One of my clients got this error in his wordpress:
Error establishing a database connection
It is one of those vague errors which give very little information about what caused it. On going to /wp-admin, it gave error:
One or more database tables are unavailable. The database may need to be repaired.
I added “define(‘WP_ALLOW_REPAIR’, true);” in wp-config.php and clicked on ‘repair’, it showed that all tables have been repaired but I was still getting the error about wordpress not being able to connect with database. It was actually able to connect but there was some other problem. I checked if any table was missing from the database, but it was not.
Finally after 2 hours of searching for a solution, I found it. In wp-options table, just make sure the option_value for the option_name ‘siteurl’ is your site’s url. You can also do this by executing the following sql statement (you can use phpmyadmin): Read the rest of this entry »
We all know about functions and how we can pass arguments to it. But did you know that we can pass variable number of arguments in a function?
Consider a function sum() which adds numbers passed into its arguments. Now we want it to add all the numbers passed onto the arguments. Eg; if we call sum (21, 45) or sum (45, 23, 78, 56, 90) it should add all of them. Even if we pass a 100 arguments, it should execute successfully.
To backup all or some of your MySQL databases, you’ll need mysqldump which comes bundled with mysql. If you have MySQL installed, you probably have mysqldump installed already.
To backup all databases use the following command: