Thursday, 30 July 2015

LOIGN-MODULE-CODE-IN-PHP

Login Module with session  Management:


(1)First create  databse & table  & insert values  for username & password:
Create database   amrapali;  
Create table  user
(id   int  primary key auto_increment,
username   varchar(20),
password   varchar(20));

(1)    Write html code for  “Loginform.html”  file , CODE IS GIVEN BELOW:
<html>
<head>
<title>
login example
</title>
</head>
<body>
<form action="login.php"    method="post">

username <input type=text    name="username">

Password <input type=text name="password">

<input type="submit"   name="submit" value="submit">

</body>
</html>






(2)write php code  which will check username & password   “login.php”  file CODE IS GIVEN BELOW:
<?php

$con=mysqli_connect("Localhost","root","","amrapali");



$username=$_POST['username'];

$password=$_POST['password'];

$sql="select  username , password from  user where username='$username' AND password='$password'";

$result=mysqli_query($con,$sql);

$num=mysqli_num_rows($result);

if($num>0)
{

session_start();

$_SESSION['username']=$username;

header('Location:welcome.php');

}

else
{

echo "invalid login";

}

?>














(3)AFTER LOGIN  page will display that is  “welcome.php”:
<?php

session_start();

if(!isset($_SESSION['username']))
{

header("Location:error.php");

}


echo "<h1>welcome </h1>";


echo '<a href="logout.php">Logout</a>';




?>



(4)logout.php   file this file destroy session:
<?php

session_start();

session_destroy();

header("Location:loginform.html");

?>

(5)error.php file:

<?php

echo "first make login ";

echo '<a  href="loginform.html">Login here</a>';

?>



PAGINATION CODE

PAGINATION CODE  :file name is   pagination.php 
NOTE: JUST CHANGE YOUR DATABASE NAME & TABLE NAME & FIELD NAME OF TABLE & USE THIS CODE   
<?php

    mysql_connect("localhost","root","")or die("Problems...");
    mysql_select_db("payroll");

    $per_page = 2;
               
    $pages_query = mysql_query("SELECT COUNT('id') FROM  employee");
               
    echo $pages_query."<br>";
               
    $pages = ceil(mysql_result($pages_query, 0) / $per_page);
               
    echo $pages;
  
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
               
    $start = ($page - 1) * $per_page;
  
    $query = mysql_query("SELECT *  FROM employee LIMIT $start, $per_page");
  
    while($row = mysql_fetch_assoc($query))
  
{
  
 echo '<form action=update.php  method=post      enctype="multipart/form-data">
<table border=1  cellspacing=3 cellpadding=2 bgcolor=orange  bordercolor=black>
<tr><td>DATE OF SIGN UP</td><td><INPUT TYPE=TEXT NAME="name"  value="'.$row['name'].'"></td></tr>
<tr><td>NAME</TD><td><INPUT TYPE=TEXT NAME="phone"      value="'.$row['phone'].'"></td></tr>
<tr><td>ADDRESS</TD><TD><input type=text name="salary"        value="'.$row['salary'].'"></td></tr>

<tr><td> </td><td>
<input type=hidden name=hidden  value="'.$row['emp_id'].'">
<input type=submit name=update value=update></td></tr>


</TABLE>


</FORM>';


  
  
  
    }
  
    $prev = $page - 1;
    $next = $page + 1;
  
    if(!($page<=1))
                {
        echo "<a href='pagination.php?page=$prev'>Prev</a> ";
    }

    if($pages>=1 && $page<=$pages)
                {
  
        for($x=1;$x<=$pages;$x++)
                                {
            echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a> ';
      
        }
  
    }
  
    if(!($page>=$pages))
                {
        echo "<a href='pagination.php?page=$next'>Next</a>";
    }
  
  

?>

Registration_ Form _ & _ PHP _form _Validation (passing data from Ajax to Php file)

(1) see Ajax code which passes  data from  form to php , see code of Registrationform.html :
<html>
<head>

<script>
  
function ajax_post(){
    // Create our XMLHttpRequest object
 
    var hr = new XMLHttpRequest();
 
    // Create some variables we need to send to our PHP file
 
    var url = "validation.php";
 
    var fn = document.getElementById("name").value;
 
    var en = document.getElementById("email").value;
 
 
 var pn = document.getElementById("phone").value;
 
 
 var pd = document.getElementById("password").value;
 
 
 var cpd = document.getElementById("cpassword").value;
 
 
 var cap = document.getElementById("capcha").value;
 
 
 
 
 

    var vars = "name="+fn+"&email="+en+"&phone="+pn+"&password="+pd+"&cpassword="+cpd+"&capcha="+cap;
 
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
     if(hr.readyState == 4 && hr.status == 200) {
      var return_data = hr.responseText;
   document.getElementById("status").innerHTML = return_data;
     }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
}
</script>

</head>

<body>


Name <input type="text"  id="name"     name="name"      ><br></br>

email <input type="text"  id="email"    name="email"      ><br></br>


phone <input type="text"  id="phone"     name="phone"      ><br></br>


password<input type="text"  id="password"    name="password"      ><br></br>



confirm password<input type="text"  id="cpassword"    name="cpassword"      ><br></br>



2+2=<input type="text"  id="capcha"    name="name"      ><br></br>

<input name="submit"    id="Button1"    type="submit" value="Submit Data"        onclick="ajax_post();"   >

<div id="
status">
</div>


</body>

</html>

(2) write code for form validation in php ,see "validation.php file code:"

<?php

$name=$_POST['name'];

$email=$_POST['email'];

$phone=$_POST['phone'];

$password=$_POST['password'];

$cpassword=$_POST['cpassword'];

$capcha=$_POST['capcha'];

$con=mysql_connect("localhost","root","");

mysql_select_db("itexam",$con);

$sql="select * from  studentrecord where   email='$email'";

$result=mysql_query($sql);

$num=mysql_num_rows($result);

$regex = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$^";

$errormsg=array();

 if($capcha=="" )

 {

 $errormsg="Plase Enter Your Answer";

 }

 else if($capcha==4)


 {

  $errormsg="";



 }

 else

 {

 $errormsg="incorrect answer";

 }

if($name=="")

{

$errormsg="enter  your Name ";

}


if($email=="")

{

$errormsg="enter  your email ";

}

if($phone=="")

{

$errormsg="enter  your phone ";

}



if($password=="")

{

$errormsg="enter  your password ";

}



if(!preg_match( $regex, $email ))

{

$errormsg="enter your valid email";



}

if($password!==$cpassword)

{

$errormsg="password did not match";



}

if(empty($errormsg))

{



if($num>0)
{

echo "<font color=red>email id is already used please use another email id</font>";

}



else 

{


$sql1="insert into studentrecord(name,email,phone,password)values('$name','$email','$phone','$password')";

mysql_query($sql1);

echo "<font color=red>successfully  registered </font>";

}


}

else

{


echo"<font color=red> $errormsg </font>";


}


?>


PHP-DATA-TYPES AND OPEARTORS

What is PHP?
  • PHP is an acronym for "PHP: Hypertext Preprocessor"
  • PHP is a widely-used, open source scripting language
  • PHP scripts are executed on the server
  • PHP is free to download and use

What is a PHP File ?
  • PHP files can contain text, HTML, CSS, JavaScript, and PHP code
  • PHP code are executed on the server, and the result is returned to the browser as plain HTML
  • PHP files have extension ".php"

What Can PHP Do ?
  • PHP can generate dynamic page content
  • PHP can create, open, read, write, delete, and close files on the server
  • PHP can collect form data
  • PHP can send and receive cookies
  • PHP can add, delete, modify data in your database
  • PHP can be used to control user-access
  • PHP can encrypt data.

Why PHP?
  • PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
  • PHP is compatible with almost all servers used today (Apache, IIS, etc.)
  • PHP supports a wide range of databases
  • PHP is free. Download it from the official PHP resource: www.php.net
  • PHP is easy to learn and runs efficiently on the server side

Basic PHP Syntax:
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
Creating (Declaring) PHP Variables:
In PHP, a variable starts with the $ sign, followed by the name of the variable:
Example:
<?php

$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
Output:

(note if we will echo  $x and $y then we will get following output use of echo statement is given on next step..just now understand that it work similar to  printf fucntion in c , cout  in c++ ,System.out.println() in java )
Hello world!
5
10.5

PHP echo and print Statements

echo and print are more or less the same. They are both used to output data to the screen.
The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.
Echo example:
<?php
$txt1 = "Learn PHP";
$txt2 = "vissicomp.com";
$x = 5;
$y = 4;

echo "<h2>$txt1</h2>";
echo "Study PHP at $txt2<br>";
echo $x + $y;
?>
Output:
Learn PHP
Study PHP at vissicomp.com
9

Print Example:

<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?> 

PHP Data Types :
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource
  •  
PHP String :
·         A string is a sequence of characters, like "Hello world!".
·         A string can be any text inside quotes. You can use single or double quotes:

<?php 
$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "<br>";
 
echo $y;
?>

Output:

Hello world!
Hello world!

PHP Integer
An integer is a whole number (without decimals).  It is a number between -2,147,483,648 and +2,147,483,647.
Rules for integers:
  • An integer must have at least one digit (0-9)
  • An integer cannot contain comma or blanks
  • An integer must not have a decimal point
  • An integer can be either positive or negative
  • Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)
In the following example $x is an integer. The PHP var_dump() function returns the data type and value:

<?php 

$x = 5985;
var_dump($x);

?>

Output:
int(5985)

PHP Float :
A float (floating point number) is a number with a decimal point or a number in exponential form.
In the following example $x is a float. The PHP var_dump() function returns the data type and value:
<?php 
$x = 10.365;
var_dump($x);
?>
Output:
float(10.365)

PHP Array :
An array stores multiple values in one single variable.
In the following example $cars is an array. The PHP var_dump() function returns the data type and value:

<?php 

$cars = array("Volvo","BMW","Toyota");
var_dump($cars);

?>
Output:
array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }

PHP Object :
An object is a data type which stores data and information on how to process that data.
In PHP, an object must be explicitly declared.
First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods:
<?php
class Car {

    function Car()
{

        $this->model = "VW";
    }
}

// create an object

$herbie = new Car();

// show object properties

echo $herbie->model;

?>

Output :
VW

PHP NULL Value  :

Null is a special data type which can have only one value: NULL.
A variable of data type NULL is a variable that has no value assigned to it.
Tip: If a variable is created without a value, it is automatically assigned a value of NULL.
Variables can also be emptied by setting the value to NULL:

Example :
<?php

$x = "Hello world!";

$x = null;

var_dump($x);

?>

Output:

NULL


PHP String Functions :

Get The Length of a String:
The PHP strlen() function returns the length of a string (number of characters).
The example below returns the length of the string "Hello world!":
<?php
echo strlen("Hello world!"); // outputs 12
?>

Output:
12


Count The Number of Words in a String
The PHP str_word_count() function counts the number of words in a string:
<?php

echo str_word_count("Hello world!"); // outputs 2

?>
Output :
2

Reverse a String
The PHP strrev() function reverses a string:
<?php

echo strrev("Hello world!");   // outputs !dlrow olleH

?>
Output:
!dlrow olleH

Search For a Specific Text Within a String
The PHP strpos() function searches for a specific text within a string.
If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.
The example below searches for the text "world" in the string "Hello world!":
<?php

echo strpos("Hello world!", "world"); // outputs 6

?>
Output:
6

Replace Text Within a String
The PHP str_replace() function replaces some characters with some other characters in a string.
The example below replaces the text "world" with "Dolly":
<?php

echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly!

?>

Hello Dolly!






Create a PHP Constant

To create a constant, use the define() function.

Syntax

define(name, value, case-insensitive)
Parameters:
  • name: Specifies the name of the constant
  • value: Specifies the value of the constant
  • case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
The example below creates a constant with a case-sensitive name:
<?php

define("GREETING", "Welcome to vissicomp.com!");

echo GREETING;

?>

Output:
Welcome to vissicomp.com!


PHP Operators
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators

PHP Arithmetic Operators:

+
Addition

 

<?php

$x = 10; 
$y = 6;

echo $x + $y;
?>
 
Output:
16

-
Subtraction
<?php
$x = 10; 
$y = 6;

echo $x - $y;
?>
  
Output:
4
*
Multiplication

<?php
$x = 10; 
$y = 6;

echo $x * $y;
?>
  
Output:
60
/
Division

<?php
$x = 10; 
$y = 6;

echo $x / $y;

?>
  
Output:
%
Modulus

<?php
$x = 10; 
$y = 6;

echo $x % $y;
?>
 
Output:
4

PHP Assignment Operators :

The PHP assignment operators are used with numeric values to write a value to a variable.
x = y
<?php
$x = 10; 
echo $x;
?>
 
Output:
10

a += b
a= a + b
Addition

<?php

$a= 20; 

$b=100;
$a +=$b;
echo $a;
?>
 
Output:
120

a -= b
a = a- b
Subtraction


<?php

$a = 50;

$b=30;
$a -= $b;

echo $a;

?>
 
Output:
20

a *= b
a = a* b
Multiplication

<?php

$a = 10; 

$b = 6;
$a*=$b;
echo $a;

?>
 
Output:
60

a/= b
a= a/ b
Division

<?php

$a = 10;

$b=5;
$a /= $b;

echo $a;

?>
 
Output:
2
a %= b
a= a % b
Modulus

<?php
$a = 15;
$b=4;
$a%=$b;
echo $a;
?>
 
Output:
3

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):
==
Equal
$x == $y
Returns true if 
$x is equal to $y

<?php
$x = 100; 
$y = "100";

var_dump($x == $y); // returns true because values are equal

?>
 
Output:
Bool(true)

!=
Not equal
$x != $y
Returns true if $x
 is not equal to $y


<?php
$x = 100; 
$y = "100";

var_dump($x != $y); // returns false because values are equal
?>
 
Output:
Bool(false)

> 
Greater than
$x > $y
Returns true if 
$x is greater than $y
<?php
$x = 100;
$y = 50;

var_dump($x > $y); // returns true because $x is greater than $y
?>
  

Output:
bool(true)

< 
Less than
$x < $y
Returns true if
 $xis less than $y
<?php
$x = 10;
$y = 50;

var_dump($x <  $y); // returns true because $x is less than $y
?>
  
Output:
Bool(true)

>=
Greater than or equal to
$x >= $y
Returns true if
 $x is greater than or equal to $y

<?php

$x = 50;

$y = 50;

var_dump($x >= $y); // returns true because $x is greater than or equal to $y

?>
 
Output:
bool(true)


<=
Less than or equal to
$x <= $y
Returns true if 
$x is less than or equal to $y






<?php
$x = 50;
$y = 50;

var_dump($x <= $y); // returns true because $x is less than or equal to $y
?>
  
Output:
bool(true)


PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable's value.
The PHP decrement operators are used to decrement a variable's value.
++$x
Pre-increment
Increments $x by one, then returns $x
<?php
$x = 10; 
echo ++$x;
?>
Output:
11

$x++
Post-increment
Returns $x, then increments $x by one

<?php
$x = 10; 
echo $x++;
?>
  
Output:
10
--$x
Pre-decrement
Decrements $x by one, then returns $x
<?php
$x = 10; 
echo --$x;
?>
   
Output:
9
$x--
Post-decrement
Returns $x, then decrements $x by one

<?php
$x = 10; 
echo $x--;
?>
 
Output:
10

PHP Logical Operators

The PHP logical operators are used to combine conditional statements.


&&
And
$x && $y
True if both $x and $y are true

<?php
$x = 100; 
$y = 50;

if ($x == 100 && $y == 50) {
     echo "Hello world!";
}
?>
 
Output:
Hello world!

||
Or
$x || $y
True if either $x or $y is true
<?php

$x = 100; 
$y = 50;

if ($x == 100 || $y == 80) {
     echo "Hello world!";
}
?>
   
Output:

Hello world!

!
Not
!$x
True if $x is not true

<?php

$x = 100; 

if ($x !== 90)
{
     echo "Hello world!";
}

?>
 
Output:
Hello world!

Ternary Logic:

* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true

What Are The Advantages of Ternary Logic?

There are some valuable advantages to using this type of logic:
  • Makes coding simple if/else logic quicker
  • You can do your if/else logic inline with output instead of breaking your output building for if/else statements
  • Makes code shorter
Example :

<?php
$a=10;
$b=15;
echo $a>$b?10:20;
?>

output:
20



PHP Array Operators: 
The PHP array operators are used to compare arrays.

Operator
Name
Example
Result
+
Union
$x + $y
Union of $x and $y
==
Equality
$x == $y
Returns true if $x and $y have the same key/value pairs
===
Identity
$x === $y
Returns true if $x and $y have the same key/value pairs in the same order and of the same types
!=
Inequality
$x != $y
Returns true if $x is not equal to $y
<> 
Inequality
$x <> $y
Returns true if $x is not equal to $y
!==
Non-identity
$x !== $y
Returns true if $x is not identical to $y


 

PHP String Operators:

PHP has two operators that are specially designed for strings.

Operator
Name
Example
Result
.
Concatenation
$txt1 . $txt2
Concatenation of $txt1 and $txt2
.=
Concatenation assignment
$txt1 .= $txt2
Appends $txt2 to $txt1






Browsers

Powered by Blogger.

Feature Post

About Me

My photo
B.E.(COMPUTER ENGINEER) PHP Web developer & Provide Training for Web Development , Digital Marketing ,Android Mobile Apps Development

Post Slider (Homepage)

Blog Archive

Malwares

Transfer Files

Media Players

Download Manager

Window

Torrents Soft

Developer Tools

Botton Ad

Ads

Newsletter

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top