How to Logout a User using PHP

Posted on Updated on

This guide will describe you about to logout a user from your site. Here I provided the snippet too.

Now a days all the websites are including login/ log out page. We have seen to create a login page using HTML and validating the login using PHP before and this article will let you learn how to logout a user when they click on the logout button.

If you want to do a validation on button click. then, include your HTML code in to form tag and give the validating PHP page in the Action attribute. Sample snippet of the HTML code to create a login page and navigate to the validating PHP page on button click will provided below.

HTML code snippet

<html>
<body>
		<form name="login Page" action="Validate.php" method="post">
		<center>
		<div id="content">	
			<div id="heading">
				<h2>Login Application for Beginners</h2>
				<hr>
			</div>
			<table id="tblstyle">
				<tr>
					<td><label name="username">Username:</label></td>
					<td><input type="text" id="username" title="Username" placeholder="Enter username"></td>
				</tr>
				<tr>
					<td><label name="username">Password:</label></td>
					<td><input type="password" title="Password" id="password" placeholder="Enter Password"></td>
				</tr>
				<tr>
					<td align="center" colspan="2">
						<input type="submit" value="Login" id="btnLogin">
						<input type="button" value="Cancel" id="btnCancel">
					</td>
				</tr>
			</table>
		</div>
		</center>
		</form>
	</body>
</html>

If you want to add a styles to your login page, you can add like what i have did in the above code snippet. There are three ways to include your CSS-StyleSheet code to the HTML page. I have written CSS-StyleSheet code in the same page. Add the following CSS snippet between the tag in the HTML page.

CSS code snippet

<style>
			body
			{
				font-family:Algeria;
				font-size:12px;
				margin:0px auto;
				align:center;
				padding:
			}
			#tblstyle
			{
				margin:0px auto;
				background-color:white;
				font-family:Algeria;
				font-size:12px;
			}
			#content
			{
				width:80%;
				height:100%;
				padding:10px 12px;
				background-color:white;
				font-family:Algeria;
				font-size:14px;
			}
			#heading
			{
				font-family:Arial;
				font-size:12px;
				text-align:center;
			}
		</style>

Copy paste the below PHP snippet in to a separate page and save it as logout.php( give name as per your wish.)
and make the code to call the PHP page through a button click event or on the link click.

Logout PHP code snippet

<?php
session_start();
unset($_SESSION);
session_destroy();
header("location:index.htm");
?>

session_start() -> will start the session of the logged user.
unset($_session) -> will reset the session variable. it will remove the current session user name from the session variable
session_destroy() -> will completely remove the session.
header(“location:index.htm”) -> redirecting the user to a particular page.

If you want to call the logout.php page in a link click then follow the below code snippet.

<a href="logout.php" title="Logout from your account">Logout</a>

If you want to call the logout.php page in the button click then follow the above HTML snippet method. Put your button within the tag.

NOTE: Above HTML code snippet is for creating login page.



Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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