Share
Simple PHP Session
Here is a simple PHP Session to hold information from one single user and available in all pages. If we visit another pages, the data store in session variable available to access.
When user input a data in a form, then session will hold the information and available in another pages. If we want to unhold the information, we can destroy the session.
<?php
session_start();
?>
<html>
<head>
<title>MyWebsite</title>
<link rel="stylesheet" type="text/css" href="css.css" />
</head>
<body>
<a name="atas" href="index.php#bawah">Kebawah</a>
<div id="kontainer">
<div id="header">
<div id="menu">
<?php include "menu.php";?>
</div>
</div>
<div style="margin : 20px; auto;">
<?php
$submit = $_POST['submit'];
$nama = $_POST['nama'];
$email = $_POST['email'];
if(isset($submit)){
if($nama == ""){
echo "<script>alert('Nama kosong!');</script>";
}else{
//echo "<script>alert('Berhasil!');</script>";
session_register($nama);
session_register($email);
$_SESSION['nama'] = $nama;
$_SESSION['email'] = $email;
}
}
?>
<script type="text/javascript">
function logout(){
location.href='index.php?mode=logout';
}
</script>
<?php
$mode = $_GET['mode'];
if($mode == "logout"){
session_destroy();
//unset($_SESSION['email']);
echo "<script>alert('Session dihancurkan!');</script>";
echo "<script>location.href='index.php';</script>";
}
?>
<?php if($_SESSION['nama'] != ""){?>
<span style="font-size : 12px; "><a style=" cursor: pointer" onclick="logout()">Logout</a></span>
<h1 align="center">
Selamat Datang <?php echo $_SESSION['nama'];?>!<br />
Email Anda adalah <?php echo $_SESSION['email'];?>
</h1>
<?php }?>
<p align="center">hari ini adalah : <?php echo date("d F Y");?></p>
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
Nama Anda <input type="text" name="nama" /><br />
Email Anda <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Send" />
</form>
<br /><br />
<select name="pilih">
<option value="">Silahkan Pilih Warna</option>
<option value="merah" style="background-color : red">Merah</option>
<option value="biru" style="background-color : blue">Biru</option>
<option value="hijau" style="background-color : green">Hijau</option>
</select><br /><br />
<input type="checkbox" /> Polisi<br />
<input type="checkbox" checked="checked" /> TNI<br /><br />
<input type="password" value="" />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<a name="bawah" href="index.php#atas">Keatas</a>
</div>
</div>
</body>
</html>
Here the menu.php include in index.php
<?php
$a = array("Home"=>"index.php","My Profile"=>"index.php?page=profile#bawah","Gallery"=>"index.php?page=gallery","Buku Tamu"=>"index.php?page=bukutamu");
foreach($a as $menu=>$link){
?>
<a href="<?php echo $link;?>"><div><?php echo $menu;?></div></a>
<?php }?>
Here the css file
body{background : #006600; margin : 10px;}
#kontainer{width : 900px; border : 1px solid #009900; background : #FFFFFF; box-shadow : 3px 3px 5px #00FF00; height : 600px; margin : auto;-moz-border-radius : 25px 25px 25px 25px;}
#kontainer #header{background : url(images/welcome.jpg) #006600 no-repeat center; background-size : 100%; height : 120px; width : 900px; -moz-border-radius : 25px 25px 0 0;}
#kontainer #header #menu{background-color : #FFFFF; margin : auto; text-align : center; padding-top : 90px;}
#kontainer #header #menu a{color : #FFFFFF;}
#kontainer #header #menu div{display : inline}
Post by : Oka Dayendra
Post on : 28 May 2011
File : simple_session.zip, size : 0 bytes
Title : Simple PHP Session
Category :
PHP
Source :
http://www.leakbali.com