Tuesday, July 31, 2012

PHP Scripts



PHP Scripts



Single file upload php
multiple file upload php
PHP Image of the day
php image thumbnails
different types of input php
Login System php
Get Visitors IP Address PHP
Users online php
simple counter php
Comments php
banner rotation.php
Calculate Age from Birth Day php
Simple Calculator PHP
Calculator 2 php
Number Converter php
Date,Birth Day & calender php
Image Filters php
Resizing Images php
standard deviation php
Simple Image Gallery php
E-Mail Extractor php
Replace php
Unzip File using PHP
Create zip file php
Create icon php
URL Rotation php
Create image php
watermark image php
Encryption and Decryption php
Numbers to Roman Number Convertion php
Edit File php
Rename file php
Form Validation
Copy And Paste File content
Retain drop down selected php
Storing user information

Monday, July 30, 2012

Web layout design



Web layout design

Designing of web lay out is done by a few methods.Basically we have to place the areas for headers, left side bar,right sidebar,mid content and footers.We could have ability of separating areas using HTML Frames,Tables and divisions.Here,I will be discussed with you most popular method,division using HTML and CSS.
We will move step by step to each division.

outer-wrapper:-The layer that contains all the content of web including headers and Footers.


 
The HTML code file contains division called outerwrapper into which i have link css file called wrap.css where styles are included to the division outerwrapper.

<html>
<head>
<title></title>
<link href="wrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="outerwrapper"><h2>Outer Wrapper</h2>
</div>

</body>
</html>
@charset "utf-8";
* {
   margin: 0;
   padding: 0; }
body {
   color: #555555;
   font-family: 'Trebuchet MS', Arial, Verdana, Helvetica, sans-serif;
   font-size: 75%;
   }
#outerwrapper {
   width: 960px;
   background-color: #62a6e6;
   margin: 0 auto;
   line-height: 1.4em;
   color: #404040;
margin-top:50px;
border: 1px solid #31e;
height:725px;
}

innerwrapper:-The layer that could be contained all the content or could not be contained all of web.This example it could not contain right side bar.


 
<html>
<head>
<title></title>
<link href="wrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="outerwrapper">
<div id="innerwrapper">
</div>

</div>
</body>
</html>
@charset "utf-8";
* {
   margin: 0;
   padding: 0; }
body {
   color: #555555;
   font-family: 'Trebuchet MS', Arial, Verdana, Helvetica, sans-serif;
   font-size: 75%;
   }
#outerwrapper {
   width: 960px;
   background-color: #62a6e6;
   margin: 0 auto;
   line-height: 1.4em;
   color: #404040;
margin-top:50px;
border: 1px solid #31e;
height:725px;
}
#innerwrapper {
   float: left;
   margin-left:5px;
   width:760px;
  background-color: #f8eb58;
height:550px;
}

content:-This layer also covers the area of innerwrapper area in this example(you can expand according to your requirements).

<html>
<head>
<title></title>
<link href="wrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="outerwrapper">
<div id="innerwrapper">
<div id="content">
</div>

</div>
</div>
</body>
</html>
@charset "utf-8";
* {
   margin: 0;
   padding: 0; }
body {
   color: #555555;
   font-family: 'Trebuchet MS', Arial, Verdana, Helvetica, sans-serif;
   font-size: 75%;
   }
#outerwrapper {
   width: 960px;
   background-color: #62a6e6;
   margin: 0 auto;
   line-height: 1.4em;
   color: #404040;
margin-top:50px;
border: 1px solid #31e;
height:725px;
}
#innerwrapper {
   float: left;
   margin-left:5px;
   width:760px;
  background-color: #f8eb58;
height:550px;
}
#content {
   float: left;
   width: 760px;
  height:550px;
background-color: #d8e8f6;
border: 1px solid #31e;
}

 Head:-Place where headers of web contains.



<html>
<head>
<title></title>
<link href="wrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="outerwrapper">
<div id="head">
</div>

<div id="innerwrapper">
<div id="content">
</div>
</div>
</div>
</body>
</html>
@charset "utf-8";
* {
   margin: 0;
   padding: 0; }
body {
   color: #555555;
   font-family: 'Trebuchet MS', Arial, Verdana, Helvetica, sans-serif;
   font-size: 75%;
   }
#outerwrapper {
   width: 960px;
   background-color: #62a6e6;
   margin: 0 auto;
   line-height: 1.4em;
   color: #404040;
margin-top:50px;
border: 1px solid #31e;
height:725px;
}
#innerwrapper {
   float: left;
   margin-left:5px;
   width:760px;
  background-color: #f8eb58;
height:550px;
}
#content {
   float: left;
   width: 760px;
  height:550px;
background-color: #d8e8f6;
border: 1px solid #31e;
}
#head {
   height: 145px;
   width: 960px;
   background-color: #76cb7e;
 }

leftsidebar:-Place where left menu is placed



<html>
<head>
<title></title>
<link href="wrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="outerwrapper">
<div id="head">
</div>
<div id="innerwrapper">
<div id="content">
 <div id="leftsidebar">
</div>

</div>
</div>
</div>
</body>
</html>
@charset "utf-8";
* {
   margin: 0;
   padding: 0; }
body {
   color: #555555;
   font-family: 'Trebuchet MS', Arial, Verdana, Helvetica, sans-serif;
   font-size: 75%;
   }
#outerwrapper {
   width: 960px;
   background-color: #62a6e6;
   margin: 0 auto;
   line-height: 1.4em;
   color: #404040;
margin-top:50px;
border: 1px solid #31e;
height:725px;
}
#innerwrapper {
   float: left;
   margin-left:5px;
   width:760px;
  background-color: #f8eb58;
height:550px;
}
#content {
   float: left;
   width: 760px;
  height:550px;
background-color: #d8e8f6;
border: 1px solid #31e;
}
#head {
   height: 145px;
   width: 960px;
   background-color: #76cb7e;
 }
#leftsidebar {
   margin-right: 10px;
   width: 180px;
   height:550px;
   float: left;
background-color: #ed9b7b;
}
 contentmid:-where page content is included


<html>
<head>
<title></title>
<link href="wrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="outerwrapper">
<div id="head">
</div>
<div id="innerwrapper">
<div id="content">
 <div id="leftsidebar">
</div>
<div id="contentmid">
</div>

</div>
</div>
</div>
</body>
</html>
@charset "utf-8";
* {
   margin: 0;
   padding: 0; }
body {
   color: #555555;
   font-family: 'Trebuchet MS', Arial, Verdana, Helvetica, sans-serif;
   font-size: 75%;
   }
#outerwrapper {
   width: 960px;
   background-color: #62a6e6;
   margin: 0 auto;
   line-height: 1.4em;
   color: #404040;
margin-top:50px;
border: 1px solid #31e;
height:725px;
}
#innerwrapper {
   float: left;
   margin-left:5px;
   width:760px;
  background-color: #f8eb58;
height:550px;
}
#content {
   float: left;
   width: 760px;
  height:550px;
background-color: #d8e8f6;
border: 1px solid #31e;
}
#head {
   height: 145px;
   width: 960px;
   background-color: #76cb7e;
 }
#leftsidebar {
   margin-right: 10px;
   width: 180px;
   height:550px;
   float: left;
background-color: #ed9b7b;
}
#contentmid {
   float: right;
   margin-right: 10px;
   width: 550px;
  height:550px;
background-color: #f2e680;
border: 1px solid #31e;
}

 rightsidebar:-content of right side is included


<html>
<head>
<title></title>
<link href="wrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="outerwrapper">
<div id="head">
</div>
<div id="innerwrapper">
<div id="content">
 <div id="leftsidebar">
</div>
<div id="contentmid">
</div>
 </div>
</div>
<div id="rightsidebar">
</div>

</div>
</body>
</html>
@charset "utf-8";
* {
   margin: 0;
   padding: 0; }
body {
   color: #555555;
   font-family: 'Trebuchet MS', Arial, Verdana, Helvetica, sans-serif;
   font-size: 75%;
   }
#outerwrapper {
   width: 960px;
   background-color: #62a6e6;
   margin: 0 auto;
   line-height: 1.4em;
   color: #404040;
margin-top:50px;
border: 1px solid #31e;
height:725px;
}
#innerwrapper {
   float: left;
   margin-left:5px;
   width:760px;
  background-color: #f8eb58;
height:550px;
}
#content {
   float: left;
   width: 760px;
  height:550px;
background-color: #d8e8f6;
border: 1px solid #31e;
}
#head {
   height: 145px;
   width: 960px;
   background-color: #76cb7e;
 }
#leftsidebar {
   margin-right: 10px;
   width: 180px;
   height:550px;
   float: left;
background-color: #ed9b7b;
}
#contentmid {
   float: right;
   margin-right: 10px;
   width: 550px;
  height:550px;
background-color: #f2e680;
border: 1px solid #31e;
}
#rightsidebar {
   float: right;
   margin-right: 5px;
   width: 180px;
 height:550px;
background-color: #ed9b7b;
}

footer:-Where footer content is placed


<html>
<head>
<title></title>
<link href="wrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="outerwrapper">
<div id="head">
</div>
<div id="innerwrapper">
<div id="content">
 <div id="leftsidebar">
</div>
<div id="contentmid">
</div>
 </div>
</div>
<div id="rightsidebar">
</div>
<div id="footer">
</div>

</div>
</body>
</html>
@charset "utf-8";
* {
   margin: 0;
   padding: 0; }
body {
   color: #555555;
   font-family: 'Trebuchet MS', Arial, Verdana, Helvetica, sans-serif;
   font-size: 75%;
   }
#outerwrapper {
   width: 960px;
   background-color: #62a6e6;
   margin: 0 auto;
   line-height: 1.4em;
   color: #404040;
margin-top:50px;
border: 1px solid #31e;
height:725px;
}
#innerwrapper {
   float: left;
   margin-left:5px;
   width:760px;
  background-color: #f8eb58;
height:550px;
}
#content {
   float: left;
   width: 760px;
  height:550px;
background-color: #d8e8f6;
border: 1px solid #31e;
}
#head {
   height: 145px;
   width: 960px;
   background-color: #76cb7e;
 }
#leftsidebar {
   margin-right: 10px;
   width: 180px;
   height:550px;
   float: left;
background-color: #ed9b7b;
}
#contentmid {
   float: right;
   margin-right: 10px;
   width: 550px;
  height:550px;
background-color: #f2e680;
border: 1px solid #31e;
}
#rightsidebar {
   float: right;
   margin-right: 5px;
   width: 180px;
 height:550px;
background-color: #ed9b7b;
}
#footer {
   clear: both;
   width: 960px;
   color: #777;
   height: 25px;
   font-size: 0.8em;
   font-weight: bold;
   margin: 0px auto;
background-color: #130703;
}

navigation:-Where main menu items are placed


<html>
<head>
<title></title>
<link href="wrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="outerwrapper">
<div id="head">
</div>
 <div id="navigation">
</div>

<div id="innerwrapper">
<div id="content">
 <div id="leftsidebar">
</div>
<div id="contentmid">
</div>
 </div>
</div>
<div id="rightsidebar">
</div>
<div id="footer">
</div>
</div>
</body>
</html>
@charset "utf-8";
* {
   margin: 0;
   padding: 0; }
body {
   color: #555555;
   font-family: 'Trebuchet MS', Arial, Verdana, Helvetica, sans-serif;
   font-size: 75%;
   }
#outerwrapper {
   width: 960px;
   background-color: #62a6e6;
   margin: 0 auto;
   line-height: 1.4em;
   color: #404040;
margin-top:50px;
border: 1px solid #31e;
height:780px;
}
#innerwrapper {
   float: left;
   margin-left:5px;
   width:760px;
  background-color: #f8eb58;
height:550px;
}
#content {
   float: left;
   width: 760px;
  height:550px;
background-color: #d8e8f6;
border: 1px solid #31e;
}
#head {
   height: 145px;
   width: 960px;
   background-color: #76cb7e;
 }
#leftsidebar {
   margin-right: 10px;
   width: 180px;
   height:550px;
   float: left;
background-color: #ed9b7b;
}
#contentmid {
   float: right;
   margin-right: 10px;
   width: 550px;
  height:550px;
background-color: #f2e680;
border: 1px solid #31e;
}
#rightsidebar {
   float: right;
   margin-right: 5px;
   width: 180px;
 height:550px;
background-color: #ed9b7b;
}
#footer {
   clear: both;
   width: 960px;
   color: #777;
   height: 25px;
   font-size: 0.8em;
   font-weight: bold;
   margin: 0px auto;
background-color: #130703;
}
#navigation {
   width: 960px;
   background-color: #f1caec;
   height: 31px;
   border-top: #023634 solid 1px;
   margin-bottom: 20px;}

Sunday, July 29, 2012

Java Sripts



Java Scripts 


Auto Fill same type of inputs js
Word And Character counter JS
Add Additional Input to Drop down js
Slide Images js
Tool Tip Js
Image Rotation JS
Add Additional Input to Drop down js
Dropdown - On change redirect to another page

Word And Character counter JS



Word And Character counter JS

When you  have a text area field where you need to provide information about the word count when the user enters some text in the field. Length of the field is supposed to be 50 Characters. Initially it would show 50 characters left.

 Only 50 characters allowed!
characters left 

<SCRIPT LANGUAGE="JavaScript">

 function CountLeft(field, count, max) {

 if (field.value.length > max)
 field.value = field.value.substring(0, max);
 else

 count.value = max - field.value.length;
 }
 </script>

  <center>
 <form name=sample action="">
 Only 50 characters
 allowed!</br>

 <textarea rows="15" name="text" cols="45"
  onKeyDown="CountLeft(this.form.text,this.form.left,50);"
 onKeyUp="CountLeft(this.form.text,this.form.left,50);"></textarea>
 <input readonly type="text" name="left" size=3 maxlength=3
 value="50">
characters left</font>
 </form>
 </center>

Counting the number of words in a  text area is quite simple with JavaScript. The idea is to count the number of spaces and use this to calculate the number of words. The following example illustrates the script.

Word Count:
<script>
function cnt(w,x){
var y=w.value;
var r = 0;
a=y.replace(/\s/g,' ');
a=a.split(' ');
for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
x.value=r;
}
</script>
<form name="myform">
<textarea rows="15" name="w" cols="45"
onkeyup="cnt(this,document.myform.c)"></textarea>
<br />Word Count: <input type="text" name="c" value="0" size="5"
onkeyup="cnt(document.myform.w,this)" />
</form>

Retain drop down selected php



Retain drop down selected php

When you post a drop down input the resulting page drop down automatically moves to it selected value.PHP switch statement could be applied to solve this problem

Codes are displayed bellow

<?php
$D=$_POST['D'];

switch ($D)
{
case "Monday":
$Mon="selected";
break;
case "Tuesday":
$Tue="selected";
break;
case "Wednesday":
$Wed="selected";
break;
case "Thursday":
$Thu="selected";
break;
case "Friday":
$Fri="selected";
break;
case "Saturday":
$Sat="selected";
break;
case "Sunday":
$Sun="selected";
break;
}

echo "
<form method=post action=selected.php>
<select name=D>
<option value=Monday $Mon>Monday</option>
<option value=Tuesday $Tue>Tuesday</option>
<option value=Wednesday $Wed>Wednesday</option>
<option value=Thursday $Thu>Thursday</option>
<option value=Friday $Fri>Friday </option>
<option value=Saturday $Sat>Saturday</option>
<option value=Sunday $Sun>Sunday</option>
</select>
<input type=submit value=Submit>
";

?>

Saturday, July 28, 2012

Auto Fill same type of inputs js




infoplus

I have changed wintekweb php as wintekweb infoplus to avoid limitations in my blog. Now, I have expanded my blog with java scripts lesions as initial step later I could have ability of blogging many different subjects.  

Java Scripts

JavaScript is also a scripting language of the Web. It adds functionality, validate forms, communicate with the server etc. As the PHP tutorials which, I have posted, these posts are also going with examples. My effort is to meet the interest of users and to solve FAQs I found in java scripts.

Auto Fill same type of input js 


Here, I have created First Name and User Name, Email and Verify Email as same fields

First Name
User Name
Email
Verify Email

Bellow,I have included the scripts with java and html save the file as .html then you need not to have local server to run those file like in php.

<html>
<head>
<script type="text/javascript" charset="utf-8">  
function updateUser(){      
first = document.getElementById("first").value;      
document.getElementById("user").value = first;  
}
function updateEmail(){
email= document.getElementById("email").value;      
document.getElementById("vemail").value = email;
}      
</script>
</head>
<body>
<table bgcolor=#5d76b5>
<tr bgcolor=#e9eff7><td>First Name</td><td><input type="text" name="some_name" value="" id="first" onkeyup="updateUser();"></td></tr>
<tr bgcolor=#e9eff7><td>User Name</td><td><input type="text" name="some_name" value="" id="user" onkeyup="updateUser();"></td></tr>
<tr bgcolor=#e9eff7><td>Email</td><td><input type="text" name="some_name" value="" id="email" onkeyup="updateEmail();"></td></tr>
<tr bgcolor=#e9eff7><td>Varify Email</td><td><input type="text" name="some_name" value="" id="vemail" onkeyup="updateEmail();"></td></tr>
</table>
</body>
</html>

Tuesday, July 24, 2012

Copy And Paste File content



Copy And Paste File Content


You can copy content of one file and paste it in same type of empty file

Copy and Paste a File

Select File
To be copied
Select empty File
To be Pasted

PHP Syntax is bellow

<html>
<head><title>Copy and paste File</title>

</head>
<body >
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<table bgcolor=#d9e5a2>
<tr>
<td colspan="4" align ="center"><h3>Copy and Paste a File</h3></td>
</tr>
<tr>
<td><strong> Select File</br> To be copied</strong></td>
<td><input type="file" id="sourcefile" name="sourcefile"></td>
<td><strong> Select empty File</br> To be Pasted</strong></td>
<td><input type="file" id="destinationfile" name="destinationfile"></td>
</tr>
<tr>
<td> </td> <td> </td><td><input type="submit" name="copy" value="Copy/Paste"/></td>
</tr>
</table>
</form>
<?php
if($_POST["copy"]){
$sourcefile = $_POST["sourcefile"];
$destinationfile = $_POST["destinationfile"];
copy($sourcefile,$destinationfile);
}
?>
</body>
</html>

Monday, July 23, 2012

auto log out php




auto log out php

After log in,You may have seen auto log out after few mints of time.First you have to log in.So I have created simple log in form and database table for check in users details correct or not.Here I have set only 5 second time for auto log out to check your script as earliest. 

CREATE TABLE IF NOT EXISTS `members` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `username` varchar(65) NOT NULL DEFAULT '',
  `password` varchar(65) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Dumping data for table `members`
--

INSERT INTO `members` (`id`, `username`, `password`) VALUES
(1, 'upul', 'upul123');


<table bgcolor=#d2dbf1 align="center">

<form name="form1" method="post" action="checklogin.php">

<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

checklogin.php

<?php
session_start();
$host="localhost";
$username="root";
$password="";
$db_name="my_database";
$tbl_name="members";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

login_success.php

<?php
header('Refresh: 5; URL=login.php');
?>
<?php
session_start();
   echo " welcome".$myusername;
    $inactive = 500;
    // check to see if $_SESSION['timeout'] is set
    if(isset($_SESSION['timeout']) ) {
    $session_life = time() - $_SESSION['start'];
    if($session_life > $inactive)
    { session_destroy(); header("Location: page6.php"); }
    }
    $_SESSION['timeout'] = time();

?>

Saturday, July 21, 2012

Form Validation



Form Validation

When you create a form for submission,It is very important to validate forms before submission to avoid filling your data base with incorrect data.
Form Validation In PHP


Name:
Email

create following php you will get error messages with empty fields and if name field contain less than 6 characters.

<?php
$errors = array();
$name='';
$email='';
if(isset($_POST['name']) && isset($_POST['email']))   {


    $name = $_POST['name'];
    $email = $_POST['email'];

    if (strlen($name) < 6) {
        $errors['name'] = 'Minimum 6 Characters Required';
    }

    if(empty($name) || empty($email))   {
        $errors['required'] = 'All fields required';
    }
}

if(empty($errors) && $_POST)    {
    echo '<font color=#91100d><b>Your form has been submitted</b></font>';          

}   elseif ($errors || !$_POST) {
?>
        <form name="form" action="" method="post">
       <font color=#91100d><b><?PHP if (isset($errors['required']))   { echo $errors['required'];} ?></b></font>
           <table bgcolor=#9ca9c9>
           <tr bgcolor=#e7eaf2><td> Name:</td><td>
            <input type="text" name="name" value="<?php echo $name ?>"> <font color=#91100d><b><?PHP if (isset($errors['name'])){ echo $errors['name']; } ?></b></font></td></tr>
            <tr bgcolor=#e7eaf2><td>Email</td><td>
            <input type="text" name="email"></td></tr>
            <tr bgcolor=#e7eaf2><td colspan=2 align="right"><input type="submit" name="submit" value="submit" style="width:200px;border: 1px dashed #BBB;background-color: #c6eef1"}
></td></tr>
        </form>
</table>
<?php   }
?>

Following I have created form validation using java scripts(Please comment I have good collection of java scripts too)

Name
EMail
Phone Number
Country




<html>
<head>
<title>Form Validation</title>
<script type="text/javascript">
<!--
// Form validation code will come here.
function validate()
{

   if( document.myForm.Name.value == "" )
   {
     alert( "Please provide your name!" );
     document.myForm.Name.focus() ;
     return false;
   }
   if( document.myForm.EMail.value == "" )
   {
     alert( "Please provide your Email!" );
     document.myForm.EMail.focus() ;
     return false;
   }
   if( document.myForm.phone.value == "" ||
           isNaN( document.myForm.phone.value ) ||
           document.myForm.phone.value.length != 10 )
   {
     alert( "Please provide a phone in numbers and digits is more than 10" );
     document.myForm.phone.focus() ;
     return false;
   }
   if( document.myForm.Country.value == "-1" )
   {
     alert( "Please provide your country!" );
     return false;
   }
   return( true );
}
//-->
</script>
</head>
<body>
 <form action="" name="myForm"
          onsubmit="return(validate());">
 <table bgcolor=#eac6c5 align="center">
 <tr>
   <td align="right">Name</td>
   <td><input type="text" name="Name" /></td>
 </tr>
 <tr>
   <td align="right">EMail</td>
   <td><input type="text" name="EMail" /></td>
 </tr>
 <tr>
   <td align="right">Phone Number</td>
   <td><input type="text" name="phone" /></td>
 </tr>
 <tr>
 <td align="right">Country</td>
 <td>
 <select name="Country">
   <option value="-1" selected>[choose country]</option>
   <option value="1">USA</option>
   <option value="2">UK</option>
   <option value="3">CANADA</option>
 </select>
 </td>
 </tr>
 <tr>
   <td align="right"></td>
   <td><input type="submit" value="Submit" /></td>
 </tr>
 </table>
 </form>
 </body>
 </html>

Friday, July 20, 2012

Rename file php


Rename file php
When you upload a file into a folder having a file name same with uploading file renaming of file is essential.
create folder name as upload.
The file php-big.jpg has been uploaded
File Name :4438
  • 1006read2.php
  • 1153xa3.png
  • 1654php-big.jpg
  • 3854write.php
  • 3880read2.php
  • 4438php-big.jpg
  • 4660php-big.jpg
  • 6499php-big.jpg
  • 660php-big.jpg
  • 7956xa3.png
  • read.php
  • write.php

  • <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form action="uploader.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td><strong>Single File Upload </strong></td>
    </tr>
    <tr>
    <td>Select file
    <input name="ufile" type="file" id="ufile" size="50" /></td>
    </tr>
    <tr>
    <td align="center"><input type="submit" name="Submit" value="Upload" /></td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>

    uploader.php

    <html>
    <body>
    <?php
    $file_name = $_POST_FILES['ufile']['name'];
    $random_digit=rand(0000,9999);
    $new_file_name=$random_digit.$file_name;
    $target_path = "upload/".$new_file_name;

    $target_path = $target_path . basename( $_FILES['ufile']['name']);

    if(move_uploaded_file($_FILES['ufile']['tmp_name'], $target_path)) {
    echo "The file ". basename( $_FILES['ufile']['name']).
    " has been uploaded</br>";
    echo "File Name :".$new_file_name."<BR/>";

    } else{
    echo "There was an error uploading the file, please try again!";
    }

    ?>

    <?php

     $folder = "upload/";
    $handle = opendir($folder);
    while ($file = readdir($handle))
    {
     $files[ ] = $file;
    }
    closedir($handle);
    foreach ($files as $file)
     {
      echo "<tr bgcolor=#ffffff>
        <td align='center'></td>";
    echo '<li><a href="upload/'.$file.'">'.$file.'</a></li>';
    }
    ?>

    </body>
    </html>

    Thursday, July 19, 2012

    Edit File php



    Edit File php

    After you have hosted your file in a web server you could not access your file for editing further more.But as an admin you could keep  page like bellow for php editing under admin authorization.

    Above file contain the code of itself which could be edited and save. bellow I have shown the necessary codes.

    <form action="write.php" method="post">
    <table bgcolor=#efc9be align="center">
    <tr><td><textarea name ="data" rows="20" cols="40">
    <?php
    $lines = file('read.php');

    foreach ($lines as $line)
    {
    echo htmlspecialchars($line);
    }
    $fileString = file_get_contents('read.php');

    ?>
    </textarea></td></tr>
    <tr><td align="right"><input type="submit" value="Save"/></td></tr></table>
    </form>vv

    write.php 

    <?php
    $data=$_POST['data'];
    $myFile = "read.php";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData =$data;
    fwrite($fh, $stringData);
    fclose($fh);
    header("location: read.php");

    ?>

    Wednesday, July 18, 2012

    Numbers to Roman Number Convertion php



    Numbers to Roman number Conversion php

    Using ph arrays conversion of Numbers in to roman numbers could be done.
       
    45 = XLV = 45
    Enter Number

    Following php scripts covert number in to roman number.

    <?php
     
       $natural_roman = array(1000 => 'M', 500 => 'D', 100 => 'C', 50 => 'L', 10 => 'X', 5 => 'V', 1 => 'I');
     

       function to_natural($numeral)
       {
           global $natural_roman;
           $numeral = str_replace(
                                   array('CM', 'CD', 'XC', 'XL', 'IX', 'IV'),
                                   array('DCCCC', 'CCCC', 'LXXXX', 'XXXX', 'VIIII', 'IIII'),
                                   trim($numeral)
                               );                      
           for ($total = 0, $n = 0; $n < strlen($numeral); $n++)
           {
               if ($numeral[$n] == 'I' && $n <> strlen($numeral)-1 && $numeral[$n+1] <> 'I')
                   $number = array_search($numeral[++$n], $natural_roman)-1;
               else
                   $number = array_search($numeral[$n], $natural_roman);
               $total += $number;
           }
           return $total;
       }
     
       // convert to roman numbers
       function to_roman($natural)
       {
           global $natural_roman;
           reset($natural_roman);
           while (list($key, $value) = each($natural_roman))
           {
               while ($natural >= $key)
               {
                   $natural -= $key;
                   $rn .= $value;
               }
           }
           return str_replace(
                                   array('DCCCC', 'CCCC', 'LXXXX', 'XXXX', 'VIIII', 'IIII'),      
                                   array('CM', 'CD', 'XC', 'XL', 'IX', 'IV'),
                                   $rn
                               );      
       }
     
       // * example use - show roman numbers for 1 to 500 and then convert back * //
     
       $n=$_POST['num'];
           $rom = to_roman($n);
           $nat = to_natural($rom);
           echo '<center>'.$n . ' = ' . $rom . ' = ' . $nat . '<center>';

     
    ?>
    <div id="formContent" align="center">
            <form action="" method="post" enctype="multipart/form-data">

            <fieldset style="width: 100px; height: 100px;">
                <legend>Enter Number</legend>

                <table border="0" width=10>
                    <tr>
                        <td><label for="country">Number: </label></td>
                        <td><input type="text"  name="num">
                        <input type="submit" id="submit" name="submit" value="Find Romen Number"></td>
                    </tr>  
                  
                  
                      
                  
              
            </fieldset>  
            </table>
            </form>
        </div>

    Tuesday, July 17, 2012

    Encryption and Decryption php



    Encryption and Decryption php

    Sending some data with encryption.Viewing then after decryption is very important in data security.  Following example created to encrypt data for sending and decrypt data for viewing.

    <form action="encrypt.php" method="get">
    <textarea name ="input" rows="2" cols="20">
    </textarea>
    <input type="submit" value="Encrypt"/>
    </form>
    <form action="decrypt.php" method="get">
    <textarea name ="data" rows="2" cols="20">
    </textarea>
    <input type="submit" value="Decrypt"/>
    </form>
    <?php
    function encrypt_string($input)
    {

        $inputlen = strlen($input);
        $randkey = rand(1, 9);

        $i = 0;
        while ($i < $inputlen)
        {

            $inputchr[$i] = (ord($input[$i]) - $randkey);//encrpytion

            $i++; // For the loop to function
        }

    //Puts the $inputchr array togtheir in a string with the $randkey add to the end of the string
        $encrypted = implode('.', $inputchr) . '.' . (ord($randkey)+50);
        return $encrypted;
    }

    $input = $_GET["input"];
    $encrypted = encrypt_string($input);
    echo "<strong>Encrypted:</strong> $encrypted";
    ?>
    <?php
    function decrypt_string($input)
    {
      $input_count = strlen($input);

      $dec = explode(".", $input);
      $x = count($dec);
      $y = $x-1;

      $calc = $dec[$y]-50;
      $randkey = chr($calc);

      $i = 0;

       while ($i < $y)
      {

        $array[$i] = $dec[$i]+$randkey;
        $real .= chr($array[$i]);

        $i++;
      };

    $input = $real;
    return $input;
    }

    $input = $_GET["data"];
    $decrypted = decrypt_string($input);
    echo "<strong>Decrypted:</strong> $decrypted";
    ?>

    watermark image php



    watermark image php

    Create 2 folders called  source and watermark Then find suitable image in png format(I ave used above header image)  for wster marking.create following files and see what will be happening.


    Before watermark

    http://localhost/watermark/watermark/best-australia-beach.jpg
    After watermarking
    Select a file to upload for processing

    <form action="watermark-image.php" method="post" enctype="multipart/form-data">
     Select a file to upload for processing<br>
     <input type="file" name="File1"><br>
     <input type="submit" value="Submit File">
    </form>ccccc
    <?php


     define( 'WATERMARK_OVERLAY_IMAGE', 'watermark.png' );
     define( 'WATERMARK_OVERLAY_OPACITY', 50 );
     define( 'WATERMARK_OUTPUT_QUALITY', 90 );

     function create_watermark( $source_file_path, $output_file_path )
     {
      list( $source_width, $source_height, $source_type ) = getimagesize( $source_file_path );

      if ( $source_type === NULL )
      {
       return false;
      }

      switch ( $source_type )
      {
       case IMAGETYPE_GIF:
        $source_gd_image = imagecreatefromgif( $source_file_path );
        break;
       case IMAGETYPE_JPEG:
        $source_gd_image = imagecreatefromjpeg( $source_file_path );
        break;
       case IMAGETYPE_PNG:
        $source_gd_image = imagecreatefrompng( $source_file_path );
        break;
       default:
        return false;
      }

      $overlay_gd_image = imagecreatefrompng( WATERMARK_OVERLAY_IMAGE );
      $overlay_width = imagesx( $overlay_gd_image );
      $overlay_height = imagesy( $overlay_gd_image );

      imagecopymerge(
       $source_gd_image,
       $overlay_gd_image,
       $source_width - $overlay_width,
       $source_height - $overlay_height,
       0,
       0,
       $overlay_width,
       $overlay_height,
       WATERMARK_OVERLAY_OPACITY
      );

      imagejpeg( $source_gd_image, $output_file_path, WATERMARK_OUTPUT_QUALITY );

      imagedestroy( $source_gd_image );
      imagedestroy( $overlay_gd_image );
     }



     define( 'UPLOADED_IMAGE_DESTINATION', 'source/' );
     define( 'PROCESSED_IMAGE_DESTINATION', 'watermark/' );

     function process_image_upload( $Field )
     {
      $temp_file_path = $_FILES[ $Field ][ 'tmp_name' ];
      $temp_file_name = $_FILES[ $Field ][ 'name' ];

      list( , , $temp_type ) = getimagesize( $temp_file_path );

      if ( $temp_type === NULL )
      {
       return false;
      }

      switch ( $temp_type )
      {
       case IMAGETYPE_GIF:
        break;
       case IMAGETYPE_JPEG:
        break;
       case IMAGETYPE_PNG:
        break;
       default:
        return false;
      }

      $uploaded_file_path = UPLOADED_IMAGE_DESTINATION . $temp_file_name;
      $processed_file_path = PROCESSED_IMAGE_DESTINATION . preg_replace( '/\\.[^\\.]+$/', '.jpg', $temp_file_name );

      move_uploaded_file( $temp_file_path, $uploaded_file_path );

      $result = create_watermark( $uploaded_file_path, $processed_file_path );

      if ( $result === false )
      {
       return false;
      }
      else
      {
       return array( $uploaded_file_path, $processed_file_path );
      }
     }



     $result = process_image_upload( 'File1' );

     if ( $result === false )
     {

      echo '<br>An error occurred !';
     }
     else
     {
      echo '<table bgcolor=#ccc><tr><td>Original image </td><td> <a href="' . $result[ 0 ] . '" target="_blank">' . $result[ 0 ] . '</a></td></tr>';
      echo '<tr><td>Watermarked image  </td><td><a href="' . $result[ 1 ] . '" target="_blank">' . $result[ 1 ] . '</a></td></tr></table>';
     }
    ?>

    Create image php


    Create image php

    Following application create an image according to the input color you submitted.
    Enter Red:
    Enter Green:
    Enter Blue:
    <form action="color.php" method="post">
    <table bgcolor=#5f5e59 align="center">
    <tr bgcolor=#f6f4ef><td>Enter Red:</td><td>
    <input type="text" name="red"></td></tr>
    <tr bgcolor=#f6f4ef><td>Enter Green:</td><td>
    <input type="text" name="green"></td></tr>
    <tr bgcolor=#f6f4ef><td>Enter Blue:</td><td>
    <input type="text" name="blue"></td></tr>
    <tr bgcolor=#f6f4ef><td align="right" colspan=2><input type="submit"value="Get Image"/></td></tr>
    </table>
    </form>
    color.php

    <?php
     header ("Content-type: image/png");
     $red=$_POST['red'];
     $green=$_POST['green'];
     $blue=$_POST['blue'];
     $handle = ImageCreate (130, 50) or die ("Cannot Create image");
     $bg_color = ImageColorAllocate ($handle, $red, $green, $blue);
     ImagePng ($handle);
    ?>

    Monday, July 16, 2012

    URL Rotation php



    URL Rotation php

    This tutorial shows you changing URL in each 5 seconds select images 3 and name as image1,image2 and image3.
    then create following 3 pages to be rotated(page5.php,page6.php and page7.php)


    <?php
    header('Refresh: 10; URL=page5.php');
    session_start();
        // set timeout period in seconds
        $inactive = 500;
        // check to see if $_SESSION['timeout'] is set
        if(isset($_SESSION['timeout']) ) {
        $session_life = time() - $_SESSION['start'];
        if($session_life > $inactive)
        { session_destroy(); header("Location: page6.php"); }
        }
        $_SESSION['timeout'] = time();

    ?>
    <img src="image1.jpg">
    <?php
    header('Refresh: 10; URL=page6.php');
    session_start();
        // set timeout period in seconds
        $inactive = 500;
        // check to see if $_SESSION['timeout'] is set
        if(isset($_SESSION['timeout']) ) {
        $session_life = time() - $_SESSION['start'];
        if($session_life > $inactive)
        { session_destroy(); header("Location: page7.php"); }
        }
        $_SESSION['timeout'] = time();

    ?>
    <img src="image2.jpg">
    <?php
    header('Refresh: 10; URL=page7.php');
    session_start();
        // set timeout period in seconds
        $inactive = 500;
        // check to see if $_SESSION['timeout'] is set
        if(isset($_SESSION['timeout']) ) {
        $session_life = time() - $_SESSION['start'];
        if($session_life > $inactive)
        { session_destroy(); header("Location: page5.php"); }
        }
        $_SESSION['timeout'] = time();

    ?>
    <img src="image3.jpg">

    Sunday, July 15, 2012

    Create icon php



    Create icon php

    First create 2 folders called icon and images where uploaded and created images are stored.Then create following php file

    Select your image:
    Image Size:
                                                            Click here to download
    <?php
    if($_POST['submit'])
    {
        $imgname = $_FILES['icoimage']['name'];
        if($_POST['s1'])
            $size=16;     
        elseif($_POST['s2'])        
            $size=32;
        if(str_replace(" ","",$size)=="")
            $size=32;

        $target_path = "images/";
        $target_path = $target_path . $imgname;
        if(move_uploaded_file($_FILES['icoimage']['tmp_name'], $target_path))
        {
            include_once "create.php";
            makeimage($target_path,$size,$size);
            unset($target_path);unset($size);unset($imgname);  
        }
        else
        {
            echo "Error in converting File";
        }

      
    }

    ?>
    <html>

    <script language="javascript">
    function check()
    {
        var file=document.getElementById("icoimage");
        var f=file.value;
        var extn=f.substring(f.lastIndexOf(".")+1,(f.length));
        file=file.value;
        if (extn=="jpg" || extn=="gif" || extn=="jpeg" || extn=="png")
        {
            return true;
        }
        if (f.length==0)
        {
            return false;
        }
      
        else
        {
            return true;
        }
    }
    </script>
    <form enctype="multipart/form-data" method="post"   name="iconconverter" onSubmit="return check();">

    <table align=center bgcolor=#858278>
    <tr bgcolor=#eeece7>
    <td>
    Select your image: </td><td><input type="file" id="icoimage" name="icoimage" />
    </td>
    </tr>
    <tr bgcolor=#eeece7>
    <td>
    Image Size:</td><td>
    <select name="size">
    <option value="s1">16x16 </option>
    <option value="s2">32x32</option>
    </select>
    </td>
    </tr>
    <tr bgcolor=#eeece7>
    <td align="right" colspan=2>
    <input type="submit" value="submit" name="submit"/>
    </td>
    </tr>
    </form>
    </table>
    </html>
    <?php
    if($_POST['submit'])
    { ?>
    <table align=center width=30%><tr><td align="center">
    <a href="download.php"><?php

     $folder = "icon/";
    $handle = opendir($folder);
    while ($file = readdir($handle))
    {
     $files[ ] = $file;
    }
    closedir($handle);
    foreach ($files as $file)
     {
    echo "Click here to download<img src='icon/$file'>";
    }
    ?>
    </a>
    <?php
    }
    else
    {

    }
    ?>
    create.php 

    <?php
    function makeimage($filename, $newwidth, $newheight) {
    $image_type = strstr($filename,  '.');
    switch($image_type) {
                case '.jpg':
                    $source = imagecreatefromjpeg($filename);
                    break;
                case '.png':
                    $source = imagecreatefrompng($filename);
                    break;
                case '.gif':
                    $source = imagecreatefromgif($filename);
                    break;
                default:
                    echo("Error Invalid Image Type");
                    die;
                    break;
                }
        $file = "created.ico";
        $fullpath = $file;
        list($width,  $height) = getimagesize($filename);
        $thumb = imagecreatetruecolor($newwidth,  $newheight);
        imagecopyresized($thumb,  $source,  0,  0,  0,  0,  $newwidth,  $newheight,  $width,  $height);
       $fullpath="icon/".$fullpath;
        imagepng($thumb,$fullpath);
     }
    ?>
    download.php 

    <?php
    $file = 'icon/created.ico';
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
    ?>

    Saturday, July 14, 2012

    Create zip file php


    Create zip file php

    This is an application type of program you need to create number of files.First you need to upload the files in to a directory which are to be zipped.My example Files have been uploaded to directory created as "files" .I think you may have created multiple files upload php in my previous tutorials. Same scripts have been used in this tutorial also for file uploading.
    Following are the multiple files uploading scripts

    Multiple File Upload

    Add another

    <html>
    <head>
     <title>multiple file upload </title>
    <script type="text/javascript">
    function add_file_field(){
    var container=document.getElementById('file_container');
    var file_field=document.createElement('input');
    file_field.name='images[]';
    file_field.type='file';
    container.appendChild(file_field);
     var br_field=document.createElement('br');
    container.appendChild(br_field);
    }
    </script>
    </head>
    <body>
     <form action="mupload.php" method="post" enctype="multipart/form-data" name="mutiple_file_upload_form" id="mutiple_file_upload_form">
     <h3>Multiple File Upload </h3>
    <div id="file_container"> <input name="images[]" type="file" /> <br /> </div>
    <a href="javascript:void(0);" onClick="add_file_field();">Add another</a><br />
    <input type="submit" name="Submit" value="Submit" />
    </form>
    </body>
     </html>
    mupload.php 

    <?php
     if (isset($_POST['Submit'])) {
     $number_of_file_fields = 0;
     $number_of_uploaded_files = 0;
    $number_of_moved_files = 0;
    $uploaded_files = array();
    $upload_directory = dirname(__file__) . '/files/';
     for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
     $number_of_file_fields++;
     if ($_FILES['images']['name'][$i] != '') {
     $number_of_uploaded_files++;
     $uploaded_files[] = $_FILES['images']['name'][$i];
     if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
    $number_of_moved_files++;
     }
     }
     }
    header("location: czip.php");
    }
     ?>

    Changes,I have made from previous tutorial on file upload, I have marked in bold blue color.Next you have to create  czip.php file as bellow.







    Create a ZIP


    File Name
    .
    ..
    flowers.jpg
    onlinewebapplication.docx
    onlinewebapplication.pdf
    <?php
        $error = "";      
        if(isset($_POST['createzip'])){
            $post = $_POST;      
            $file_folder = "files/";  
            if(extension_loaded('zip')){  
                if(isset($post['files']) and count($post['files']) > 0){  
                    $zip = new ZipArchive();          
                    $zip_name = time().".zip";          
                    if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){      
                        $error .=  "* Sorry ZIP creation failed at this time<br/>";
                    }
                    foreach($post['files'] as $file){              
                        $zip->addFile($file_folder.$file);          
                    }
                    $zip->close();
                    if(file_exists($zip_name)){
                      
                        header('Content-type: application/zip');
                        header('Content-Disposition: attachment; filename="'.$zip_name.'"');
                        readfile($zip_name);
                      
                        unlink($zip_name);
                    }
                  
                }else
                    $error .= "* Please select file to zip <br/>";
            }else
                $error .= " ZIP extension Missing<br/>";
        }
    ?>

    <html>
    <head>

    <title>create Zip</title>
    </head>
    <body>
    <center><h3>Create a ZIP</h3>
    </center>
    <form name="zips" method="post">

    <table bgcolor=bgcolor=#b19879 align="center">
      <tr bgcolor=#e5d8b2>
        <td width="33" align="center"><img src="rt.jpg"></td>
        <td width="117" align="center">File Name</td>
      
      </tr>
    <?php

     $folder = "files/";
    $handle = opendir($folder);
    while ($file = readdir($handle))
    {
     $files[ ] = $file;
    }
    closedir($handle);
    foreach ($files as $file)
     {
      echo "<tr bgcolor=#ffffff>
        <td align='center'><input type='checkbox' name='files[]' value='$file' /></td>";
     echo "<td>".$file."</a></td></tr>";
    }
    ?>
      

     
        <tr bgcolor=#e5d8b2><td colspan="2" align="center">
            <input type="submit" name="createzip"value="Download as ZIP" /></td></tr>
          <tr bgcolor=#e5d8b2> <td colspan="2" align="center"><input type="reset" name="reset"  value="Reset" /></td></tr>
         <tr bgcolor=#e5d8b2> <td colspan="2" align="center"><a href="empty.php"><img src="ep.png"></a></td></tr>
        </td>
        </tr>
    </table>

    </form>

    </body>
    </html>

    Then create empty.php to make your folder empty.

    <?PHP

    function empty_folder($folder, $debug = false){
     
        if ($debug) {
            header("location: czip.php");
        }
     
        $d = dir($folder);
     
        while (false !== ($entry = $d->read())) {
     
            $isdir = is_dir($folder."/".$entry);
         
            if (!$isdir and $entry!="." and $entry!="..") {
         
                unlink($folder."/".$entry);
             
            } elseif ($isdir  and $entry!="." and $entry!="..") {
         
                empty_folder($folder."/".$entry,$debug);
             
                rmdir($folder."/".$entry);
             
            }
        }
        $d->close();
    }
    empty_folder("files",true);
    ?>