Very Easy and Simple PHP Login and Logout Tutorial

Login and Logout is a must have feature if you are creating a website.

In PHP, I can show you the basic method and mechanism of Login and Logout without any database, at this point.

Watch the video tutorial here:

Here is the source code:

<!DOCTYPE html>
<html>
	<head>
		<title>PHP Login and Logout</title>
	</head>
	<body>
		<?php
			session_start();
			if(isset($_GET["logout"])){
				session_destroy();
			}
			$username = "habibie";
			$password = "somepassword";
			if(isset($_POST["username"]) &amp;&amp; isset($_POST["password"])){
				if($_POST["username"] == $username &amp;&amp; $_POST["password"] == $password){
					echo "<p style='color: green;'>Login Success!</p>";
					$_SESSION["user"] = $_POST["username"];
				}else{
					echo "<p style='color: red;'>Login failed!</p>";
				}
			}
			if(isset($_SESSION["user"]) &amp;&amp; $_SESSION["user"] == $username){
				?>
				<p>Welcome. You are loged in. Click <a href="?logout">here</a> to logout.</p>
				<?php
			}else{
				?>
				<form method="post">
					<label>Username</label>
					<input name="username" type="text"><br>
					<label>Password</label>
					<input name="password" type="password"></br>
					<input type="submit" value="Login">
				</form>
				<?php
			}
		?>
	</body>
</html>
loading...

Leave a Reply

Your email address will not be published. Required fields are marked *