PHP Prevention of Direct linking code

soulesschild

Supreme [H]ardness
Joined
Feb 18, 2007
Messages
6,176
Hey folks!

So I have this piece of code that works on my home.php page, but if I try to add this same code to any other page, it just keeps redirecting to the login page->home page. Any idea why? Is it because the home.php should also POST the userID like the index login page does?

Code:
<?php

include('header.php');
//check to see if the user has logged in
if(isset($_POST['userID'])){
	// if it works display ID and allow access
	$userID = $_POST['userID'];
	echo "<strong>User ID: $userID</strong><br>";
	}
	else{
	// if not logged in redirect to login page
		header('Location: index.php');
	};


?>
 
You'll want to read up on the $_POST global var: http://php.net/manual/en/reserved.variables.post.php

You'll also want to check out the isset function: http://us.php.net/manual/en/function.isset.php

Generally speaking, you don't want be POSTing for every page refresh. If you have a session variable ( like userID seems to be ), you can either use the built in session support in php ( http://us.php.net/manual/en/book.session.php ) or roll your own with mysql and cookies ( http://us.php.net/manual/en/function.setcookie.php ).
 
Back
Top