Session Variable Step By Step Tutorial - Part 2: Session is usually used for the security aplications and e-commerce (online shopping). A website user will be checked first whether he is allowed to access the site or not. For example, the user who already gets the permission of 'user' identification. Not all of the user can access the whole website pages itself because there are some of the pages can only be accessed by 'super user'.
In making this application, first, you have to make a file named 'user.dat.php' which contains of three field datas. There are name, password, and user identification data. You can use md5() function in order to make password (string 32 character).
<?
,dian,superuser,f97de4a9986d216a6e0fea62b0450da9,
ryan,user,10c7ccc7a4f0aff03c915c485565b9da,
brian,user,cbd44f8b5b48a51f7dab98abcdf45d4e,
?>
Next, you will see a form where the visitor must have to fill the name and password.
<?
//form login
//user.form.php
if($submit){
$password = md5("$password");
$fh=fopen("user.dat.php","r");
$str= fread($fh,1024*40);
$str01 = explode(",",$str);
$i=1;
$u=0;
while($str01){
if($name==$str01[$i] && $password==$str01[$i+2]){
session_start();
$user_session = "$str01[$i]";
$cus = $i + 1;
$cat_user_session = "$str01[$cus]";
session_register("user_session");
session_register("cat_user_session");
echo "Welcome User: $str01[$i]";
echo ("<p><ol><li><a href=page1.php>Look
page 1 >>></a></li>");
echo (" <li><a href=page2.php>Look
page 2 >>></a></li>");
echo (" <li><a href=page3.php>Look page
3 >>></a></li></ol>");
$u = 1;
break;
}
$i += 3;
if($i>(count($str01)+3)){
break;
}
}
if($u != 1){
echo("<br>You are not user");
}
}else{
echo("<form method=post action=$PHP_SELF>");
echo("<table>");
echo("<tr>");
echo("<td>Welcome");
echo("</td>");
echo("</tr>");
echo("<tr>");
echo("<td>Name <input
type=text name=name size=30>");
echo("</td>");
echo("</tr>");
echo("<tr>");
echo(" <td>Password <input
type=password name=password
size=30>");
echo("</td>");
echo("</tr>");
echo("</tr>");
echo("<tr>");
echo("<td align=right><input type=submit
name=submit value=Process size=30>");
echo("</td>");
echo("</tr>");
echo("</table>");
echo("</form>");
}
?>
Then the result will be: