Increase PHP Script Execution Time Limit

Every once in a while we need to process a HUGE file. Though PHP probably isn’t the most efficient way of processing the file, we usually use PHP because it makes coding the processing script much faster. To prevent the script from timing out, I need to increase the execution time of the specific processing script.

How to fix

<?php
     //300 seconds = 5 minutes
     ini_set('max_execution_time', 300);
?>

Place this at the top of your PHP script.

Permanent link to this article: https://blog.openshell.in/2010/11/increase-php-script-execution-time-limit/

UBUNTU the Best Linux Desktop Distribution

There are a lot of Linux distributions that have the primary focus of becoming the next best desktop replacement for Windows or OS X. Canonical’s UBUNTU is the great and most contentious group of distro which offers 4 kinds of desktop and special edition for laptops and Live Distros. you can download ubuntu from the following link Download.

Ubuntu edges out its closest contenders, Fedora and openSUSE, because its development team is constantly focused on the end-user experience. Canonical and the Ubuntu community have spent a lot of time and resources on bringing ease-of-use tools to this distribution, particularly in the area of installing Ubuntu and installing applications within Ubuntu.

In addition, Ubuntu’s level of support for its desktop products is highly superior, which is important in this class of distributions since it is the most likely to contain users new to Linux. Both the official and unofficial Ubuntu documentation is robust and searchable, a big plus.

Permanent link to this article: https://blog.openshell.in/2010/11/best-linux-desktop/

Linux and its Advantages

Linux is an operating system that was initially created as a hobby by a young student, Linus Torvalds, at the University of Helsinki in Finland. Linus had an interest in Minix, a small UNIX system, and decided to develop a system that exceeded the Minix standards. He began his work in 1991 when he released version 0.02 and worked steadily until 1994 when version 1.0 of the Linux Kernel was released. The kernel, at the heart of all Linux systems, is developed and released under the GNU General Public License and its source code is freely available to everyone. It is this kernel that forms the base around which a Linux operating system is developed. There are now literally hundreds of companies and organizations and an equal number of individuals that have released their own versions of operating systems based on the Linux kernel. More information on the kernel can be found at our sister site, LinuxHQ and at the official Linux Kernel Archives.

Apart from the fact that it’s freely distributed, Linux’s functionality, adaptability and robustness, has made it the main alternative for proprietary Unix and Microsoft operating systems. IBM, Hewlett-Packard and other giants of the computing world have embraced Linux and support its ongoing development. Well into its second decade of existence, Linux has been adopted worldwide primarily as a server platform. Its use as a home and office desktop operating system is also on the rise. The operating system can also be embedding into electronic devices such as routers, atm machines etc.

  1. Linux is not only a free operating system. but it also provides free software for everyone.
  2. Linux is virus free operating system.
  3. Linux is Hiperformance sclable and stable
  4. Compatable with atmost all drivers.
  5. Update all your software with a single click.
  6. No need to use software illegally coz linux comes with a huge package of softwares which is larger than windows.
  7. even if u need new software, Don’t searching the web, Linux installer gets it for you.
  8. Linux provides the next generation of desktops.
  9. you can customise your desktop.
  10. No back doors in your software.
  11. Enjoy free and unlimited support.
  12. customise your start menu.
  13. you can use MSN, GTalk, Yahoo, AIM, ICQ, Jabber, with a single program.
  14. Keep an eye on the weather using widgets.
  15. Linux works in minamal hardware support

Most people think linux is hard to learn. but it is easy then windows. why dont just give a try on the free software. MINT and Ubuntu would be a good distrubtion for start using computers.

Permanent link to this article: https://blog.openshell.in/2010/11/linux-and-its-advantages/

Refresh / Redirection

When you need your web page automatic refresh in 5 second or any second, use this meta tag.  It’s a simple code, put it between HEAD tag in your web page. This script easy but powerful.

<HEAD>
<meta http-equiv='refresh' content='2;url='file_name or URL'>
</HEAD>
// content = time (second)
// file_name = name of file you want to refresh or redirect

Many websites use this scripts to redirect to another page or refresh the same page.

For example:  My website updates in every 10 minutes and I want to show user my latest content then I put this script to my page and set it refreshs in every 10 minutes

Permanent link to this article: https://blog.openshell.in/2010/11/refresh-redirection/

cURL Function

cURL is a library which allows you to connect and communicate to many different types of servers with many different types of protocols. Using cURL you can:

  • Implement payment gateways’ payment notification scripts.
  • Download and upload files from remote servers.
  • Login to other websites and access members only sections.

PHP cURL library is definitely the odd man out. Unlike other PHP libraries where a whole plethora of functions is made available, PHP cURL wraps up a major parts of its functionality in just four functions.

A typical PHP cURL usage follows the following sequence of steps.

curl_init – Initializes the session and returns a cURL handle which can be passed to other cURL functions.

curl_opt – This is the main work horse of cURL library. This function is called multiple times and specifies what we want the cURL library to do.

curl_exec – Executes a cURL session.

curl_close – Closes the current cURL session.

Below are some examples which should make the working of cURL more clearer.

Download file or web page using PHP cURL

The below piece of PHP code uses cURL to download Google’s RSS feed.

<?php
/**
* Initialize the cURL session
*/
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL,
'http://news.google.com/news?hl=en&topic=t&output=rss');
/**
* Ask cURL to return the contents in a variable
* instead of simply echoing them to the browser.
*/
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/**
* Execute the cURL session
*/
$contents = curl_exec ($ch);
/**
* Close cURL session
*/
curl_close ($ch);
?>

As you can see, curl_setopt is the pivot around which the main cURL functionality revolves. cURL functioning is controlled by way of passing predefined options and values to this function.

The above code uses two such options.

  • CURLOPT_URL: Use it to specify the URL which you want to process. This could be the URL of the file you want to download or it could be the URL of the script to which you want to post some data.
  • CURLOPT_RETURNTRANSFER: Setting this option to 1 will cause the curl_exec function to return the contents instead of echoing them to the browser.

Download file or web page using PHP cURL and save it to file

The below PHP code is a slight variation of the above code. It not only downloads the contents of the specified URL but also saves it to a file.

<?php
/**
* Initialize the cURL session
*/
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL,
'http://news.google.com/news?hl=en&topic=t&output=rss');
/**
* Create a new file
*/
$fp = fopen('rss.xml', 'w');
/**
* Ask cURL to write the contents to a file
*/
curl_setopt($ch, CURLOPT_FILE, $fp);
/**
* Execute the cURL session
*/
curl_exec ($ch);
/**
* Close cURL session and file
*/
curl_close ($ch);
fclose($fp);
?>

Here we have used another of the cURL options, CURLOPT_FILE. Obtain a file handler by creating a new file or opening an existing one and then pass this file handler to the curl_set_opt function.

cURL will now write the contents to a file as it downloads a web page or file.

If you don’t have cURL functionality already installed, then please click on the following link to find out how install cURL,

https://blog.openshell.in/2011/01/enable-curl-for-php/

Permanent link to this article: https://blog.openshell.in/2010/11/curl-function/

PHP sessions problem

Below are some of the common problems faced by beginners while using sessions in PHP.

Headers already sent

This is the most commonly faced error is also the easiest to fix. Lets first see what a typical HTTP response looks like.

As can see from the image headers always precede (come before) the content. The “headers already sent” error occurs when you call session_start after you have already sent some content to the browser. Event an empty line at the start of the file is considered as content and will cause error.

set Session

How to fix

  1. Ensure that there is no empty line or HTML comments before the call to session_start function
  2. Add ob_start at the start of the file. A call to ob_start causes the output of the server to be buffered, that is stored in memory. While output buffering is active no output,except the headers, is sent from the script. The output is stored in the buffer and sent to the browser at the end of the script. You can also send the buffer’s contents to the browser by calling ob_flush or ob_end_flush
<?php
	//Start bufferng the output
	ob_start();
	print("Some text");
	//Since the output is now buffered,
	//the above statement will not cause any error
	session_start();
?>

Calling session_start multiple times

It is possible that session_start might get called multiple times by way of include files. This will not break you script but it might, depending on your error reporting settings, display a notice on your browser.

Multiple calls to session_start will result in an error of level E_NOTICE. Only the first call to session_start will be effective and all subsequent calls will be ignored.

Permanent link to this article: https://blog.openshell.in/2010/11/php-sessions-problem/

Set error reporting level to maximum

Configure your development environment to display all PHP warnings, notices and errors. This will help you code better and also help in catching potential bugs early in development phase.

You can change the error reporting level at runtime by adding the following code at the top of your PHP script:

<?php
// Report all PHP errors.
// Don't enable on production servers
error_reporting(E_ALL);
?>

You can also control whether you want to display errors in the browser itself or whether you want to log the errors.

Note: Displaying errors is not recommended for production servers.

Permanent link to this article: https://blog.openshell.in/2010/11/set-error/

SQL injections / Escape special characters like ‘,”,

In the simplest case, special characters may simply break your query. In a more extreme case, a hacker might use SQL injections to gain access to your application. So it is important that we escape these special characters with a (backslash). That is, insert a backslash before each special character.

We can escape special characters (prepend backslash) using mysql_real_escape_string or addslashes functions. In most cases PHP will this do automatically for you. But PHP will do so only if the magic_quotes_gpc setting is set to On in the php.ini file. We first check whether this setting is on or not. If the setting is off, we use mysql_real_escape_string function to escape special characters. If you are using PHP version less that 4.3.0, you can use the addslashes function instead.

A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level E_WARNING is generated.

If the magic quotes setting is on, we do not need escape special characters since PHP has already done it for us. We can check the magic_quotes_gpc by using get_magic_quotes_gpc function.

<?php
if(!get_magic_quotes_gpc()) {
	$login=mysql_real_escape_string($_POST['login']);
}else {
	$login=$_POST['login'];
}
?>

Permanent link to this article: https://blog.openshell.in/2010/11/sql-injection-php/

Who we are?

OpenshellOpenshell brings up a wide range of web solutions required by different kind of organizations and provides full-range of high class web site development for your complex business problems using PHP, Ruby on Rails, J2EE, .NET, MySQL, PostgreSQL, etc.

We proud to offer the best web hosting and domain web hosting services for all kind of business around the globe using the technologies such as Ecommerce, Dedicated, Shared and VPS solutions.

We also provide solutions for IT Infrastructure Management, Open Source Solutions and Network Administration & Troubleshooting.

Permanent link to this article: https://blog.openshell.in/2010/11/who-we-are/