Tuesday, July 3, 2012

Storing user information

Storing user information
Storing user information we can use following functions
  • Cookie
  • session
  • $_POST[](If browser does not support cookies.)
Cookie :-By using cookies we can pass user information from page to page.with browser page back it does not expires until expiration time is passed.
Ex:-First create html form to pass user information to php file

First Name:
Last Name:
<html>
<body>
<table>
<form action="c1.php" method="post">
<tr><td>First Name: </td
<td><input type="text" name="Fname" /></td></tr>
<tr><td>Last Name: </td
<td><input type="text" name="Lname" /></td></tr>
<tr><td colspan=2><input type="submit" /></td></tr>
</form>
</table>
</body>
</html>

Then we will be passing data into 2 php file called c1.php&c2.php

 c1.php
<?php
$Fname=$_POST['Fname'];
$Lname=$_POST['Lname'];
setcookie("user", "$Fname $Lname", time()+3600);
echo "welcome"." ".$_COOKIE["user"];
?>
</br><a href=" c2.php">NEXT</a>
Result(input First Name=Angelina,Last name=Jolie)
welcome Angelina Jolie
NEXT
 c2.php
<?php
echo "welcome Again"." ".$_COOKIE["user"];
?>
welcome Again Angelina Jolie

Session:-By using session also we can pass user information from page to page until it unset or destroy,but with browser back pages it expires.
Ex:-You can use above HTML file to input user information.


First Name:
Last Age:





<html>
<body>
<table>
<form action="s1.php" method="post">
<tr><td>First Name: </td
<td><input type="text" name="Fname" /></td></tr>
<tr><td>Last Age: </td
<td><input type="text" name="Lname" /></td></tr>
<tr><td colspan=2><tr>
</form>
</table>
</body>
</html>

s1.php
<?php
session_start();
$Fname=$_POST['Fname'];
$Lname=$_POST['Lname'];
$_SESSION['First_Name']=$Fname;
$_SESSION['Last_Name']=$Lname;
?>

<html>
<body>

<?php

echo "Welcome"." ". $_SESSION['First_Name']." "
.$_SESSION['Last_Name'];

?>
</br><a href=" s2.php">NEXT</a>
</body>
</html> 
Result
Welcome Angelina Jolie
NEXT
s2.php
<?php
session_start();
?>
<html>
<body>

<?php

echo "Welcome Again"." ". $_SESSION['First_Name']." "
.$_SESSION['Last_Name'];
session_destroy();
?>
</br><a href=" s3.php">NEXT</a>
</body>
</html> 
Welcome Again Angelina Jolie
NEXT

In s2.php session has been destroyed.So s3.php displays only Welcome Again.

<?php
session_start();
?>
<html>
<body>

<?php

echo "Welome Again"." ". $_SESSION['First_Name']." ".$_SESSION['Last_Name'];

?>

</body>
</html>
Welcome Again

$_POST[]:-You are now familiar with $_POST with previous post we used $_POST repeatedly.we can use $_POST when browser does not support cookies.But with $_POST we can pass user information only into one php file otherwise  we have to submit it repeatedly.
Ex:-


First Name:
Last Age:


<html>
<body>
<table>
<form action="p1.php" method="post">
<tr><td>First Name: </td
<td><input type="text" name="Fname" /></td></tr>
<tr><td>Last Name: </td
<td><input type="text" name="Lname" /></td></tr>
<tr><td colspan=2><td></tr>
</form>
</table>
</body>
</html>

p1.php can carry the information but p2.php no information without re submitting information.

<?php
$Fname=$_POST['Fname'];
$Lname=$_POST['Lname'];

echo "welcome"." ".$Fname." ".$Lname;
?>
</br><a href=" p2.php">NEXT</a>
welcome Angelina Jolie
NEXT
<?php
$Fname=$_POST['Fname'];
$Lname=$_POST['Lname'];
echo "welcome"." ".$Fname." ".$Lname;
?>
welcome

No comments:

Post a Comment