On your loging page you should start with
// start session
session_start();
// unset any session data until user submits valid username and password
unset($_SESSION);
Then when user enters a valid username (i.e johnybegoode) and password set session variables you need, such as
$_SESSION['username'] = 'johnybegoode';
Then on other pages for that user first check for valid username using the code from previous post
<?php
session_start();
// say that user johnybegoode has successfuly logged in and $_SESSION['username'] was set
// to johnybegoode
if(!isset($_SESSION['username'] or $_SESSION['username'] !='johnybegoode')){
// re-direct ke index.php
header("location:index.php");
}
?>
Then provide a logout link which points to the login page. When user clicks on it (logs out) the session is unset on the login page first.
Option 2: Your logout link can point to some other page (logout.php) where you can thank the user, unset session, do other cleanup, log the event etc and automaticaly redirect to login page.
Note: you can not unset session with javascript onclick directly, you have to use ajax and implement a javascript function that calls a php script that unsets the session but that is a more complex topic.
Even more important note: in this example there was nothing said about security. Make sure you do all the security exercises when dealing with input and session. See previous posts in this thread and other threads here and arround.