Caching the Data using APC – Alternative PHP Cache

Now a days most of the website are used to cache their data to reduce the server load, Also speed up the response. We are going to discuss about the APC – Alternative PHP Cache.

APC is a Alternative PHP Cache as I already mentioned, its used to cache the data, remove and expire in the given time.  To work with APC first we need to enable php_apc.dll in windows and php_apc.so in Linux operating system.

Where to use APC ?

Consider you have a website with lots of content driven from database with data for all the users, on that time you cannot query the database each and every time. To avoid this case we will cache the data and share between the users. This will allow one time query and share the same data between the users.

Let’s see about fews function in APC.

apc_add – function used to store a data in key format and expire it in given TTL.

apc_add ( array $values [, mixed $unused = NULL [, int $ttl = 0 ]] )

Assigning value into $myData variable

<?php
    $myData = 'Welcome to APC';
    apc_add('myData',$myData);

apc_fetch – function used to fetch data from the given key. Continue reading

Persistent and Non-Persistent Cookie

Cookie used to store user data in form of text file or memory and access the values from it, Cookie are the part of HTTP header.

What is Persistent Cookie ?

Persistent Cookie which will store the data in text file with expire date and time. If you are access the website or particular page cookie will be created based on cookie name.  Next time If you are visiting the website or page it will be accessible from the cookie file which was created in the client browser.

Also Persistent Cookie is unsafe, because it will be easy viewable in the browser.

persistent_cookie.php

<?php
// creating the name:cookie_name_persistent and value:my cookie value with expire 1 hour
setcookie("cookie_name_persistent","my cookie value",time()+3600);

Above PHP file will create cookie and expire within 1 hour, you can view the cookie expire time in browser cookie part. Continue reading

Working with simple Array to CSV, XML and XLS Class in PHP

Most of the Array to CSV, XML and XLS are having different function or class, So we have though of writing all in same class. Class support different type of format like CSV, XML and XLS.

Class is design as calling the static method instead of creating he object, as the current version we have support limited methods.

How it’s work

It’s work based on the format type and date which have header and rows in array format, Once you call the getData function it will communicate the method based on the format type. Then set the header part to download the file.

Setting Up the Data

Three arguments accept in getData function. Continue reading

Debugging server-side script using ChromePHP

I have been working for server and client side scripting,  whenever I got the issue in JavaScript used to debug in console or fire-bug based on the browser and extension.

But it’s not possible in server-side script, if you want server-side script to track a log entry in browser it won’t support. Chrome Logger have the feature to track the log entry in console for server-side script.

They support few platform to display the server-side scripting in console.

  1.  Install Chrome Logger from Google Chrome web store.
  2. Click the extension icon to enable logging for the current tab’s domain (It will light up blue).
  3. Include the ChromePHP file into your script.

Simple example which will log the script-side script into console.

Chrome.php

<?php include 'ChromePhp.php';

// simple function will return content.
function chromePHPExample() {
	return 'Welcome to ChromeLogger, you can see me in Google Chrome Console.';
}

// below function will display the content from the chromePHPExample function 
// print in Google Chrome console
ChromePhp::log(chromePHPExample());

ChromePHP have lots of logging method, you can also check below list. Continue reading

How to change the PHP configuration in 3 ways

PHP has ability to change the configuration setting by using ini_set function, well that’s good to hear. But why it’s need to change the default setting on the fly.

If you want to change the variable value for some purpose instead of disturbing php.ini, here is the option you can go with ini_set in PHP and php_value in htaccess.

If your website is hosted in shared hosting it will be helpful to change instead of changing in php.ini file. Most of shared hosting not allowed to change variable value in php.ini file.

1) ini_set : sets variable value on the fly by using ini_set function, but it won’t change all variable value in php.ini or hosting. It will applicable only for current php or config file which was include.

http://php.net/manual/en/ini.list.php

<?php
//variable newvalue
ini_set("session.name","yoursession");
<?php
//display the variable new value
echo ini_get("session.name");

2) php_value : sets variable value in htaccess file, which will set the variable value in folder and sub folders not for full hosting.

.htaccess file

php_value session.name yoursessionname

3) php.ini : If you are using dedicated hosting you can change the variable value in php.ini file. It will applicable for the hosting.