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

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

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

				margin:0px auto;
				margin:0px auto;
				padding:10px 12px;

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


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.


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s