php session tutorial
Here is an example of php session.create following file with .php extension
1.index.php
2.login.php
3.logout.php
4.page1.php
5.page2.php
6.page3.php
Now fill the files with following code.
1.index.php
<form action="login.php" method=post>
username: <input type=text name=username >
<input type=submit value=login >
</form>
<a href="index.php">home</a>
<a href="page1.php">page 1</a>
<a href="page2.php">page 2</a>
<a href="page3.php">page 3</a>
2. login.php
<?php
$username = addslashes($_POST['username']);
if(empty($username)){
header("Location: index.php");
}else{
session_start();
$_SESSION['auth'] = 'yes';
$_SESSION['user'] = $username;
header("Location: page1.php");
}
?>
3.logout.php
<?php
session_start();
$_SESSION['user'] = '';
session_destroy();
header("Location: index.php");
?>
4.page1.php
<?php
session_start();
$_SESSION['auth'] = 'yes';
$username = $_SESSION['user'];
if(!empty($username)){
echo "welcome <b>" . $username ." </b> you are logged in to logout <a href=logout.php >click here</a> <BR> I am using page 1 :C :( haaaaaaa";
?>
<br>
<a href="index.php">home</a>
<a href="page1.php">page 1</a>
<a href="page2.php">page 2</a>
<a href="page3.php">page 3</a>
<?php
}else
header("Location: index.php");
?>
5.page2.php
<?php
session_start();
$_SESSION['auth'] = 'yes';
$username = $_SESSION['user'];
if(!empty($username)){
echo "welcome <b>" . $username ." </b> you are logged in to logout <a href=logout.php >click here</a> <BR> this is page 2 lkjhdslk jklsah gklh lkfgksd f";
?>
<br>
<a href="index.php">home</a>
<a href="page1.php">page 1</a>
<a href="page2.php">page 2</a>
<a href="page3.php">page 3</a>
<?php
}else
header("Location: index.php");
?>
6.page3.php
<?php
session_start();
$_SESSION['auth'] = 'yes';
$username = $_SESSION['user'];
if(!empty($username)){
echo "welcome <b>" . $username ." </b> you are logged in to logout <a href=logout.php >click here</a> <br> I am now in page three hehehe";
?>
<br>
<a href="index.php">home</a>
<a href="page1.php">page 1</a>
<a href="page2.php">page 2</a>
<a href="page3.php">page 3</a>
<?php
}else
header("Location: index.php");
?>
Special thanks to hussein huzimeh.
for further info please write in comment.
No Response to "php session tutorial"
Post a Comment
leave a comment