PHP

The Popular CMS of PHP:

Joomla
Joomla.in is a dedicated Joomla company prviding joomla design, development, themes, custom development, SEO, joomla security and portal systems.
Drupal
Drupal is an open source content management platform powering millions of websites and applications
Wordpress
WordPress is web software you can use to create a beautiful website or blog. We like to say that WordPress is both free and priceless at the same time.

The Popular Frameworks of PHP:

CodeIgniter
Open source PHP web application framework
CakePHP
The rapid development php framework
Zend Framework
Zend is the leading provider of software and services for developing, deploying and managing business-critical applications in PHP

Login Form Using PHP
 index.html

<html>
<head>
<title>Login Form</title>
</head>
<body>
<form method="post" action="checklog.php">
<fieldset style="width:350px;color:#CC0000; text-align:center; border:2px #333399 double;">
 <legend style="font-size:18px; text-decoration:underline;">Login Form</legend>
 <table style="color:#0099FF">
 <tr>
  <td>Email</td>
  <td><input type="text" name="email"/></td>
 </tr>
 <tr>
 <td>&nbsp;&nbsp;&nbsp;&nbsp;Password</td>
 <td><input type="password" name="pass"/></td>
 </tr>
 <tr>
 <td colspan="2"><input type="submit" name="sub" value="Login"></td>
 </tr>
 </table>
</fieldset>
</form>
</body>
</html>

Checklog.php
<?php
$host='localhost';
$user='root';
$pwd='';
$db='siva';
$con=mysql_connect($host,$user,$pwd);
$sdb=mysql_select_db($db,$con);
$email=$_POST['email'];
$pass=$_POST['pass'];
$sql="SELECT * FROM users WHERE Email='$email' and Pass='$pass'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
echo "Login Successfully";
}
else {
echo "Wrong Username or Password";
}
?>



Registration Form Using PHP


<?php
$host='localhost';
$user='root';
$pwd='';
$db='register';
$con=mysql_connect($host,$user,$pwd);
$sdb=mysql_select_db($db,$con);
if(isset($_POST['reg']))
{
if(!$_POST['name'] || !$_POST['email'] || !$_POST['pass'] || !$_POST['addr'] || !$_POST['ph'] ){
die('You didn\'t fill in a required field.');
}
$ins="INSERT INTO `register`.`users`(`Name`,`Email`,`Pass`,`address`,`Phno`) VALUES ('$_POST[name]','$_POST[email]','$_POST[pass]',  '$_POST[addr]','$_POST[ph]')";
mysql_query($ins) or die(mysql_error());
}
?>

<html>
<head>
<title>registration Form</title>
</head>
<body style="background-image:"../images/gflowers64.gif no-repeat">
<h1 align="center" style="color:#FF0000;"><u>Registration Form</u></h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table background="../images/ivory-cream-colored-leather-texture-600x400.jpg" style="border:1px #3399CC dotted;" align="center">
<tr>
 <th colspan="2" align="center" style="text-decoration:underline">Registration Form</th>
</tr>
<tr>
   <td>Name<font color="#FF0000">*</font></td>
   <td><input type="text" name="name"/></td>
</tr>
<tr>
   <td>Email<font color="#FF0000">*</font></td>
   <td><input type="text" name="email"/></td>
</tr>
<tr>
   <td>Password<font color="#FF0000">*</font></td>
   <td><input type="password" name="pass"/><font color="#993300">mininum 6 characters</font></td>
</tr>
<tr>
   <td>Address</td>
   <td><textarea rows="5" cols="30" name="addr"/></textarea>
</tr>
<tr>
   <td>Phoneno<font color="#FF0000">*</font></td>
   <td><input type="text" name="ph"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<img src="verificationimage.php?<?php echo rand(0,9999);?>" alt="verification image, type it in the box"  name="im" width="50" height="24" align="absbottom" /></td></tr>
<tr>
<td><strong>Enter Access Code<font color="#FF0000">*</font></strong></td>
<td><input name="acc" type="text"/></td>
</tr>
<tr>
   <td colspan="2" align="center"><input type="submit" name="reg" value="Register" /></td>
</tr>
</table>
</form>
</body>
</html>

verificationimage.php

<?php

header('Content-type: image/jpeg');
$width = 50;
$height = 24;
$my_image = imagecreatetruecolor($width, $height);
imagefill($my_image, 0, 0,0xff99ff);
for ($c = 0; $c < 40; $c++){
$x = rand(0,$width-1);
$y = rand(0,$height-1);
imagesetpixel($my_image, $x, $y, 0x000000);
}
$x = rand(1,10);
$y = rand(1,10);
$rand_string = rand(1000,9999);
imagestring($my_image, 5, $x, $y, $rand_string, 0x000000);
setcookie('tntcon',(md5($rand_string).'a4xn'));
imagejpeg($my_image);
imagedestroy($my_image);
?>



Simple Example of Emp-Dept using PHP& Mysql

index.html


<html>
<head>
<title>Home Page</title>
</head>
<body background="siteBackground.jpg">
<h1  align="center" style="background-color:#999999; color:#FF0033;">Siva Career Services</h1>
<h4 align="center" style="color:#FF0033;">Software Development   |  Training   |  Consultancy</h4>
<hr/>
<table cellpadding="10" cellspacing="10">
<tr>
<td><h2 style="color:#993300; text-decoration:underline">Employee Details:</h2></td>
<td><a href="insemp.php" style="text-decoration:none; color:#FF6666;" target="_blank"><h2 style="color:#FF6633">Insert<h2></a></td>
<td><a href="disemp.php" style="text-decoration:none; color:#FF6666;" target="_blank"><h2 style="color:#FF6633">Display<h2></a></td>
</tr>
<tr>
<td><h2 style="color:#993300; text-decoration:underline">Department Details:</h2></td>
<td><a href="insdept.php" target="_blank" style="text-decoration:none; color:#FF6666;"><h2 style="color:#FF6633">Insert<h2></a></td>
<td><a href="disdep.php" target="_blank" style="text-decoration:none; color:#FF6666;"><h2 style="color:#FF6633">Display<h2></a></td>
</tr>
<tr>
 <td colspan="2"><a href="disboth.php"  target="_blank" style="color:3ff6666; text-decoration:none; ">Dispaly Employees with Department name which belongs to same department</a></td>
</tr>
</table>
</body>
</html>

dbconnect.php
<?php

$con=mysql_connect('localhost','root','');
$sel= mysql_select_db('job',$con);
?>





insemp.php


<?php
include('dbconnect.php');
if(isset($_POST['sub']))
{
/*$eno=$_POST['eno'];
$ename=$_POST['ename'];
$dep=$_POST['dep'];
$sal=$_POST['sal'];
$age=$_POST['age'];*/
$ins="INSERT INTO `job`.`emp` (`empno`, `empname`, `dept`, `salary`, `age`) VALUES ('$_POST[eno]', '$_POST[ename]', '$_POST[dep]', '$_POST[sal]','$_POST[age]')";
mysql_query($ins) or die(mysql_error());
}
?>
<html>
<head>
<title>Insert Employee</title>
</head>
<body>
<form method="post" action="">
<table  align="center" bgcolor="#00FFCC" cellpadding="10" cellspacing="10">
 <tr>
   <th colspan="2" style="background:#660066; text-decoration:underline">Emmployee Details</th>
 </tr>
 <tr>
    <td>Emp No</td>
    <td><input type="text" name="eno" id="eno"/></td>
 </tr>
 <tr>
    <td >Emp Name</td>
    <td><input type="text" name="ename" id="ename"/></td>
 </tr>
 <tr>
 
    <td>Department</td>
    <td><select name="dep">
    <?php
$sel='SELECT dname FROM dept';
$dis=mysql_query($sel) or die(mysql_error);
while($row=mysql_fetch_array($dis)){
  $dd=$row[dname];
  echo "<option>$dd</option>";}?>
 </select>
  </td>
  </tr>
  <tr>
    <td>Salary</td>
    <td><input type="text" name="sal" id="sal"/></td>
  </tr>
 <tr>
    <td>Age</td>
    <td><input type="text" name="age" id="age"/></td>
  </tr>
 <tr>
  <td colspan="2" align="center"><input type="submit" name="sub"/></td>
 </tr>
 </table>
</form>
</body>
</html>



insdept.php


<?php
include('dbconnect.php');
if(isset($_POST['sub']))
{
/*$dno=$_POST[dno];
$dname=$_POST[dname];
$loc=$_POST[loc];
$det=$_POST[tt];*/
$ins=" insert into `job`.`dept` (Dno, Dname, Location, Details) VALUES ('$_POST[dno]','$_POST[dname]','$_POST[loc]','$_POST[tt]')";
mysql_query($ins) or die(mysql_error());
}
?>
<html >
<head>
<title>Insert Department</title>
</head>
<body>
<form method="post" action="">
<table  align="center" bgcolor="#00FFCC" cellpadding="10" cellspacing="10">
 <tr>
   <th colspan="2" style="background:#FF6600; text-decoration:underline">Department Details</th>
 </tr>
 <tr>
    <td>Dept No</td>
<td><input type="text" name="dno" id="dno"/></td>
 </tr>
 <tr>
    <td >Dept Name</td>
    <td><input type="text" name="dname" id="dname"/></td>
 </tr>
 <tr>
    <td>Location</td>
    <td><input type="text" name="loc" id="loc"/></td>
 </tr>
 <tr>
    <td>Details</td>
    <td><textarea rows="2' cols="50" name="tt"></textarea></td>
 </tr>
 <tr>
  <td colspan="2" align="center"><input type="submit" name="sub"/></td>
 </tr>
 </table>
</form>
</body>
</html>




disemp.php


<?php
include('dbconnect.php');
$sel="SELECT * FROM  `emp`  ";
$dis=mysql_query($sel) or die(mysql_error());
?>
<html>
<head>
<title>Employee Records</title>
</head>

<body>
<table bgcolor="#00FFFF" border="1">
<tr>
 <th>Empno</th>
 <th>Empname</th>
 <th>Dept</th>
 <th>Salary</th>
 <th>Age</th>
</tr>
<?php
while($row=mysql_fetch_row($dis))
{
?>
<tr>
<td><?php echo $row[0]."<br/>"?></td>
<td><?php echo $row[1]."<br/>"?></td>
<td><?php echo $row[2]."<br/>"?></td>
<td><?php echo $row[3]."<br/>"?></td>
<td><?php echo $row[4]."<br/>"?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>




disdept.php

<?php
include('dbconnect.php');
$sel="SELECT * FROM  `dept`  ";
$dis=mysql_query($sel) or die(mysql_error());
?>

<html >
<head>
<title>Department Records</title>
</head>
<body>
<table bgcolor="#00FFFF" border="1">
<tr>
 <th>Deptno</th>
 <th>Deptname</th>
 <th>Location</th>
 <th>Details</th>
 </tr> 
<?php
while($row=mysql_fetch_row($dis))
{
?>
<tr>
<td><?php echo $row[0]."<br/>"?></td>
<td><?php echo $row[1]."<br/>"?></td>
<td><?php echo $row[2]."<br/>"?></td>
<td><?php echo $row[3]."<br/>"?></td>

</tr>
<?php
}
?>
</body>
</html>


disboth.php
<?php
include('dbconnect.php');
$sel="SELECT emp.empname, dept.dname, dept.location, emp.salary FROM emp, dept where emp.deptno=dept.dno";
$dis=mysql_query($sel) or die(mysql_error());
?>

<html>
<head>

<title>Display</title>
</head>

<body>
<table bgcolor="#00FFFF" border="1">
<tr>
 <th>EmpName</th>
 <th>Deptname</th>
 <th>Location</th>
 <th>Salary</th>
 </tr> 
<?php
while($row=mysql_fetch_array($dis))
{
?>
<tr>
<td><?php echo $row['empname']."<br/>"?></td>
<td><?php echo $row['dname']."<br/>"?></td>
<td><?php echo $row['location']."<br/>"?></td>
<td><?php echo $row['salary']."<br/>"?></td>

</tr>
<?php
}
?>
</body>
</html>



Sample Chessboard using PHP & HTML


<HTML>
<HEAD>
<TITLE>CHESS BOARD</TITLE>
</HEAD>
<BODY>
<?php
function createBoard ($lines, $cols)
{
$j=1;
echo "<TABLE ALIGN=CENTER BORDER=1 CELLSPACING=0>";
while ($j<=$lines) {
echo "<TR>";
$k=1;
while ($k<=$cols) {
if (($j+$k)%2>0)
echo "<TD WIDTH=30 HEIGHT=30 BGCOLOR=#000000> </TD>";
else
echo "<TD WIDTH=30 HEIGHT=30 BGCOLOR=#FFFFFF> </TD>";
$k++;
}
echo"</TR>";
$j++;
}
echo "</TABLE><BR/><BR/>";
}
createBoard(8,8);
createBoard(4,4);
?>
</BODY>
</HTML>




Comparison Between Zend and CakePHP Framework

    Zend Framework  offers a simple component library to provide 80 percent of the functionality mostly needed by developers. The rest of the 20 percent can be altered as per your requirements of the business. This can be done since it has a flexible architecture and you do not need any configuration file to get started with.

       CakePHP is a rapid development framework for PHP that offers a flexible architecture for development, maintenance and deployment of applications. It uses the usual design patterns like MVC and ORM within the convention over configuration paradigm that helps lower development expenses and does not need the developers to write a lot of code.


Feature Comparison of CakePHP and the Zend Framework
Feature
CakePHP
Zend Framework
License
Compatability
4 and 5

5.1.4 or later
Documentation

Community

Wiki,

MVC
Strict
Optional
Configuration
PHP file
PHP Array, XML, or
INI files

Database Abstraction
PHP, PEAR, ADODB
   Data Handling

good

Excellent
  Caching
good

Excellent
Sessions
Excellent

Excellent
JavaScript/Ajax
good

fair
Web Services
good

Excellent
Localization
good

Excellent
Unit Testing
Yes
Yes


CMS Comparison: Drupal, Joomla and Wordpress

              A computer software system for organizing and facilitating collaborative creation of documents and other content, especially for loading to a website

COMPLETE CONTENT MANAGEMENT SYSTEMS: Drupal ,Joomla!...

BLOGS: Word press, Movable Type , Blogger...

WIKIS : Media Wiki , Confluence , pmwiki...

FORUMS: PhpBB,vBulletin,OpenBBm,Zorum,MercuryBorad...

FRAMEWORKS: yii,codeignitor,zend,cakephp,Symfony...



Drupal
Joomla
Wordpress
HomePage
About
    Drupal is a powerful, developer-friendly tool for building complex sites. Like most Powerful tools, it requires some expertise and experience to operate.
Joomla offers middle ground between the developer-oriented, extensive capabilities of Drupal and user-friendly but more complex site development options than Wordpress offers
Wordpress began as an innovative, easy-to-use blogging platform. With an ever-increasing repertoire of themes, plugins and widgets, this CMS is widely used for other website formats also.
Example Sites
Community Portal: Fast Company, Team Sugar
News Publishing:The New York Observer

Social Networking: MTV Networks Quizilla
Education: Harvard University
Restaurant: IHOP

Social Networking: PlayStation Blog
News Publishing: CNN Political Ticker
Education/Research: NASA Ames Research Center

Installation
Features
Known for its powerful taxonomy and ability to tag, categorize and organize complex content.
Designed to perform as a community platform, with strong social networking features.
Ease of use is a key benefit for experts and novices alike. It’s powerful enough for web developers or designers to efficiently build sites for clients; then, with minimal instruction, clients can take over the site management. Known for an extensive selection of themes. Very user-friendly with great support and tutorials, making it great for non-technical users to quickly deploy fairly simple sites.
Caching –plugins
Pressflow: This is a downloadable version of Drupal that comes bundled with popular enhancements in key areas, including performance and scalability.
JotCache offers page caching in the Joomla 1.5 search framework, resulting in fast page downloads. Also provides control over what content is cached and what is not. In addition, page caching is supported by the System Cache Plugin that comes with Joomla.
WP-SuperCache:The Super Cache plugin optimizes performance by generating static html files from database-driven content for faster load times.
Best Use Cases
For complex, advanced and versatile sites; for sites that require complex data organization; for community platform sites with multiple users; for online stores
Joomla allows you to build a site with more content and structure flexibility than Wordpress offers, but still with fairly easy, intuitive usage. Supports E-commerce, social networking and more.
Ideal for fairly simple web sites, such as everyday blogging and news sites; and anyone looking for an easy-to-manage site. Add-ons make it easy to expand the functionality of the site.

Evaluator: Be familiar with web terminology and concepts.

End-user: Be familiar with browsing, clicking, submitting web pages, selecting options.

Administrator: Manage roles, select themes, categorize web pages (content), configure module settings, install and upgrade software and databases, apply security fixes.

Site designer/developer: Install software, design style and layout (with css and minimal php), build and deploy websites, evaluate contributed modules, work with LAMP.

Programmer: Program in php, administer databases, program through a well-defined API,design database objects, evaluate existing solutions, apply patches, collaborate with other developers.

Examples Of Drupal: (www.Drupalsites.net)
  •        Community Portal Websites ex:New York Observer
  •         Intranet/Corporate Websites ex:Suddenthoughts
  •        International Sites ex:puntBarra
  •        Education ex:engl 42os
  •         Art,Music,Multimedia ex:project opus


Drop Down Box Example

1. <select  name="year">

    <option>Year</option>
    <?php
     for ($i = 1925; $i <=2025; $i++) {
        echo "<option value=\"$i\">$i</option>";
     }
    ?>



2.<select name="dep">
<?php
$sel='SELECT dname FROM dept';
$dis=mysql_query($sel) or die(mysql_error); 
while($row=mysql_fetch_array($dis)){
  $dd=$row[dname]; 
  echo "<option>$dd</option>";}?>
 </select>

3.<?php 
        // 1. countries (country_id, country_name) 
        // 2. cities (city_id, city_name, country_id) 
    mysql_connect("localhost", "username", "password"); 
    mysql_select_db("database name"); 
   ?> 
  <form action="" method="post"> 
  <select name="country"> 
  <?php 
        $country_query = "SELECT country_id, country_name FROM countries"; 
        $country_result = mysql_query($country_query); 
        while($country = mysql_fetch_array($country_result)) { 
            if($_POST['country'] == $country['country_id'] ) { 
                echo '<option selected value="' . $country['country_id'] . '">' . $country['country_name'] . '</option>'; 
            } else { 
                echo '<option value="' . $country['country_id'] . '">' . $country['country_name'] . '</option>'; 
            } 
        } 
    ?>     
</select> 
<input type="submit" name="submit" value="GO" /><br /><br /> 
<select name="city"> 
<?php 
    if($_POST['submit']) { 
        echo $_POST['country']; 
        $city_query = "SELECT city_id, city_name FROM cities WHERE country_id = '{$_POST[country]}'"; 
         
        $city_result = mysql_query($city_query); 
         
        while($city = mysql_fetch_array($city_result)) { 
            echo '<option value="' . $city['city_id'] . '">' . $city['city_name'] . '</option>'; 
        } 
    } 
?> 
</select> 
</form> 

Simple Email Script in PHP

<?php 
if ($_POST['email'] <> '') { 
    $ToEmail = 'youremail@site.com'; 
    $EmailSubject = 'Site contact form'; 
    $mailheader = "From: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
    $MESSAGE_BODY = "Name: ".$_POST["name"].""; 
    $MESSAGE_BODY .= "Email: ".$_POST["email"].""; 
    $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"]).""; 
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?> 
Your message was sent
<?php 
} else { 
?> 
<form action="test.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form> 
<?php 
}; 
?>

Inserting check box values in PHP

<?php
if(isset($_POST['team']))
{
foreach($_POST['team'] as $value){
$insert=mysql_query("INSERT INTO team('team') VALUES ('$value')");
}
}
?>

<html>
<body>
<form method="post" action="chk123.php">
<input type="checkbox" name="team[]" value="AG"> Australia 
<input type="checkbox" name="team[]" value="GE">India 
<input type="checkbox" name="team[]" value="BR"> Japan 
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>


Display Images using dropdown


<html>
<body>


<select onchange='document.smallimage.src= this.value;document.header.background.src=this.value'>
<option selected='selected' value='blank.jpg'>Choose</option>
<option value='1.gif'>one</option>
<option value='2.jpg'>two</option>
<option value='3.jpg'>three</option>
<option value='4.jpg'>four</option>
<option value='5.jpg'>five</option>
</select><br>
<img name='smallimage' id='smallimage' width='320' height='240' src='4.jpg'>

</body>
</html>



Useful Tips in PHP


Ø Befriend the PHP Manual (PHP manual.)

Ø Turn on Error Reporting (Error reporting in PHP )

Ø Try an IDE (plenty of great IDE)

Ø Try a PHP Framework (CakePHP or CodeIgniter )

Ø Learn the DRY Approach (Don’t Repeat Yourself)

Ø Indent Code and Use White Space for Readability

Ø “Tier” your Code (how to tier your PHP applications)

Ø Always Use <? php?>

Ø Use Meaningful, Consistent Naming Conventions

Ø Comment, Comment, Comment

Ø Install MAMP/WAMP ( MAMP (Mac) or WAMP (Windows))

Ø Give your Scripts Limits (set_time_limit)

Ø Use Objects (or OOP)( object-oriented programming with PHP.)

Ø Know the Difference between Single and Double Quotes

Ø Don’t Put phpinfo () in your Webroot (Phpinfo )

Ø Never, Ever Trust Your Users ( properly secured form)

Ø Store Passwords with Encryption ( MD5)

Ø Use Database Visualization Design Tools ( DBDesigner and MySQL Workbench)

Ø Use Output Buffering (ob_start())

Ø Protect your Script From SQL Injection (mysql_real_escape_string)

Ø Try ORM( Propel)

Ø Cache Database-Driven Pages

Ø Use a Caching System (Memcached, Zend Cache, eAccelerator, XCache)

Ø Validate Cookie Data (htmlspecialchars() or mysql_real_escape_string().)

Ø Use Static File Caching Systems(. Smarty )

Ø Profile your Code( xdebug)

Ø Code to a Standard( Zend and Pear)

Ø Keep Functions Outside of Loops

Ø Don’t Copy Extra Variables

Ø Upgrade to the Latest Version of PHP

Ø Reduce the Number of Database Queries( Stace (Unix) and Process Explorer (Windows) )

Ø Don’t be Afraid to Ask for Help (getting PHP help)


Creative Uses for PHP


Ø  E-Commerce(Magento, Zen Cart, Shopify)


Ø  Project Management Tools(Basecamp)


Ø  Graphical User Interface(PHP GTK, ZZEE PHP GUI )


Ø  Building an Online Community(php BB, vBulletin, Pun BB)


Ø  Developing Facebook Applications(Facebook developer’s wiki )


Ø  Generating PDF Files


Ø  Parsing XML Files


Ø  Mailing Lists(PHP list)


Ø  Image Processing and Generation


Ø  Create Graphs and Charts(Image_Graph)


Ø  Content Management Systems(Drupal, WordPress, Joomla)


Ø  Create a PHP Photo Gallery( exif)


Ø  Create Dynamic Website Templates


Ø  Create WordPress Plugins


Ø  Creating Flash (Ming library)






No comments:

Post a Comment