PHP Session Variables
1. Login page program using PHP and MySQL DBÂ
a) first create a html page as following.
<html> <body > <form method="post" action="userLogin.php" > <table border="1" > <tr> <td> <B>User-Id</B> </td> <td><input type="text" name="userid"> </tr> <tr> <td><B>Password</B></td> <td><input name="password" type="password"></input></td> </tr> <tr> <td><input type="submit" value="Submit"> <td><input type="reset" value="Reset"> </tr> </table> </form> </body> </html>
b) now create page called “userLogin.php” as following code.
<?php
$f_usr= $_POST["userid"];
$f_pswd= $_POST["password"];
$con=mysql_connect("localhost","root","");
if(!$con)
{
die('Connection Failed'.mysql_error());
}
mysql_select_db("mars_db",$con);
$result=mysql_query("select * from users");
while($row=mysql_fetch_array($result))
{
if(($row["usernm"]==$f_usr) && ($row["password"]==$f_pswd))
{
echo "Welcome";
}
}
?>
2. Program on Session Variables
<?php error_reporting(E_ALL ^ E_NOTICE); session_start(); if (!$_SESSION['count']) $_SESSION['count'] = 0; if ($_GET['count'] == 'yes') $_SESSION['count'] = $_SESSION['count'] + 1; echo "<h1>".$_SESSION['count']."</h1>"; ?> <a href="session.php?count=yes">Click here to count</a>
3. Destroying Sessions using PHP
<?php // start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
$_SESSION = array();
session_destroy();
echo "<strong>Step 5 - Destroy This Session </strong><br />"; if($_SESSION['name'])
{ echo "The session is still active"; }
else
{ echo "Ok, the session is no longer active! <br />";
echo "<a href=\"page1.php\"><< Go Back Step 1</a>";
}
?>
