You want to use sessions to see if they are logged in.
Start your page with:
session_start();
So after they have submitted the login info and you have verified it is all ok set a session by simply declaring it:
$_SESSION['logged_id'] = true;
Then when you want to only display something to people who are logged in you can use something like:
if($_SESSION['logged_id']) {
// do stuff here
}
In order to send them back to the page they were trying to view before they logged in you will need to keep track of that page somehow.
For example if you have a link that says login here you could append the page to the url
<a href="login.php?page=this_page.php">Login Here</a>
You can then follow what page equals by either using it as a hidden field in your login form or again using the sessions.
So assuming you include a hidden field in your login form like:
<input type="hidden" name="page" value="<?php echo $_GET['page']; ?>" />
You will have it after you have verified their login details and set the login session so you now know where to send them
if(isset($_POST['page'])) {
$location = $_POST['page'];
}
else {
$location = 'index.php'; // default location in case none was passed
}
//redirect them
header(sprintf("Location: %s",$location));