Home » MySQL, PHP

Session Handling in PHP

15 May 2007 615 views Popularity: 2% Share/Bookmark

email

At first create database named ‘test’ in mysql. Use ‘test’. Then create table ‘project’.

MySQL queries for the above tasks ::

create database test;
use test;
create table project (id int(9) not null auto_increment, username varchar(50), password varchar(50), time datetime, primary key(id));

Now the php code::

config.php

<?php

// $dbhost is a variable and holds the localhost or server name
$dbhost = "localhost"; 

// $dbusername is a variable and holds the database username
// i.e. the username of your database server (mine is set to null)
$dbusername = "root"; 

// $dbpassword holds username password
// i.e. the password of your database server (there is no password in my case)
$dbpassword = ""; 

// $dbname holds the database name
$dbname = "test"; 

// $connect is a variable and stores the connection for future use
// in this case "die" is used to display error message in case the connection with the database fails
$connect = mysql_connect($dbhost,$dbusername,$dbpassword)
	or die ("Could not connect to database"); 

// in this case "or die()" - statement is used to display error message
// incase there is no database with the database name provided
mysql_select_db($dbname,$connect)
	or die ("Could not select database"); 

?>

register.php

<html>
<head>
<title>Register</title>
</head>

<body>

<br/><h2>User Registration ::</h2>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
	<table>
		<tr><td>Username</td><td><input type="text" name="user" /></td></tr>
		<tr><td>Password</td><td><input type="password" name="pass" /></td></tr>
		<tr><td></td><td><input type="submit" name="submit" value="Submit" /></td></tr>
	</table>
</form>

<?php
include("config.php");
$user = $_POST['user'];
$pass = $_POST['pass']; 

// checking whether the username or password field is empty or not
if($user == "" || $pass == "")
{
	echo "<font color='red'>Please fill in all the fields.</font>";
}
else
{
	// inserting the form values to the database
	$result = mysql_query("insert into project(username,password,dtime) values ('$user','$pass',now())",$connect)
	or die (mysql_error()); // or displaying the error
	echo "<font color='green'>Data entered successfully. To login <a href='login.php'>click here</a></font>";
}
?>
</body>
</html>

login.php

<?php session_start(); // session started at the top ?>
<html>
<head>
<title>Login</title>
</head>

<body>
<?php
include("config.php");
$user = $_POST['user'];
$pass = $_POST['pass']; 

$result = mysql_query("select * from project where password='$pass' and username='$user'",$connect)
or die(mysql_error());

while($username = mysql_fetch_assoc($result)) // fetching the associative array
{
	$_SESSION['valid'] = $user; // giving a name username to the session variable
}

if(isset($_SESSION['valid'])) // checking if the session variable is set or not
{
	echo "<font color='#0099cc'><h2>Welcome! ".$_SESSION['valid']." </h2></font>";
	echo "<br/>";
	echo "<a href='secret.php'>Secret Page</a> || <a href='logout.php'>Logout</a>";
}
else
{
	if(isset($user))
	{
		echo "<font color='red'>Invalid username or password.</font>"; // if invalid attempt is made to login
	}
	else
	{
		echo "<font color='green'>You are not logged in.</font>"; // message to be displayed when the login page is loaded at first
	}
	// below the message, the login form is displayed
	?>
<br/><h2>Login ::</h2>
	<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
		<table>
			<tr><td>Username</td><td><input type="text" name="user" /></td></tr>
			<tr><td>Password</td><td><input type="password" name="pass" /></td></tr>
			<tr><td></td><td><input type="submit" name="submit" value="Submit" /></td></tr>
			<td><td></td><td></td></td>
			<tr><td></td><td>Not registered yet? <a href="register.php">Register</a></td></tr>
		</table>
	</form>
<?
}
?>
</body>
</html>

logout.php

<?php session_start(); // session is started at the begining of the page ?>
<html>
<head>
<title>Logout</title>

</head>

<body>
<?php
session_destroy(); // destroying the session variable
echo "You have been successfully logged out."; // displaying the message
echo "<br/><br/><a href='login.php'>Login</a> || <a href='register.php'>Register</a>";
?>
</body>
</html>

secret.php

<?php session_start(); ?>
<html>
<head>
<title>Secret Page</title>
</head>

<body>
<?php
include("config.php");

if(isset($_SESSION['valid'])) // checking if the session variable is set or not
{
	// if the user is authorized then displaying the message in green color
	echo "<font color='green'>You are authorized to view this page ".$_SESSION['valid']."!</font>";
	echo "<br/><br/><a href='logout.php'>Logout</a>";
}
else
{
	// if the user is not authorized then displaying the message in red color
	echo "<font color='red'>You are not authorized to view this page.</font>";
	echo "<br/><br/><a href='login.php'>Login</a> || <a href='register.php'>Register</a>";
}
?>
</body>
</html>

Download full source code: Session Handling in PHP

Cheers,

Related posts:

  1. Fun with strings in PHP (Part 1)
  2. Regular Expression check, Validation in PHP
  3. Very simple add, edit, delete, display in PHP
  4. Magento: Get Set Unset Session
  5. Backup and Recovery of MySQL database
  6. Magento: How to get admin user id, name, username, email, etc?
  7. Add, edit, delete, login, register in PHP :: A simple and complete tutorial
  8. Magento Admin login problem
  9. Page refresh in PHP
  10. How to get(view) html source code of a website
  • Jazosebno

    Perfect example, thanks Mukesh!!