Friday, November 14, 2008

.htaccess tutorial

AuthUserFile /home/yourdomain/www/.htpasswd
AuthType Basic
AuthName "yourUsername"
Require valid-user

note: The password will be stored in .htpasswd file which is in root directory of the site.

Saturday, September 13, 2008

PHP XML foreach loop

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("checkbox array.xml");

$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
echo $item->nodeName . " = " . $item->nodeValue . "
";
}
?>

Load XML and Save XML - PHP

<?php
$xmlDom = new DOMDocument();
$xmlDom->load("enthiran.xml");

print $xmlDom->saveXML();
?>

Thursday, September 4, 2008

PHP array shift

<?php
$stack = array("ajax", "php", "javascript", "mysql");
$fruit = array_shift($stack);
print_r($stack); // first value of the array off
?>

PHP array difference

<?php    
$array1 = array("a" => "php", "string", "array", "PHP");   
$array2 = array("b" => "php", "PHP", "string");    
$result = array_diff($array1, $array2);   
print_r($result);
?>

Saturday, August 30, 2008

Export datas to mysql tables

Export CSV datas to mysql tables

LOAD DATA INFILE 'data.csv' INTO TABLE tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Export Excel sheet datas to mysql tables

LOAD DATA INFILE 'data.csv' INTO TABLE tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Export XML spread sheet datas to mysql tables

LOAD DATA INFILE 'data.csv' INTO TABLE tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Load Data Infile Syntax

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[CHARACTER SET charset_name]
[{FIELDS | COLUMNS}
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char']
]
[LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
]
[IGNORE number LINES]
[(col_name_or_user_var,...)]
[SET col_name = expr,...]

Wednesday, August 20, 2008

PHP checkbox array checked

<form action="checkbox_array_checked.php" method="post">
<input type="checkbox" name="check[]" value="sakkarakati" <? if (in_array('sakkarakati', $_POST['check'])) { ?> checked="checked" <? }?>/> Sakkarakati<br>
<input type="checkbox" name="check[]" value="kuselan" <? if (in_array('kuselan', $_POST['check'])) { ?> checked="checked"<? }?> /> Kuselan<br>
<input type="checkbox" name="check[]" value="villu" <? if (in_array('villu', $_POST['check'])) { ?> checked="checked"<? }?> /> Villu<br>
</form>

Saturday, August 9, 2008

PHP file download using header functions

header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);

PHP header file types

header('Content-type: application/pdf');
header('Location: http://ashokseenivas.blogspot.com/');
header("HTTP/1.0 404 Not Found");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sun, 10 Aug 2008 05:00:00 GMT");
header('Content-type: text/xml');

Monday, August 4, 2008

PHP Filesystem - fopen, fread, fputs or fwrite, filesize

<?
if (isset($_POST['Submit']))
{
$file = '\rajini-kuselan.php';
$filecontent = $_POST['filecontent'];
if ($fp = fopen ($file, 'w'))
{
fputs( $fp, stripslashes( $filecontent ) );
fclose( $fp );
}

}
$file = '\rajini-kuselan.php';
$fp = fopen( $file, 'r' );
$content = fread( $fp, filesize( $file ) );
$content = htmlspecialchars( $content );

?>
<form name="kuselan.php" method="post">
<textarea style="width:100%;height:500px" cols="110" rows="25" name="filecontent"><? echo $content;?></textarea>
<input type="submit" name="Submit" value="Submit" />
</form>

Saturday, July 26, 2008

PHP Filesystems - fgets, feof, fclose

<?
$phpfilesystem = fopen("php latest technologies.txt", "r");//read only

if ($phpfilesystem) {
while (!feof($phpfilesystem)) {
$buffer = fgets($phpfilesystem, 4096);
echo $buffer;
}
fclose($phpfilesystem);
}
?>

PHP Filesystem - fopen

<?
$phpfilesystem = fopen("kuselan movie.txt", "r"); //read only
$phpfiles = fopen("rajini robo.txt", "w"); //write only file pointer at beginning
$phpexamples = fopen("rajini robo.txt", "a"); //write only file pointer at end
?>

Thursday, July 17, 2008

PHP - WHILE LOOP

$counter = 1;
while ( $counter <= 10 ) {
echo "Hello world";
$counter = $counter + 1;
}

PHP - FOR LOOP

for ( $counter = 0; $counter <= 10; $counter += 1) {
echo "Hello world";
}

Tuesday, July 15, 2008

PHP Radio button array example

HTML Code:
1 <input type="radio" name="Seenivasagaperumal" value="1"/>
2 <input type="radio" name="Seenivasagaperumal" value="2"/>
3 <input type="radio" name="Seenivasagaperumal" value="3"/>
4 <input type="radio" name="Seenivasagaperumal" value="4"/>

PHP code:
if(isset($_POST['Submit'])){
$eric = $_POST['Seenivasagaperumal'];
}

Thursday, July 3, 2008

PHP array selected item delete

<?
$tagCollection = array("AJAX", "Analytics", "CSS", "Javascript", "JoshSchumacher.com", "PHP", "Prototype", "Web 2.0");
function strMatch($val)
{
$query = isset($_REQUEST['sel']) ? $_REQUEST['sel'] : '';
return (stripos($val, $query) !== false);
}
$tags = array_filter($tagCollection, "strMatch");
$key = key($tags);
unset($tagCollection[$key]);
print_r($tagCollection);
?>

Monday, June 30, 2008

MySql Connect

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

mysql_close($link);

Saturday, June 28, 2008

Bulk email sending using sleep function

$between_delay = 10;
$send_delay = 1;

for ( $sent=0; $row = mysql_fetch_array($result); $sent++ ) {

if ( ($sent % $between_delay) == 0 )
sleep( $send_delay );

$emailall = $row[0];
$message = stripslashes($message);
mail("$emailall", "$subject", "$message", "From: $email");
}

Friday, June 27, 2008

PHP Get and Post method Example

<form action="php manual.php" method="post">
Enter your name: <input name="name" type="text">
Enter your age: <input name="age" type="text">
<input type="submit">
</form>

<?php echo $_POST["name"]; ?>
<?php echo $_POST["age"]; ?>

<form action="php tutorial.php" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

<?php echo $_GET["name"]; ?>
<?php echo $_GET["age"]; ?>

Thursday, June 26, 2008

PHP String Functions Part II

$str = "My name is O\'Billa?";
echo stripslashes($str);// Outputs: My name is O'Billa?

$str = "My name is O'Billa?";
echo stripslashes($str);// Outputs: My name is O\'Billa?

$str = "Vijay's next film Villu";
$str = strtoupper($str);
echo $str; // Prints VIJAY'S NEXT FILM VILLU

$str = "AJITH'S NEXT FILM AEGAN";
$str = strtoupper($str);
echo $str; // Prints ajith's next film aegan

Wednesday, June 25, 2008

PHP String Funcitons

$txt="PHP MYSQL";
echo $txt; // Returns PHP MYSQL (Print the text)

echo strlen("My first coding in PHP"); // Returns 22 (Print the string length)

echo strpos("Hello world","l"); // Returns 2 (Print the string position)

$email = 'ashokseenivas50@gmail.com';
$domain = strstr($email, '@');
echo $domain; // prints @gmail.com

$user = strstr($email, '@', true);
echo $user; // prints ashokseenivas50

Wednesday, June 18, 2008

PHP Array Functions

Array Merge

$array1 = array("fruits" => "apple", 2, 4);
$array2 = array("a", "b", "fruits" => "orange", "shape" => "rectangle", 4);
$result = array_merge($array1, $array2);
print_r($result);

Array Combine

$a = array('a', 'b', 'c');
$b = array('d', 'e', 'f');
$c = array_combine($a, $b);
print_r($c);

Array Push

$stack = array("a", "b");
array_push($stack, "c", "d");
print_r($stack);

Array Pop

$stack = array("a", "b", "c", "d");
$fruit = array_pop($stack);
print_r($stack);

Thursday, June 12, 2008

.htaccess tutorial

1) Password Protected Directory

AuthUserFile /your/directory/here/.htpasswd
AuthGroupFile /dev/null
AuthName "Secure Document"
AuthType Basic
require user username

2) Password Protectection for a file


AuthName "Restricted File"
AuthType Basic
AuthUserFile /user/home/www/directory/.htpasswd
require valid-user

3) Protecting the file types


AuthName "Restricted"
AuthType Basic
AuthUserFile /user/home/www/directory/.htpasswd
require valid-user

4) Error Documents

ErrorDocument 400 /error/badrequest.htm
ErrorDocument 401 /error/authorisereqd.htm
ErrorDocument 403 /error/forbidden.htm
ErrorDocument 404 /error/filenotfound.htm
ErrorDocument 500 /error/servererror.htm

5) Redirecting Pages

Redirect /old-directory/oldpage.htm http://domain.com/new-directory/newpage.htm

Redirect /old-directory http://domain.com/new-directory/

6) Default page

DirectoryIndex default.htm

DirectoryIndex default.htm home.cgi sadass.pl otherfile.html

7) Add types and Add handler

AddType text/html .html
AddHandler server-parsed .html
AddHandler server-parsed .htm

8) Block Users by using IP


order deny,allow
deny from 123.456.789.000
deny from .domain.com
allow from all

Monday, June 9, 2008

PHP Secure mail example

$to = 'ashokseenivas50@gmail.com' ;
$subject = 'PHP Technical Questions';
$message = '
Upload Your Resumes: PHP FAQ,
PHP interview questions,
PHP Frequently Asked Questions,
Earn More money from google,
Cricket Updates,
Cinema gossips,
Dating.
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: Mary , Kelly ' . "\r\n";
$headers .= 'From: Blog Creation ' . "\r\n";
$headers .= 'Cc: seenivasagaperumal@yahoo.com' . "\r\n";
$headers .= 'Bcc: seenivasagaperumal@yahoo.com' . "\r\n";
mail($to, $subject, $message, $headers);

Monday, May 26, 2008

PHP checkbox array example

if (isset($_POST['submit'])) {

$fruit = $_REQUEST["fruit"];

echo("Fruits chosen: " . count($fruit));

for ($i=0; $i<count($fruit); $i++) {
echo( ($i+1) . ") " . $fruit[$i]);
}
}

Wednesday, May 21, 2008

PDF Generation Using PHP

PDF Generation Using PHP

$pdf=new FPDF();
$pdf->AddFont('Calligrapher','','calligra.php');
$pdf->AddPage();
$pdf->SetFont('Calligrapher','',35);
$pdf->Cell(0,10,'Enjoy new fonts with FPDF!');
$pdf->Output();

More Examples:
PDF Generation using line breaks
PDF Generation using header footer

Friday, January 11, 2008

FAQ - PHP

1) How can we extract string ‘rajinirobo.com ‘ from a string ‘http://info@rajinirobo.com’ using regular expression of PHP?

preg_match(”/^http:\/\/.+@(.+)$/”,’http://info@rajinirobo.com’,$found);
echo $found[1];

2) In how many ways we can retrieve the data in the result set of MySQL using PHP?

1. mysql_fetch_row.
2. mysql_fetch_array
3. mysql_fetch_object
4. mysql_fetch_assoc

3) What are the different types of table in mysql?

5 types of tables we can create
1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM

4) Difference between MSSQL server & MYSQL?

MS SQL Server 2005

SQL Server is a full-fledged database system developed specifically for large enterprise databases. All advanced features of a relational database are fully implemented. Once you purchase the product, you are only limited to the Sybase-derived engine.

MySQL 5.x

The latest release of MySQL, the 5.X offering, has rounded up on features that lagged commercial equivalents such as SQL Server. Triggers, stored procedures, Views, Information Schema, Serverside Cursors, and Precision Math are features that are implemented but not yet stabilised.

5) What is the difference between echo and print statement?

Print is a function whereas echo is a language construct.

You can use something link $a = print $b while you cant use $a = echo $b.

Print return true or false value on printing. Echo doesn't

Print cannot have multiple expression whereas Echo can have Multiple Expression statement to echo the value.

6) What is the difference between echo and print statement?

echo() can take multiple expressions,Print cannot take multiple expressions.

echo has the slight performance advantage because it doesn't have a return value.

True, echo is a little bit faster.

Print is a function whereas echo is a language construct.

You can use something link $a = print $b while you cant use $a = echo $b.

7) What do you need to do to improve the performance for the script you have written?

Use EXPLAIN to monitor the amount of records retrieved for each query. You can use UNIQUE, LIMIT, WHERE to filter the no of records returned

8) How to reverse given string?

with function:

strrev()

without function:

for($i=0;sizeof($str_name)>$i;$i++)
{

for($l=1;$i-$l;$l--)
{
echo $str_name;
}

9. How many persons have hit my site?

session_start();
(!isset($_SESSION['count'])){ $_SESSION['count']=0;}
else{ $_SESSION['count']++;}
echo "session".$_SESSION['count'];

10. Will PHP supports inheritance?

Yes, PHP supports single inheritance

11. How do you create subdomains using PHP?

* Create the appropriate web root directory, for example /home/sites/username/web , and any subdirectories you wish
* Edit httpd.conf - add a new virtual host section
* Restart httpd

12. How to get the URL domain name in PHP?

$domainName=$_SERVER['HTTP_HOST'];
echo $domainName;

13. What is the difference between Split and Explode?

split() can work using regular expressions while explode() cannot.