You are on page 1of 15

1.

Write a php script to extract data from a form


Bio-data.php
<html> <body> <form action="Details.php"> <center><h1>BIO-DATA </h1></center> <b>First Name:</b><input type="text" value="dinesh"/></br> <b>Lastname:</b><input type="text" value="katuri"/></br> <b>Date of birth:</b> <select name="s1"> <option>22</option> <option>23</option> </select> <select name="s2"> <option>07</option> <option>08</option> </select> <select name="s3"> <option>1990</option> <option>1991</option> </select> </br></br> <b>gender:</b></br> <input type="radio" name="gender" value="male" c checked/>male</br> <input type="radio" name="gender" value="female" />female</br></br> <b>permanent address:</b></br> <textarea name="address" id="t" cols="20" rows="5"> </textarea></br></br> <b>Educational Qualifications :</b> </br> <table border="3" bordercolor="black" name="tble" > <b><tr name="f"> <td name="u">Course</td> <td><b>college</b></td> <td><b>year of passing</b></td> <td><b>aggregate</b></td> </tr> </b> <tr> <td>B.tech</td> <td>bec</td> <td>2011</td> <td>91.4742% (upto 3-2)</td> </tr>

name="firstname" name="lastname"

<tr> <td>IPE</td> <td>MC JR college</td> <td>2007</td> <td>97.6%</td> </tr> </table></br></br> <b>Soft Skills</b> <ul> <li><b>operating system</b> :</br> <input type="checkbox" name="c1" value="Windows XP"checked/>windows XP(sp2)</br> <input type="checkbox" name="c2" value="unix"/> unix </br> </li> <li><b>languages :</b></br> <input type="checkbox" value="C"checked />C <input type="checkbox" value="C++" checked/>c++ <input type="checkbox" value="core java" />java </li> <li><b>web technologies</b>:</br> <input type="checkbox" value="java scripts"checked/> java scripts </li> </ul>

name="choice3" name="choice4" name="choice5"

name="choice6"

<b>Hobbies:</b></br> <input type="checkbox" name="c7" cricket"checked/> playing cricket</br> <input type="checkbox" name="c8" cricket"/> playing chess</br> <input type="checkbox" name="c9" puzzles"checked/> solving puzzles</br></br> </br>

value="playing

value="playing

valu e="solving

<b>Declaration:</b> <p>I here by declare that the information is true to the best of my knowledge </p> <p>K.Dinesh</p>

above

stated

<p><h1>Click here to submit details</h1></p> <input type="submit" value="clickme"/> </form> </body> </html>

Details.php

<?php print_r('<b>FirstName</b> :'.$_GET['firstname']); print_r('<br>'); print_r('<b>LastName</b> :'.$_GET['lastname']); print_r('<br>'); print_r('<b>Date of birth</b> :'.$_GET['s1']); print_r('-'.$_GET['s2']); print_r('-'.$_GET['s3']); print_r('<br>'); print_r('<b>Gender</b> :'.$_GET['gender']); print_r('<br>'); print_r('<b>Address</b> :'.$_GET['address']); print_r('<b>Educational Qualifications</b> :'.$_POST[u]); print_r('<br>'); print_r('<b>SoftSkills:</br> 1.operating systems</b> :'.$_GET[c1]); print_r(','.$_GET[c2]); print_r('<b></br> 2.Languages</b> :'.$_GET[choice3]); print_r(','.$_GET[choice4]); print_r(','.$_GET[choice5]); print_r('<b></br> 3.Web Technologies</b> :'.$_GET[choice6]); print_r('<b></br>Hobbies </b> :'.$_GET[c7]); print_r(','.$_GET[c8]); print_r(','.$_GET[c9]); ?>

2.Write a php script to encrypt given text


encrypt2.php
<html> <head> </head> <body> <form action="encrypt1.php" method="GET"> <input type="text" name="h" value="<?php echo $_GET["h"];?>" /> <input type="submit" /> </form> </body> </html>

encrypt1.php
<?php $Str_message=$_GET["h"]; function encrypt_decrypt($Str_message) { $Len_Str_message=strlen($Str_message); $Str_Encrypted_message="";

for($position=0;$position<$Len_Str_message;$position++) { $key_to_use=(($Len_Str_message+$position)+1); $key_to_use=(255+$key_t o_use)%255; $byte_to_be_encrypted=substr($Str_message,$position,1); $Ascii_num_byte_to_encrypt=ord($byte_to_be_encrypted); $xored_byte=$Ascii_num_byte_to_encrypt^$key_to_use; $Encrypted_byte=chr($xored_byte); $Str_Encrypted_message.=$ Encrypted_byte; } return $Str_Encrypted_message; } $t=encrypt_decrypt($Str_message); $_GET["h"]=$t; include("encrypt2.php"); ?>

3.Write a php script for validating fields in a form


Validate.php
<html> <body> <?php $errname=" "; $erraddress=" "; $erremail=" "; $erruser=" "; $errzip=" "; $errpassport=" "; $errdate=" "; $errpass=" "; if($_POST["ac"]=="login") { if(preg_match("/^[A-Z][a-zA-Z]+$/",$_POST["name"])==0) { $errname='<p>Name must be from letters,dashes,spaces and must not start with dash</p>'; } if(preg_match("/^[a-zA-Z0-9.,:\"\']+$/",$_POST["address"])==0) { $erraddress='<p>address must be only letters ,numbers or one of the following</p>'; } if(preg_match("/^[a-zA-Z-]\w+(\.\w+)*\@\w+(\.[0-9a-zAZ+)*\.[a-zA-Z]{2,4}$/",$_POST["email"])==0) { $erremail='<p>email must comply with this mask :chars(.chars)@chars(.chars).chars(2 -4)</p>'; } if(preg_match("/^\d{10}$|^d{12}$/",$_POST["passport"])==0 ) { $errpassport='<p>pass digits</p>'; } port must be 10 or 12

if(preg_match("/^\d{1}-\d{3}-\d{3}\d{4}$/",$_POST["phone"])==0) { $errphone='<p>phone must comply with this mask: 1 -333444-4444</p>'; }

if(preg_match("/\d{4}$/",$_POST["zip"])==0) { $errzip='<p>zip must be 4 digits</p>'; } if(preg_match("/[0-9]{4}-[0-9]{1,2}-[09]{1,2}$/",$_POST["date"])==0) { $errdate='<p>date must comply with this mask yyyy -mmdd</p>'; } if(preg_match("/[0-9a-zA-Z]{5,}$/",$_POST["user"])==0) { $erruser='<p>user must be bigg er than 5 characters and contain only digits , letters, and underscore</p>'; } if(preg_match("/^.*(?={8,})(?=.*[0 -9])(?=.*[a-z])(?=.*[AZ])(?.*[A-Z]).*$/",$_POST["pass"])==0) { $errpass='<p>password must be atleast 8 characters and must contain atleast one lower case letter,one uppercase letter and one digit</p>'; } } ?> </body> <center> <form name="main" action="<?php $PHP_SELF?>" method="post"> <input type="hidden" name="ac" value="login"> <table width="500" border="0" cellpadding="4" cellspa cing="0" border-color="#000000" bgcolor="#EDEFF1"> <tr align="center"> <td colspan="2"> <strong>Field verification </strong> </td> </tr>

<tr> <td> full name:</td> <td> <input name="name" type="text" size="50" maxlength="100" value=" <?php echo $_POST['name']; ?>"/> <?php if(isset($errname)) echo $errname;

?> </td> </tr> <tr> <td> ADDRESS:</td> <td> <input name="address" type="text" size="50" maxlength="250" value="<?php echo $_POST['address']; ?>"></input> <?php if(isset($erraddress)) echo $erraddress; ?> </td> </tr> <tr> <td> Email:</td> <td> <input name="email" type="text" size="50" maxlength="100" value="<?php echo $_POST['email']; ?>"></input> <?php if(isset($erremail)) echo $erremail; ?> </td> </tr> <tr> <td> Zip:</td> <td> <input name="zip" type="text" size="50" maxlength="100" value="<?php echo $_POST['zip']; ?>"></input> <?php if(isset($errzip)) echo $errzip; ?> </td> </tr> <tr> <td>user:</td> <td> <input name="user" type="text" size="50" maxlength="100" value="<?php echo $_POST['user']; ?>"></input> <?php if(isset($erruser)) echo $erruser; ?> </td> </tr> <tr>

<td>phone:</td> <td> <input name="phone" type="text" size="50" maxlength="100" value="<?php echo $_POST['phone']; ?>"></input> <?php if(isset($errphone)) echo $errphone; ?> </td> </tr> <tr> <td> passport:</td> <td> <input name="passport" type="text" size= "50" maxlength="100" value="<?php echo $_POST['passport']; ?>"></input> <?php if(isset($errpassport)) echo $errpassport; ?> </td> </tr> <tr> <td> date:</td> <td> <input name="date" type="text" size="50" maxlength="100" value="<?php e cho $_POST['date']; ?>"></input> <?php if(isset($errdate)) echo $errdate; ?> </td> </tr> <tr> <td> pass:</td> <td> <input name="pass" type="password" size="50" maxlength="100" value="<?php echo $_POST['pass']; ?>"></input> <?php if(isset($errpass)) echo $errpass; ?> </td> </tr> </table> <input type="submit" method="post" value="submit"></input> </form>

</html>

4.Write a php script for login validation.If valid user,print some appropriate message
index.php
<html> <body> <form action="login1.php" method="GET"> <table border="4" align="center"> <tr> <td><b>login</b></td><td><input type="text"/></td> </tr></br>

name="login"

<tr><td><b>password</b></td> <td><input name="password" type="password"/> </td></tr> <tr> <td><input type="submit" name="submit" value="login" /></td> </tr> </table> </form> </body> </html>

login1.php
<?php $log=$_REQUEST["login"]; $pass=$_REQUEST["password"]; echo $log; echo $pass; $con = mysql_connect("localhost","root","welcome"); if (!$con) { die('Could not connect:'.mysql_error()); } $db=mysql_select_db("dinesh"); if(!$db) { die("can't select database"); }

else { $qry="select* from members where login='".$_GET['login']."'and password='".$_GET['password']."' "; $result=mysql_query($qry); echo $result; $num=mysql_num_rows($result); echo $num;

if($result) { if($num==1) { header("location:home.php"); exit(); } else { header("location:index.php"); exit(); } } else { die("query failed"); } }//end of 1st else; ?>

home.php
<?php echo "login successful"; ?>

5.Write a php script which takes as input table name and prints the table entries
table.php
<html> <body> <form action="login.php"> <b>Enter table name to know the values in it</b></br> <input type="text" name="t_name" /> <input type="submit" value="press to see table from "dinesh" database" /> </form> </body> </html>

login.php
<?php $table=$_REQUEST["t_name"];//getting input table name $con = mysql_connect("localhost","root","welcome"); if (!$con) { die('Could not connect:'.mysql_error()); } $db=mysql_select_db("dinesh"); if(!$db) { die("can't select database"); } $query="select * from {$table}"; $result=mysql_query($query); if(!$result) { die("query failed"); } $numrows=mysql_num_rows($result); $field_num=mysql_num_fields($result); while($row=mysql_fetch_row($result)) { foreach($row as $cell) { echo "$cell"; } echo "</br>"; } ?>

6.Write a php script to count the number of hits(to a website)


HitCounter.php
<?php class HitCounter { private $DBConnect; private $DBName = "cse440"; private $TableName = "hits"; private $Hits = 0; function __construct() { $this->DBConnect= @new mysqli("10.1.6.25", "cse440", "cse"); if(mysqli_connect_errno()) { die("<p>Unable to connect to th e database server.</p>". "<p>Error code " . mysqli_connect_errno(). ": " . mysqli_connect_error()) . "</p>"; } } function __destruct() { $this->DBConnect->close(); } public function setDatabase($Database) { $this->DBName = $Database; @$this->DBConnect->select_db($this->DBName Or die("<p>Unable to select the database.</p>" ."<p>Error code " . mysqli_errno($this->DBConnect). ": " . mysqli_error($this >DBConnect)) . "</p>"; } public function setTable($Table) { $this->TableName = $Table; } public function setHits() { $SQLstring = "UPDATE $this->TableName SET hits=$this>Hits WHERE id=1";

$QueryResult=@mysqli_query($this ->DBConnect,$SQLstring)Or die("<p>Unable to perform the query </p>"."<p>Error code " . mysqli_errno($this ->DBConnect).":". mysqli_error($this->DBConnect)) . "</p>"; } public function getHits() { $SQLstring = "SELECT * FROM id=1"; $QueryResult=$this->DBConnect>query($SQLstring) Or die("<p>Unable to perform the query.</p>". "<p>Error code " . mysqli_errno($this ->DBConnect) . ": " . mysqli_error($DBConnect)) . "</p>"; $Row = $QueryResult->fetch_row(); $this->Hits = $Row[1]; $this->Hits = $this->Hits + 1; echo "<p>This page has received " . $this ->Hits . " hits.</p>"; } } ?>

$this ->TableName

WHERE

CountVisits.php
<html> <body> <h3>Hit Counter</h3> <?php $Database = "cse440"; $Table = "hits"; if (class_exists("HitCounter")) { $Counter = new HitCounter(); $Counter->setDatabase($Database); } else { exit("<p>The HitCounter class available!</p>"); } $Counter->setTable($Table); $Counter->getHits(); $Counter->setHits(); ?> </body> </html>

is

not

You might also like