PHP “session” and a logout example

In the previous blog post I have showed you a login example. Today I am going to show how we can logout from a web site which we have logged. Further more I am going to use session variables in PHP to show your username after you have logged in.

User interface is as same in the login example, so I am not going to put a snapshot of that again here. When you click on the Log In button you will redirect to login.php page. Take a look at the following code and compare it with the previous example and identified the changes I have made here.

$selectQuery = mysql_query("SELECT * FROM hec_login WHERE username='$var_username' AND password='$var_password'");
$fetchArray = mysql_fetch_array($selectQuery);
$numberOrRows = mysql_num_rows($selectQuery);


	header("Location: login_success.php");		
	echo 'Login fail. Redirecting You Back....';


The important thing is I have put the username in to a session variable so that I can access it as a local variable. In this example I have used to show the name of the logged person as follow.

Successfully Logged

From the following code you can see how I used the session variable to show the name in login_success.php.

... echo $_SESSION['session_user']; ...
... (Logout...)...


[Some tags I cannot include because it will not display in the blog. Actually I am new to WordPress so still there are some points I have to learn when I am blogging. So please excuse for that🙂 ]

The when you click on the Logout link, you will be redirect to logout.php page where there are only three lines of codes which is start of the session, destroy of the session and redirecting link.

header("Location: index.php");


So after you logged out of the system you should not be able to access those logged in pages either by giving the full URL in the address bar or by pressing the back arrow of the browser. To activate that you have to include the following code for the logged in pages, as an example login_success.php.


if($_SESSION['session_user']== null){	
		header("Location: index.php");


That’s it for the today’s post. Meet you another day with some simple different thing. Till then keep in touch😉

About AnujAroshA

An undergraduate in the stream of ICT (Information & Communication Technology). A simple person :)
This entry was posted in PHP examples. Bookmark the permalink.

One Response to PHP “session” and a logout example

  1. I am now not sure where you are getting your info, but good topic.
    I must spend some time studying much more or working out more.
    Thanks for fantastic information I was looking for this info for my mission.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s