- First we make a login page in html.
- The we create a database in phpAdmin.
- Then through query we used to access the database.
First make an html page:
PAGE :-home.php
<form name="frm" method="POST"> <table> <tr> <td>Login ID</td> <td><input type="text" name="LoginId" /></td> </tr> <tr> <td>Password</td> <td><input type="password" name="Password" /></td> </tr> <tr> <td><input type="checkbox" name="rememberme" />Remember me!</td> <td><input type="submit" value="Login" /></td> </tr> </table> </form>
Save that page with home.php extension and the output of page when we run on browser is:-
Then we create database in phpmyadmin
The name of database is xyz in this we create a table user have values UID, LoginId, Password and Address.
Then we create a php page that get data from html form and compare it to the database the coding of page is describe below save that page with match.php.
PAGE: - match.php
<?php ob_start(); session_start(); $a= $_POST['LoginId']; $b= $_POST['Password']; $c=$_POST['rememberme']; $encrypted_mypassword= $b; $con=mysql_connect("localhost","root",""); if(!$con){ die('Cound'.mysql_error()); } mysql_select_db("xyz",$con); $result= mysql_query("select * from user where LoginId='$a' and Password='$encrypted_mypassword'"); $row= mysql_fetch_array($result); if($row){ $_SESSION['LoginId']= $a; if (isset($a) && isset($encrypted_mypassword)) { if (($a) && ($encrypted_mypassword)){ if (isset($c)) { /* Set cookie to last 1 year */ setcookie('LoginId', $a, time()+60*60*24*365); setcookie('Password', md5($encrypted_mypassword), time()+60*60*24*365); } else { /* Cookie expires when browser closes */ setcookie('LoginId', $a, false); setcookie('Password', md5($encrypted_mypassword), false); } header('Location: next.php'); } else { echo "Username/Password Invalid"; } } else { echo "You must supply a username and password."; } } else { include("home.php"); } mysql_close($con); ?>
When we click on login box we reach on next.php page the codding of that page is describe below:-
PAGE:- next.php
<?php ob_start(); session_start(); echo $_SESSION['LoginId']; if(isset($_SESSION['LoginId'])){ echo "You were already logged in ".$_SESSION['LoginId']."."; echo "<a href="/p.php?id=.$_SESSION['LoginId'].">Click Here</a>"; }else{ header("location: home.php"); } ?>
Then we create a php page that get data from html form and compare it to the database the coding of page is describe below save that page with match.php.
PAGE: - match.php
<?php ob_start(); session_start(); $a= $_POST['LoginId']; $b= $_POST['Password']; $c=$_POST['rememberme']; $encrypted_mypassword= $b; $con=mysql_connect("localhost","root",""); if(!$con){ die('Cound'.mysql_error()); } mysql_select_db("xyz",$con); $result= mysql_query("select * from user where LoginId='$a' and Password='$encrypted_mypassword'"); $row= mysql_fetch_array($result); if($row){ $_SESSION['LoginId']= $a; if (isset($a) && isset($encrypted_mypassword)) { if (($a) && ($encrypted_mypassword)) { if (isset($c)) { /* Set cookie to last 1 year */ setcookie('LoginId', $a, time()+60*60*24*365); setcookie('Password', md5($encrypted_mypassword), time()+60*60*24*365); } else { /* Cookie expires when browser closes */ setcookie('LoginId', $a, false); setcookie('Password', md5($encrypted_mypassword), false); } header('Location: next.php'); } else { echo "Username/Password Invalid"; } } else { echo "You must supply a username and password."; } } else { include("home.php"); } mysql_close($con); ?>
As we pass session value to that page and we print session value to that page so it will print session value 001. And we pass the hyperlink of another page through click here. And also pass the session value and cookies value through query string to another page i.e. p.php. as we encrypt password with md5 algorithm so it will pass its encrypted value through query string.
PAGE – p.php
<?php ob_start(); session_start(); echo "Hellow".$_COOKIE['LoginId']; echo "<br />"; echo "Password=".$_COOKIE['Password']; echo "<br />"; if(isset($_SESSION['LoginId'])){ unset($_SESSION['LoginId']); echo "<a href="/home.php">Logout</a>"; } else { header("location: home.php"); } ?>
Then we create a php page that get data from html form and compare it to the database the coding of page is describe below save that page with match.php.
Here we again print session value and the query string value is show on toolbar. As:-
<a href="http://localhost/xyz/p.php?id=001.?Pwd=dc5c7986daef50c1e02ab09b442ee34f"> <?php echo p.php?id=001.?Pwd=dc5c7986daef50c1e02ab09b442ee34f;?> </a>
and when we click logout hyperlink the session is expired and the page will redirect on login page
PHP ARRAY
Like another programming language php also having array. Array is a special variable which can used to store multiple values of same data type.
Array is used to hold all values under one single name and we used that value by referring that name of array. Array can be accessed by its index value.
Type of array in PHP
- 1.Numeric Array.
- 2.Associative Array.
- 3.Multidimensional Array
- Numeric Array: - In numeric array store each element with a numeric index.
There are generally two methods to create a numeric array:
- a)$computer = array("Acer”,”Lenovo”,”deal”,”hp”);
In this the index value are automatically assigned to each array element starts from 0.
- b)$computer[0] = “Acer”;
- c)$computer[1] = “Lenovo”;
- d)$computer[2] = “deal”;
- e)$computer[3] = “hp”;
In this we assign the index value to each element in array.
Example:
$computer[0] = “Acer”;
$computer[1] = “Lenovo”;
$computer[2] = “ deal”;
$computer[3] = “hp”;
Echo $computer[0].”and”.$computer[1].”are cheap and best”;
?>
Output: Acer and Lenovo are cheap and best
- Associative Array: In this each id is associative with a value.
Numeric array is not best in storing data about specific named so we can use associative array. With associative array we can use the value as key and assign to them.
Example :
$employee = array (“Ram”=>23,”Sham”=>24,”Pankaj”=>23);
Or
$employee [‘Ram’] =”23”;
$employee [‘Sham’] = “24”;
$employee [‘Pankaj’] = “23”;
Example:
$employee [‘Ram’] =”23”;
$employee [‘Sham’] = “24”;
$employee [‘Pankaj’] = “23”;
echo “Ram is”.$employee[‘Ram’].”Year old”;
?>
Output: Ram is 23 year old.
- Multidimensional Array: in this an array element contain array or we can say that each element in the main array can also be an array. And each element in the sub-array can be an array.
Example:
Array
(
[kumar]=> array
(
[0] =>pankaj
[1]=>amit
)
[singh]=> array
(
[0]=> sumit
)
)
PHP also having operators like other programming Languages like Java, C++, and C etc.
PHP operators are:-
- Assignment Operators.
- Arithmetic’s Operators.
- Comparison Operators.
- String Operators.
Assignment Operators:
PHP having assignment operators i.e. (=)
Example:
Arithmetic’s Operators:
PHP Arithmetic’s Operators are: +,-,*,/,%.
Example:
Comparison Operators:
Example:
String Operators:
PHP Strings Operators are:
There are generally tow String Operators in PHP
- First is a concatenation Operators (‘.’).
- The Second one is Assignment Operators (‘=’)
Example:
PHP Conditional Statements
PHP Conditional operators are:
- IF Statement.
- IF..ElSE Statement.
- IF..ELSEIF….Statement
- Switch Statement.
IF Statement: Like other programming language IF statement executes some statement when some particular condition is true.
Syntax: If (condition)
Code Execute;
Example:
IF..Else Statement: IF statement is true then execute one statement otherwise execute second statement.
Syntax:
Example:
IF..ELSEIF….Statement : If some condition is true then execute one statement else if some another statement then execute another statement. It is particularly an if else ladder.
Syntax:
Example:
Switch Statement: To overcome the multiple If-Else statement problems we used switch statement. It is conditional statement are used to perform different action based on different conditions.
Working of Switch Statement: It takes one variable as input and checks it against all the different cases you set for the switch statements.
Syntax:
What is PHP?
Php stands for pre hypertext programming language or hypertext preprocessor. It is a server site programming language like other programming language like Asp.net etc.
Now a day it is very much popular in website development. Many big websites like yahoo, Facebook and many other social networking sites are generally made in php .
Php files have a file extension .php, .php3 , .phtml etc.
Features of PHP
There are many features of php:
- It is server site programming language.
- Php is an open source language and it can be free download and use .
- It is a loosely type language there is no need to declare the type of variable in php.
Database in PHP
Php support many databases like MySQL, Informix, Oracel, ODBC etc. Rather than it is also having its database i.e. phpMyAdmin.
Basic Body Architecture of PHP
Statement;
Statement;
?>
The whole programmed in php is written in between .
Evolution of PHP
Php was developed by Ramus Lerdrof in 1994. After that it was modified by Zeev Suraski and Andi Gutmans in 1997.
After installation php in your computer you can test your browser by typing:
http://localhost and then press enter
Variables in PHP
Variables are used to store information or a value to its. Like other programming language php have its own variable .but in php there is no need to define its data type it will be automatically regret its vale that it is of integer or character type.
In php variable define with a sigh of ‘$’ i.e.
$a, $abc etc.
The first character after $ must be character or underscore. The remaining character in name may be letter , number or underscore.
Syntax :
$variable_name = value;
Example:
$a
$a=10;
$b=20;
$c= $a+$b;
First programme in PHP
Echo “Hello”;
php?>
echo: eco is used for output a string and must be ended with a semicolon. The string must be written in between doubleqoutes.
We can also use print as in place of echo.
Example:
Print “Hello My Name is Pankaj Singh”;
?>
Output: Hello My Name is Pankaj Singh
Example:
$abc=” Hello My Name is Pankaj Singh”;
Echo $abc;
Print $abc;
?>
Output of above program is:
Hello My Name is Pankaj Singh Hello My Name is Pankaj Singh
Comment in PHP
There are generally two types of comments used in php
- Single line comments : //
- Multiline comment: /*….*/
Example:
// this is a single line comment
?>
/* This
Is a
Multiline comment
*/
?>
Introduction to PHP
PHP stands for Hypertext Preprocessor.It is a server-side scripting language.It is an open source software and free to download and use.
PHP files have a file extension of ".php", ".php3", or ".phtml".
PHP script:
PHP scripting block always starts with
Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another.
variables :
All variables will start with $ symbol.
Simple example
$var_name = value;
condition statement:
All the condition statement syntax are same as in C language.Example if..else,switch.
Array:
Array is a homogenious collection of elements.
$arr[0]="12345";
echo $arr[0];
There are 3 types of arrays
1)Numerical arrays
2)Associative array
3)Multi dimensional arrays
Numerical arrays:
A numeric array stores each array element with a numeric index.
$type=array("cat","dog","rat");
Internally it will store as
$type[0]="cat";
$type[1]="dog";
$type[2]="rat";
Example
$type[0]="cat";
$type[1]="dog";
$type[2]="rat";
echo $type[0] . " kills " . $type[2];
?>
Associative array:
In associative array the index is replaced with a value.
$names = array("ravi"=>40, "rani"=>45, "ram"=>50);
$names['ravi']="40";
$names['rani']="45";
$names['ram']="50";
example:
$names['ravi']="40";
$names['rani']="45";
$names['ram']="50";
echo "marks of ravi,rani,ram are". $names['ravi'] .$names['rani']. $names['ram'];
?>
Loops:
All the loop statement syntax also similar as in C language.
example:
WHILE
while (condition)
{
code to be executed;
}
example:
$temp=0;
while($temp<=10)
{
echo "The number is" . $temp ;
$temp++;
}
?>
DO-WHILE
do
{
code to be executed;
}
while (condition);
example:
$temp=0;
do
{
echo "The number is" . $temp ;
$temp++;
}while($temp<=10)
?>
FOR
for (init; condition; increment)
{
code to be executed;
}
example:
for($temp=0;$temp<=10;$temp++;)
{
echo "The number is" . $temp ;
}
?>