rss twitter facebook
May 20th, 2012 By joanruanes Categories: PHP and MYSQL, PHP Functions

PHP Script Timeout

If you’ve been having trouble with your script, and you cannot seem to figure out why your scripts just stop randomly at random times especially while reading large file… Try the following:
PHP: set_time_limit

void set_time_limit ( int $seconds )

Where it limits the maximum execution time.

When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.

Note: This function has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini. See here.

If you’re using PHP_CLI SAPI and getting error “Maximum execution time of N seconds exceeded” where N is an integer value, try to call set_time_limit(0) every M seconds or every iteration.

For example:

<?php

require_once('db.php');

$stmt = $db->query($sql);

while ($row = $stmt->fetchRow()) {
    set_time_limit(0);
    // your code here
}

?>

Keep in mind though that for CLI SAPI max_execution_time is hardcoded to 0. So it seems to be changed by ini_set or set_time_limit but it isn’t, actually.

The only references I’ve found to this strange decision are deep in bugtracker and in php.ini (comments for ‘max_execution_time’ directive).

Or if you got something like: msg: set_time_limit() [function.set-time-limit]: Cannot set time limit in safe mode.

Try this:

<?php
        if( !ini_get('safe_mode') ){
            set_time_limit(25);
        }
?>

Unfortunately though, a script which gets into an infinite loop can produce an alarming amount of output in only a few seconds. Try attempting to debug a script, and add this to the beginning of the script:

<?php
set_time_limit(2);
?>

But still, even two seconds of run time produced enough output to overload the memory available to the browser.

To work it perfectly, add this to the beginning of the script:

<?php
set_time_limit(2);

ob_start();     // buffer output

function shutdown () {
    // print only first 2000 characters of output
    $out = ob_get_clean();
    print substr($out, 0, 2000);
}

register_shutdown_function('shutdown');
?>

A short routine which would limit the execution time, and also limit the amount of output returned.

Comments Off
May 20th, 2012 By joanruanes Categories: Tutorials

Download Script in PHP

There comes a time that you need to offer some of your website’s content for downloading (i.e. most sites commonly offer downloads of PDF and MP3 files). You would want to to set up your download system so that it could perform certain tasks or give you certain information like how often certain files have been downloaded, etc. In this post we will share with you some PHP scripts that accomplishes this and more.
PHP Script that can be used to manage the download process:
To prevent user to
Comments Off
May 19th, 2012 By joanruanes Categories: Code Snippets, Tutorials
Twitter PHP Script. List of some commonly used and very useful code snippets to interact with Twitter in your web development projects.
Get the number of follower in full text:
It is always cool to display how many followers you have. To do so, simply use the short code snippet below.

1

Autofollow script:
This code allow you to automatically follow user who has tweet about a specific term.

1
<?php
//

Comments Off
May 19th, 2012 By joanruanes Categories: Code Snippets, Tutorials
In this post is a list of most commonly used Facebook PHP Scripts. From Facebook PHP script that will communicate to the Facebook API and retrieve very basic user information to show Facebook PHP Script error messages.
Facebook PHP script to save user information to MySQL database and automatically publish to Wall:
This script can automatically collect user information (i.e. e-mail, name, birthday or picture etc.) and save to MySQL database. It also automatically publish posts to users Wall.

1
<?php

session_start();

if (!empty($_SESSION)) {
header("Location: home.php");
}
mysql_connect(‘localhost’

Comments Off
May 18th, 2012 By joanruanes Categories: Articles
PHP or PHP: Hypertext Preprocessor is a general-purpose server-side scripting language originally designed for Web development to produce dynamic Web pages. It is one of the first developed server-side scripting languages to be embedded into an HTML source document, rather than calling an external file to process data. (wiki)

Even though PHP is much younger than other languages, it was established as the leading website programming language.

What do PHP code look like?

It is a rather simple language and most of its syntax is borrowed from C except for dealing with

Comments Off
May 18th, 2012 By joanruanes Categories: PHP Functions
PHP array to string. Sometimes data arrive in array which has to be convert into string, in this situation we can use implode() function which is used to convert the array into string.
One-Dimensional Arrays to String
Use the implode() function:
1

Or the var_export function:
1

Multi-Dimensional Arrays to String

This code:
1

Results in this:

8 Array to string conversion
8 Array to string

Comments Off
May 17th, 2012 By joanruanes Categories: Tutorials
TortoiseSVN Turn of Merge. Here is a trick for TortoiseSVN:

How to turn off “auto-merge” in Subversion is to set SVN external diff tool to a program that will constantly fail. If external diff program fails, SVN concludes that conflict is unresolvable and wouldn’t merge it.

svn –diff-cmd=/bin/false

A good choice for the new mime-type would be:

application/octet-stream

Which just means a general binary type file.

To set the Subversion tag of a file using TortoiseSVN:

1. Right-click on the file and select properties,
2

Comments Off
May 17th, 2012 By joanruanes Categories: Code Snippets

PHP Uppercase Every First Character in Array

A simple code to make every first character uppercase in a string, even they are separated by hyphen.
ucwords
1
Returns a string with the first character of each word in str capitalized, if that character is alphabetic.
Examples:
ucwords()
1
<?php
$foo = ‘the quick brown fox’;
$foo = ucwords($foo); // The Quick Brown Fox

$bar = ‘THE QUICK BROWN FOX’;
$bar = ucwords($bar); // THE QUICK BROWN FOX
$bar =

Comments Off
May 16th, 2012 By joanruanes Categories: PHP and MYSQL

PHP Strip Table Name from MySQL Query

There comes a time that you need to rename a table in MySQL. You either run an SQL query to do this or do it with phpMyAdmin if you don’t want to bother remembering the SQL to do so.
1

This statement renames one or more tables.

Example:
The example SQL below renames the MySQL table tablename to tablename_renamed:
1
Comments Off
May 16th, 2012 By joanruanes Categories: Code Snippets

PHP Backend Joomla Form Validation

Form validation working on the input form in the front-end, but not working in the backend. Either doesn’t work or the form submits when it is not meant to.
You might have to change the toolbar buttons…

Put something like this above your code:
1

Or.. instead of usual script, replace it with the script:
1
<script
Comments Off