PHP
libphp5.so on Apache 2 is faster than CGI or FastCGI
Why this site loads so fast ? The reason is we are using Apache 2.2.10 with libphp5.so
Earlier we were using CGI.
I also tried PHP-FastCGI
- Nilesh's blog
- 1 comment
- Read more
- 111 reads
Generating Serial Page Numbers in PHP [Pager]
Hello friends,
In this tutorial we explain how to generate page numbers serially and store them in a file called pages.html
You can further use this tutorial to implement this technique in your CMS [if you are building one :D]
In this tutorial we count the no. of articles in the database and divide it by 5 which implies that we display 5 articles per page. (we take intval() of the result).
This code will generate page nos as
Page 1
Page 2
....so on
Enjoy!
The code:
$db = mysqli_connect("localhost","user","password","database");
if ( mysqli_errno($db) ) {
die(mysqli_error($db));
}
$count = mysqli_query($db,"SELECT COUNT(*) FROM content");
if ( mysqli_errno($db) ) {
die(mysqli_error($db));
}
$r = mysqli_fetch_row($count);
$pg_no = intval($r[0]/5);
$htmlfile = fopen("pages.html", "w") or die("Error occurred while opening file");
for ( $i = 0; $i != $pg_no; $i++ ) {
$str = "<a href=home.html?p=".($i+1).">Page ".($i+1)."<br>";
fwrite($htmlfile, $str);
}
fclose($htmlfile);
print "\n";
?>
- Nilesh's blog
- Add new comment
- 249 reads
Adding your META tags on to a drupal page
Most blogging systems OR CMSes don't have the option to add the META KEYWORDS and META DESCRIPTION tags which are the key to search engine success to some extent.
Yes, I know you can add them in theme files... but if you change theme ? You have to re-add them. So we need a persistent solution which doesn't change while changing themes.
Below is a simple procedure where you can add your META tags to a drupal (taking 5.x here) system (without editing any of the theme files!).
Open common.inc in your favourite editor. common.inc is found in <drupal-install-path>/includes
Scroll down to Line Number 116 (or search for drupal_get_html_head).
After the first function line, you will see a line like this -
$output = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
You then append these lines RIGHT AFTER the above line (else its not going to work).
$output .= '<meta name="keywords" content="keywrd1,keywrd2,..." />'."\n"; $output .= '<meta name="description" content="A small description here" />'."\n";
Save the file. Make a backup of the older file and place this new common.inc overriding the old one.
The above method has been tested and was found to work. So, if you have any problems just comment down here!
Have a nice day!
- Nilesh's blog
- Add new comment
- 262 reads
PHP: Receiving Function Arguments without declaring any parameters in prototype!
As you know, we usually declare functions in PHP like this -
function foo($arg1, $arg2) {
// code
}
This leads to PHP-error when one of the argument is missing. To deal with this, we have an alternate method. We shall NOT declare ANY arguments in the prototype. Then we'll use func_get_args() to get all the arguments passed to function in an array. Further we can manipulate the array as we want.
function foo() {
$args = func_get_args();
}
When you will call foo(1,2,3) then $args[0] = 1 ; $args[1] = 2 and so on.
This way you can prevent PHP-Errors and have custom errors.
To handler PHP-Errors I suppose there does exist an function to set a custom handler for PHP-Errors. This is just an alternate method ;)
- Nilesh's blog
- Add new comment
- 91 reads
Creating a MySQL quoter
Well, many times we have to quote field names and table names in SELECT, INSERT, and other MySQL queries via PHP using backticks ` . This is a big trouble inserting backticks in each SQL query especially when you are using the traditional MySQL extension instead of MySQLi which has a prepare() method. Well, I have created a simple PHP function (rather a snippet :D) in which you just have to specify the field names to be quoted (it can also be table names). So, here's my snippet -
(read at the end what it does)
function quote($f = NULL,$quote="`") {
if($f == NULL):
return false;
endif;
$f = explode(',',$f);
foreach($f as $k => $v) {
$sql_fields .= $quote.$v.$quote.",";
}
$sql_fields = strrev($sql_fields);
$sql_fields_exp = explode(',',$sql_fields);
end(array_reverse($sql_fields_exp));
array_shift(&$sql_fields_exp);
$sql_fields = array_reverse($sql_fields_exp);
$while_init=0;
$while_count=count($sql_fields);
while($while_init != $while_count) {
$sql_fields[$while_init] = strrev(&$sql_fields[$while_init]);
$while_init++;
}
$sql_fields = implode(',',$sql_fields);
return $sql_fields;
}
Description of what it does -
- By default if no input is given to the function, it will set $f to NULL and $quote to a backtick.
- Next, it checks if $f is NULL or not.
- Now, the input $f is split into an array by removing the commas between the input string.
- Each field into the split array $f, is appended as a string to $sql_fields with a comma on the end which results in an unwanted comma at the end of the string.
- The string $sql_fields is reversed.
- And again the fields are exploded by removing a comma preserving the backtick on each field name.
- Now we reverse the array as obtained in the previous step and set the array's internal pointer to the end.
- Now, the last element is empty eariler was a comma is deleted from the array.
- We create a new array which contains the reversed array obtained in the previous step.
- As you know, explode() splits a string into numerical array (or an array with numeric indices).
- So, we have a while loop which brings back the order of fields (it matters a lot in inserts).
- Now, imploding the new string with a comma gets us the final result!
I have tested this code with single value and multiple value -- it works!
If you have any suggestions, questions, please comment else spread the link to everyone!
- Nilesh's blog
- 1 comment
- 397 reads
Recent comments
2 days 47 min ago
2 days 56 min ago
4 days 11 hours ago
4 days 11 hours ago
4 days 13 hours ago
2 weeks 2 days ago
3 weeks 4 days ago
4 weeks 5 days ago
5 weeks 3 days ago
5 weeks 4 days ago