Monkey gets the banana :)
May 30th, 2008Webmonkey used to be a great resource for web developers long back. It’s back now as a wiki ![]()
The original web developer’s resource has returned. Webmonkey has been completely redesigned, and we’re ready to rock once more. Also, our entire content library is now hosted on a wiki, so every tutorial, reference page and code example is open for editing. Come on in and show us what you’ve got!
Go check it out.
Elsewhere:
Continue script execution on client disconnection
June 6th, 2006Jayant, I have written a PHP code to do some processing on data which should not be stopped by the user in between as it leaves the system in an invalid state. The problem comes when the user disconnects by either pressing the browser’s stop button or closes the browser window. Is there a way in PHP to define atomic functions or atomic scripts? I want the script or the function to execute completely or not at all. Thanks!
- Dale
To continue the script execution on client disconnection, you can use a function provided by PHP called ‘ignore_user_abort‘. This function sets whether on client disconnect, should script execution be continued or aborted. Calling the function with the parameter set to ‘TRUE’ should ignore user disconnection and continue with script execution.
<?php
ignore_user_abort(TRUE);
?>
When finished you may again set the value to ‘FALSE’ to allow the script to halt on client disconnect.
<?php
ignore_user_abort(FALSE);
?>
Modify maximum execution time of a PHP script
June 6th, 2006I have written a PHP code to do some processing on data, but before the processing can finish, the script throws an error ‘Fatal error: Maximum execution time of 30 seconds exceeded in /home/dales/public_html/processbulkdata.php’.
- Dale
To change the maximum execution time of a PHP script you need to use the ‘set_time_limit‘ function. The function takes one argument which is the maximum number of seconds a script is allowed to run. If seconds is set to ‘0′, no time limit is imposed on the script and it may run indefinitely. Use the following snippet just before your time consuming code begins:
<?php
set_time_limit(0);
?>
Note that, if you set the number of seconds to a value very high, you may recieve the fatal error just after 1 second. Its better to use ‘set_time_limit(0)‘ instead. Also, remember that when you are finished with the portion that performs the busy computation, its a good idea to call:
<?php
set_time_limit(30);
?>
so that the remaining portion of the script finishes in 30 seconds or fatal error is thrown.
PHP: Language Specific Response
June 6th, 2006Ever wanted to give response to the users in the language they prefer? Most browsers pass the language preference using the HTTP header ‘Accept-Language’ which looks like ‘Accept-Language: en-us,en;q=0.5‘. We can use this to detect the users first preferred language and show the response in that language.
$languages_supported_by_browser=$_SERVER["HTTP_ACCEPT_LANGUAGE"];
$pos=strpos($languages_supported_by_browser,";");
if ($pos>0) {
$languages_supported_by_browser =
substr($languages_supported_by_browser,0,$pos);
}
$languages=split(",",$languages_supported_by_browser);
echo "First language supported by browser is: <strong>".
$languages[0]."</strong>.";
Using the variable ‘$languages[0]‘ we can redirect the page to the language specific URL, include a perticular language file, etc to achieve the language specific response. We can also have a list of languages we support in an array and keep looping through the array ‘$languages‘ and comparing the two, we can easily find the languages that we support and is preferred by the browser.
Check the syntax of multiple PHP files all at once
June 4th, 2006To check the syntax of all files ending with ‘.php’ file extension and execute the syntax check of php for every found file, we can use:
find ./ -type f -name \*.php -exec php -l {} \;
It is quite useful when you want to quicly see if any php file in a project/bunch of files, contains a parse error. Note: This command is for Linux/ Unix based systems. This will not work on a Windows system.
I just need a MySQL Query that can select the next 10 records after the first top 10 records.
- Anuj Madan
ln MySQL, you will do it this way:
SELECT *
FROM tablename
LIMIT x,y;
LIMIT x,y means skip the first x records, and then return the next y records.
So your query will look like:
SELECT *
FROM tablename
LIMIT 10,10;
How can I change the appearance of the ‘Submit’ button?
June 2nd, 2006Jayant, I have seen many sites using fancy form ‘Submit’ buttons
(font, color of button, etc.). How do I change the default form submit
buttons to those fancy ones?
- Anish Khanna
There are bascially two popular ways in which you can tweak the appearance of a submit button in an HTML form:
The most popular and preferred way these days is to use CSS attributes.
<style type="text/css">
.funkybutton { background-color: #ccffcc; font-size: 160%;
font-weight: bold; }
</style>
<form action="http://www.example.com/test.php" method="get">
<input type="submit" value="Submit This!!!" class="funkybutton" />
</form>
This will appear as:
In the method 2, all you need to do is:
<form action="http://www.example.com/test.php" method="get">
<input type="image" name="submit" src="submit.gif" border="0" />
</form>
This will appear as something like:
(depending upon the image used)
Some other methods include creating buttons using Flash, Java Applets.